0

Write a class LinkedList

Write a class LinkedList that holds a linked list of values. Your class should be in a source file named linked_list.py, and should support the following operations: LinkedList(list) - create a LinkedList that initially holds the values in the given Python list l.to_list() - return a Python list containing the values in this LinkedList l.len() - return the number of nodes in a LinkedList l.get(n) - return the value in the nth node, where nodes are numbered from 0. You may assume that 0 <= n < l.len(). l.has(x) - true if the list includes the value x l.delete(x) - delete the first occurrence (if any) of the value x Important: You may not use Python lists anywhere, except in the initializer and the to_list() method. Your LinkedList class may not store a Python list in any attribute. Also, generators are not allowed in this assignment. You do not need to read any input or write any output; simply submit a file linked_list.py containing the class described above. >>> l = LinkedList([2, 7, 4, 9, 18, 19, 22]) >>

17th Feb 2024, 5:36 PM
safir safir1
safir safir1 - avatar
14 ответов
+ 2
init should be __init__ in Node and in LinkedList. https://docs.python.org/3/reference/datamodel.html#object.__init__ The readability convention is to separate top level code blocks such as classes with two blank lines. Inside of those, separate smaller code blocks such as methods with one blank line. For example: class Node:     def __init__(self, value):         self.value = value         self.next = None class LinkedList:     def __init__(self, values):         self.head = None         self.tail = None         for value in values:             self.append(value)     def append(self, value):         new_node = Node(value)         if not self.head:             self.head = new_node             self.tail = new_node         else:             self.tail.next = new_node             self.tail = new_node [etc.] https://peps.python.org/pep-0008/#blank-lines
17th Feb 2024, 9:59 PM
Rain
Rain - avatar
+ 2
Rain yes, having interoperability with standard python methods is probably a better design decision. You can implement both, but it feels like unnecessary code bloat and feels like training people to code redundant stuff.
18th Feb 2024, 1:58 PM
Bob_Li
Bob_Li - avatar
+ 2
Bro this is weird. - Why do you want help with the challenge when the challenge was meant for you to grow? - If you don't know how to do it, shouldn't you search and read-up on the problem so you could actually grow and learn? - Why not run your code in the Sololearn playground to see if your code is valid or works as intended?
18th Feb 2024, 10:30 PM
Ryan
Ryan - avatar
+ 1
put your code in a script on sololearn playground – not in a comment.
17th Feb 2024, 6:57 PM
Lisa
Lisa - avatar
+ 1
safir safir1 , I know you can't change the requirements of the task, but I don't like them. They ask for regular methods that really should be special methods. For example, they ask you to write a len method so you can then call it with the dot syntax like this. print(my_linked_list.len()) But they should ask you to write a __len__ method so you can use the built in len() function as you would for any other collection such as str or list. print(len(my_linked_list)) https://docs.python.org/3/reference/datamodel.html#object.__len__
17th Feb 2024, 10:24 PM
Rain
Rain - avatar
+ 1
Ryan , What? Nobody's putting anybody down. I didn't create the continuation. I linked it, because it's more recent and a better place to add new comments.
18th Feb 2024, 11:56 PM
Rain
Rain - avatar
0
Do you have a question or do you just want us to do your homework?
17th Feb 2024, 5:45 PM
Lisa
Lisa - avatar
0
No just I want to help me
17th Feb 2024, 6:27 PM
safir safir1
safir safir1 - avatar
0
class Node:     def init(self, value):         self.value = value         self.next = None class LinkedList:     def init(self, values):         self.head = None         self.tail = None         for value in values:             self.append(value)     def append(self, value):         new_node = Node(value)         if not self.head:             self.head = new_node             self.tail = new_node         else:             self.tail.next = new_node             self.tail = new_node     def to_list(self):         result = []         current = self.head         while current:             result.append(current.value)             current = current.next         return result     def len(self):         length = 0         current = self.head         while current:             length += 1             current = current.next         return length     def get(self, n):         current = self.head         for _ in range(n):             current = current.next         return current.value       
17th Feb 2024, 6:32 PM
safir safir1
safir safir1 - avatar
0
  def has(self, x):         current = self.head         while current:             if current.value == x:                 return True             current = current.next         return False     def delete(self, x):         if not self.head:             return         if self.head.value == x:             self.head = self.head.next             if not self.head:                 self.tail = None             return         current = self.head         while current.next:             if current.next.value == x:                 current.next = current.next.next                 if not current.next:                     self.tail = current                 return             current = current.next    
17th Feb 2024, 6:32 PM
safir safir1
safir safir1 - avatar
0
Is this code false?
17th Feb 2024, 6:34 PM
safir safir1
safir safir1 - avatar
0
See also the more recent continuation thread. https://www.sololearn.com/Discuss/3265755/?ref=app
18th Feb 2024, 10:50 PM
Rain
Rain - avatar
0
Hi Rain There is no need to point out more of the same behavior and implicitly say, "Oh, look what he's also done." I asked these questions for him to reflect and grow from. Not to put him down. I don't see how posting a continuation thread is going to help.
18th Feb 2024, 11:37 PM
Ryan
Ryan - avatar
0
Rain I think there's a misunderstanding on your part about Ryan 's posts. in the Ryan's first post, "Bro" is probably refers to safir safir1. in the second code, Ryan is suggesting that maybe we should let safir safir1 figure out where the mistakes are, and maybe we are being too helpful. Though giving a link to where this post continues is a very helpful move, so that others can follow along. So I think posting a link to where the OP continued the thread is a good move. Maybe the max-character limit was hit and instead of continuing in a post, it was continued in a new thread... I'm interested to see if the OP finally manages to fix the mistake. The code is already working as intended if the two mistakes are fixed.
19th Feb 2024, 1:05 AM
Bob_Li
Bob_Li - avatar