# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head=None cur=None while l1 is not None or l2 is not None: if head is None: if l1 is not None and l2 is not None and l1.val<l2.val: head=l1 l1=l1.next cur=head elif l1 is not None and l2 is not None and l1.val>l2.val: head=l2 l2=l2.next cur=head elif l1 is None and l2 is not None: head=l2 l2=l2.next cur=head else: head=l1 l1=l1.next cur=head elif l1 is not None and l2 is not None and l1.val<l2.val: cur.next=l1 l1=l1.next cur=cur.next elif l1 is not None and l2 is not None and l1.val>l2.val: cur.next=l2 l2=l2.next cur=cur.next elif l1 is None and l2 is not None: cur.next=l2 l2=l2.next cur=cur.next else: cur.next=l1 l1=l1.next cur=cur.next return head
Tuesday, April 17, 2018
Beat 99.95% of submitted solutions for Merge Two Sorted Linked Lists
Yes, I did it again. Simple enough.
Monday, April 9, 2018
Beat 95.56% of python submissions
Good result. I'm happy.
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def removeNthFromEnd(self, head, n): """ :type head: ListNode :type n: int :rtype: ListNode """ count=0 tmp=list() cur=head while cur is not None: # if count>n: # break tmp.append(cur) cur=cur.next count+=1 if count==1 and n==1: return None if n==1: tmp[len(tmp)-n-1].next=None return head if n==len(tmp): head=None head=tmp[1] return head if n>0: tmp[len(tmp)-n-1].next=tmp[len(tmp)-n+1] return head if __name__ =='__main__': s=Solution() class ListNode: def __init__(self, x): self.val = x self.next = None self.head=self def add_to_the_end(self, val): cur=self.head while cur.next is not None: cur=cur.next cur.next=ListNode(val) def printLinkedList(self): cur =self.head while cur is not None: print (cur.val) cur=cur.next ln=ListNode(1) ln.add_to_the_end(2) ln.add_to_the_end(3) ln.add_to_the_end(4) ln.add_to_the_end(5) s.removeNthFromEnd(ln,5) ln.printLinkedList()
Monday, March 12, 2018
Beat 100% of python3 submissions
Yes! I did it.
Here is simple code that gives me performance described on picture above.
Here is simple code that gives me performance described on picture above.
class powxy: def pow(self, x, n): if n == 0.0: return 1 if n < 0: n = -n x = 1/x if n % 2 == 0: return self.pow(x*x, int(n/2)) return x*self.pow(x*x, int(n/2))
Tuesday, March 6, 2018
How to use virtual invironment in python 3.6
A Virtual Environment, allows to create an isolated working copy of Python. It is quite common situation when Python applications use packages that are not part of the standard library. It happens that multiple applications may require different version of the same library. In other words- single installation of Python will not be able to serve all the possible requirements for applications. This issue can be solved by utilizing Virtual environments. Virtual environment is directory that contains all the Python installation and all the required packages. Different Python application may use dedicated virtual environments.
Starting Python 3.6 - virtual environment creation module shipped as part of Python installation.
Here is example - how to create virtual environment using Python 3.6
This command creates new forlder "env" in current directory (if directory not yet exists). It also create subdirectories in "env" directory that containing a copy of the Python interpreter, the standard library, supporting files, etc.
After virtual environment is created- it needs to be activated:
Depend on OS type- it can be done in several ways.
For Windows:
For Linux:
After virtual environment is activated- shell will show activated virtual environment as a prefix in command prompt: (venv).
Now we can install all the required libraries for our current Python application.
For example:
All the installed packages will be stored inside of "venv" folder and not overlap/affect any other Python applications.
v
Starting Python 3.6 - virtual environment creation module shipped as part of Python installation.
Here is example - how to create virtual environment using Python 3.6
python -m venv env
This command creates new forlder "env" in current directory (if directory not yet exists). It also create subdirectories in "env" directory that containing a copy of the Python interpreter, the standard library, supporting files, etc.
After virtual environment is created- it needs to be activated:
Depend on OS type- it can be done in several ways.
For Windows:
.\env\Scripts\activate.bat
For Linux:
./env/Scripts/activate.sh
After virtual environment is activated- shell will show activated virtual environment as a prefix in command prompt: (venv).
Now we can install all the required libraries for our current Python application.
For example:
python -m pip install beautifulsoup4
python -m pip install lxml
All the installed packages will be stored inside of "venv" folder and not overlap/affect any other Python applications.
v
Subscribe to:
Posts (Atom)