I am often compiling lists one element at a time in python 3; say for instance I am making a list by going through a linked list with first element head:
l = []
while head:
l.append(head.val)
head = head.next
I was wondering what the best practices are. Is there another way of writing this? Could it be possible to describe the list in one line, with something like this instead:
while head:
l = # something creating the list AND appending elements
head = head.next
Even better: do I always have to use a loop to create lists in similar cases, or are there often ways to make the desired list in one line ?
Thanks!
EDIT : a typo in the code !
From an OOP perspective, the best practice is to rely on Python's __iter__ method to cast an iterable to a list.
I am assuming your linked-list class looks a bit like this.
class LinkedList:
def __init__(self, value, nxt=None):
self.value = value
self.next = nxt
To allow iteration on your linked-list, you can define __iter__
class LinkedList:
def __init__(self, value, nxt=None):
self.value = value
self.next = nxt
def __iter__(self):
while self:
yield self.value
self = self.next
You can then let list handle the cast of the LinkedList iterable.
head = LinkedList(1, LinkedList(2, LinkedList(3)))
lst = list(head) # [1, 2, 3]
Related
I'm new to programming, so excuse the possibly stupid question.
I'm doing leetcodes and I got to the linked lists. I think I understand them okay, it's just that I don't know how to test my code/call my function(?)
Problem I'm working on
Here's my code, I know it works since I uploaded it onto leetcode, but I would still like to be able to run it on my machine.
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
I guess I have two different problems:
the "Optional[ListNode]) -> Optional[ListNode]:" part
and the actual calling of the function
Some questions before used the "typing" module functions like "List", so I would simply import them and they wouldn't be a problem. But I'm not really sure what to do here
To check my solutions, I write a short piece of code that I can put example inputs into
Solution = Solution()
print(Solution.middleNode(head = [1,2,3,4,5,6]))
But isn't the "head" there, just a normal list? Do I have to create an extra function separately to create the "links". I've seen the creation of a linked list done by calling a function every time you want to add a new node. So would I use a for loop to add my example case?
well if you looking for internal boilerplate, below is code for that.
here you need to create classes for nodes, linked list and solutions,.
then with the given number, you need to create a linkedlist object.
this above part is done in leetcode by themself and this object is passed to class Solution method middleNode, where OP code run and give result. next once output is got it is compared with existing solution
# Node class for individual nodes
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# linked list class, to create a linked list of the list nodes
class LinkedList:
def __init__(self):
self.head = None
# adding a node element to linked list
def add(self, node):
if self.head is None:
self.head = node
else:
curr = self.head
while curr.next:
curr = curr.next
curr.next = node
curr = node
# printing element of existing linked list
def print_ll(self):
curr= self.head
while curr:
print(curr.val)
curr= curr.next
# leetcode solution class
class Solution:
def middleNode(self, head) :
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
# value with which we create list nodes
values = [1,2,3,4,5,6,7,8,9,10]
ll = LinkedList() # create a linked list class object
for i in values:
node = ListNode(i) # creating a list node
ll.add(node) # adding list node to linked list
#ll.print_ll() # printing linked list
x = Solution().middleNode(ll.head) # passing linked list object to leetcode solution method and getting result
while x: # printing result
print(x.val)
x=x.next
I think the problem on LeetCode is poorly worded. head = [1,2,3,4,5] is not really a head as it should only refer to the first item in the list - there seems to be a bit of hidden boilerplate code that creates a linked list from input list and an output list from output node.
Here's an example code that works similiar to the LeetCode task.
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
inp = [1,2,3,4,5,6]
next = None
for i in reversed(inp):
next = ListNode(i, next) # next points to head at the end of the loop
res = Solution().middleNode(next)
out = []
while res:
out.append(res)
res = res.next
print([o.val for o in out])
to make your code work on your machine you have to implement a couple of things:
First, for your first answer, you have to implement the class ListNode given at the top of the leetcode-page:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
The you import "Optional" from typing:
from typing import Optional
Then you have the prerequisites for your code.
You have to initialise the class, as you have mentioned. The only problem here is, that your variable has the same name as your class, what could cause trouble later.
To finish, you have to call your function as you already did, with one little difference: This function has to be called with "head" as a variable of type ListNode, not List, and gives you back a variable of the type ListNode.
In a nutshell, this would be my solution (of course you can and as much ListNodes as you want):
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
# Initialise class
s = Solution()
# Defining nodes of the list, assigning always the next node.
seven = ListNode(7)
six = ListNode(6, next=seven)
five = ListNode(5, next=six)
four = ListNode(4, next=five)
three = ListNode(3, next=four)
two = ListNode(2, next=three)
one = ListNode(1, next=two)
# Calling your function (with "one" as your head node of the list)
# NOTE: As this gives you back an attribute of type ListNode, you have to access the "val" attribute of it to print out the value.
print(s.middleNode(one).val)
Is it possible to write this single linked list program in the class Node and using all methods of class LinkedList inside that by eliminating the class LinkedList and using only one class for all this ? If it is possible then why we prefer writing this way and why not with a single class ?
Some say we use it to keep a track of head node but I don't get it. We can simply use a head named variable to store the head node and then use it further in other operations. Then why a different class for head node.
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def deleteNode(self, key):
temp = self.head
if (temp is not None):
if (temp.data == key):
self.head = temp.next
temp = None
return
while(temp is not None):
if temp.data == key:
break
prev = temp
temp = temp.next
if(temp == None):
return
prev.next = temp.next
temp = None
def printList(self):
temp = self.head
while(temp):
print (" %d" %(temp.data)),
temp = temp.next
There are lots and lots of ways to implement the same interface as this LinkedList class.
One of those ways would be to give the LinkedList class data and next fields, where next points to a linked list... but why do you think that is better? The Node class in your example is only used inside a LinkedList. It has no external purpose at all. The author of this LinkedList class made two classes so he could separate the operations applied to nodes from the operations applied to the list as a whole, because that's how he liked to think about it. Whether you think your way is better or worse is a matter of choice...
But here's the real reason why it's better to keep LinkedList separate from nodes:
In real life programming, you will encounter many singly-linked lists. Probably none of these will be instances of any kind of List class. You will just have a bunch of objects that are linked together by some kind of next pointer. You will essentially have only nodes. You will want to perform list operations on those linked nodes without adding list methods to them, because those node objects will have their own distinct purposes -- they are not meant to be lists, and you will not want to make them lists.
So this LinkedList class you have above is teaching you how to perform those list operations on linked objects. You will never write this class in real life, but you will use these techniques many many times.
Below is a code that divides each number of the node by 10. For example, node = 2->3->3, output would be 0.2->0.3->0.3.
However I am confused on why self.head.next gets updated each time given it's cur that receive the change. Suppose a=1,b=2 and we make a(cur)=b(self.head), if we change the value of a to 3, that wouldn't affect b, b is still 2. Therefore I couldn't understand why changing cur would affect self.head.next. Thank you!
class node(object):
def __init__(self,value,next=None):
self.value=value
self.next=next
class linkedlist(object):
def __init__(self):
self.head=None
self.next=None
def test(self,List):
self.head=node(0)
cur=self.head
while List:
s=List.value/10
cur.next=node(s)
cur=cur.next
List=List.next if List else 0
return self.head.next
Suppose below is the input:
a=node(2)
a=node(2,a)
a=node(3,a)
c=linkedlist()
Below is the output:
c.test(a).value=0.3
c.test(a).next.value=0.2
c.test(a).next.next.value=0.2
I am confused on why self.head.next gets updated each time given it's cur that receive the change
That is because, at least in the first iteration of the loop, self.head is cur:
cur=self.head
After that, it builds a new list with updated values, using a "dummy" node as self.head (and thus the first cur) which is then discarded and only it's next is returned. However, I find that code rather confusing and overly complicated (I had a hard time understanding it myself), e.g. the ternary ... if ... else ... in the last line is redundant as List can not be None at that point. Also, there's no need in making that a class, since none of its member attributes are used beyond the scope of a single execution of the method.
Instead, you could use a simple function, e.g. using a loop and modifying the original list, or even simpler, recursively creating a new list:
def div(lst, d=10):
first = lst
while lst:
lst.value /= d
lst = lst.next
return first
def div(lst, d=10):
return node(lst.value / 10, div(lst.next, d)) if lst else None
For easier debugging, you can also add a __repr__ method to your node class:
def __repr__(self):
return "(%r %r)" % (self.value, self.next)
I'm confused with this Stack implementation (ADT). Mainly with adding and removing options, but also by using empty dictionary, instead of empty list.
Here is my problem:
By using an empty list, removing and adding implementations are understandable:
class Stack:
def __init__(self):
self.elements = []
def add(self, item):
self.elements.append(item)
def remove(self):
self.elements.pop()
However, I'm confusing such implementations for an empty dictionary:
class D:
def __init__(self):
self.elements = {}
self.index = 0 # since only adding one element per add method
# better to use indexing for key1, then key2
def add(self, item):
# For dictionary better to use
# self.elements[item] = new_item
# but in stack we adding one element
self.elements[item] = self.index
self.index = item
def remove(self):
self.elements.pop(self.index)
For this code, I want to create new dictionary after adding two elements:
For instance we add(7), add(8) and we get {7:8}. But instead, after adding more elements, like add(9) and add(10), I got {7:8}, {8:9}, {9:10}.
How do I deal with that? My remove method working fine, but adding seems not great.
I am in need of a Python (2.7) object that functions like a set (fast insertion, deletion, and membership checking) but has the ability to return a random value. Previous questions asked on stackoverflow have answers that are things like:
import random
random.sample(mySet, 1)
But this is quite slow for large sets (it runs in O(n) time).
Other solutions aren't random enough (they depend on the internal representation of python sets, which produces some results which are very non-random):
for e in mySet:
break
# e is now an element from mySet
I coded my own rudimentary class which has constant time lookup, deletion, and random values.
class randomSet:
def __init__(self):
self.dict = {}
self.list = []
def add(self, item):
if item not in self.dict:
self.dict[item] = len(self.list)
self.list.append(item)
def addIterable(self, item):
for a in item:
self.add(a)
def delete(self, item):
if item in self.dict:
index = self.dict[item]
if index == len(self.list)-1:
del self.dict[self.list[index]]
del self.list[index]
else:
self.list[index] = self.list.pop()
self.dict[self.list[index]] = index
del self.dict[item]
def getRandom(self):
if self.list:
return self.list[random.randomint(0,len(self.list)-1)]
def popRandom(self):
if self.list:
index = random.randint(0,len(self.list)-1)
if index == len(self.list)-1:
del self.dict[self.list[index]]
return self.list.pop()
returnValue = self.list[index]
self.list[index] = self.list.pop()
self.dict[self.list[index]] = index
del self.dict[returnValue]
return returnValue
Are there any better implementations for this, or any big improvements to be made to this code?
I think the best way to do this would be to use the MutableSet abstract base class in collections. Inherit from MutableSet, and then define add, discard, __len__, __iter__, and __contains__; also rewrite __init__ to optionally accept a sequence, just like the set constructor does. MutableSet provides built-in definitions of all other set methods based on those methods. That way you get the full set interface cheaply. (And if you do this, addIterable is defined for you, under the name extend.)
discard in the standard set interface appears to be what you have called delete here. So rename delete to discard. Also, instead of having a separate popRandom method, you could just define popRandom like so:
def popRandom(self):
item = self.getRandom()
self.discard(item)
return item
That way you don't have to maintain two separate item removal methods.
Finally, in your item removal method (delete now, discard according to the standard set interface), you don't need an if statement. Instead of testing whether index == len(self.list) - 1, simply swap the final item in the list with the item at the index of the list to be popped, and make the necessary change to the reverse-indexing dictionary. Then pop the last item from the list and remove it from the dictionary. This works whether index == len(self.list) - 1 or not:
def discard(self, item):
if item in self.dict:
index = self.dict[item]
self.list[index], self.list[-1] = self.list[-1], self.list[index]
self.dict[self.list[index]] = index
del self.list[-1] # or in one line:
del self.dict[item] # del self.dict[self.list.pop()]
One approach you could take is to derive a new class from set which salts itself with random objects of a type derived from int.
You can then use pop to select a random element, and if it is not of the salt type, reinsert and return it, but if it is of the salt type, insert a new, randomly-generated salt object (and pop to select a new object).
This will tend to alter the order in which objects are selected. On average, the number of attempts will depend on the proportion of salting elements, i.e. amortised O(k) performance.
Can't we implement a new class inheriting from set with some (hackish) modifications that enable us to retrieve a random element from the list with O(1) lookup time? Btw, on Python 2.x you should inherit from object, i.e. use class randomSet(object). Also PEP8 is something to consider for you :-)
Edit:
For getting some ideas of what hackish solutions might be capable of, this thread is worth reading:
http://python.6.n6.nabble.com/Get-item-from-set-td1530758.html
Here's a solution from scratch, which adds and pops in constant time. I also included some extra set functions for demonstrative purposes.
from random import randint
class RandomSet(object):
"""
Implements a set in which elements can be
added and drawn uniformly and randomly in
constant time.
"""
def __init__(self, seq=None):
self.dict = {}
self.list = []
if seq is not None:
for x in seq:
self.add(x)
def add(self, x):
if x not in self.dict:
self.dict[x] = len(self.list)
self.list.append(x)
def pop(self, x=None):
if x is None:
i = randint(0,len(self.list)-1)
x = self.list[i]
else:
i = self.dict[x]
self.list[i] = self.list[-1]
self.dict[self.list[-1]] = i
self.list.pop()
self.dict.pop(x)
return x
def __contains__(self, x):
return x in self.dict
def __iter__(self):
return iter(self.list)
def __repr__(self):
return "{" + ", ".join(str(x) for x in self.list) + "}"
def __len__(self):
return len(self.list)
Yes, I'd implement an "ordered set" in much the same way you did - and use a list as an internal data structure.
However, I'd inherit straight from "set" and just keep track of the added items in an
internal list (as you did) - and leave the methods I don't use alone.
Maybe add a "sync" method to update the internal list whenever the set is updated
by set-specific operations, like the *_update methods.
That if using an "ordered dict" does not cover your use cases. (I just found that trying to cast ordered_dict keys to a regular set is not optmized, so if you need set operations on your data that is not an option)
If you don't mind only supporting comparable elements, then you could use blist.sortedset.