Thanks for pointing this out! Sometimes it’s hard on Codecademy to tell whether you’re being shown best practice, or whether something is altered for the sake of teaching the lesson.
I went a different route from the solution. I got the same result (sort of) but with one issue.
When I set the variable list_string and use either ""
or "\n"
I get the below:
def stringify_list(self):
lst = [self.head_node]
i = 0
while lst[i].next_node != None:
lst.append(lst[i].next_node)
i += 1
list_string = "\n".join([str(item) for item in lst])
return list_string
Output from above:
Resulting error from above:
def stringify_list(self):
lst = [self.head_node]
i = 0
while lst[i].next_node != None:
lst.append(lst[i].next_node)
i += 1
list_string = "".join([str(item) for item in lst])
return list_string
Output from above:
Resulting error from above:
I am confused.
Update:
I forgot that I had added the below to my Node class:
def __repr__(self):
return "{value} \n".format(value=self.value)
That explains the issue with the extra lines.
But I’m confused why it does not recognize this as a valid solution. Would this not be a valid solution?
def stringify_list(self):
string_list=""
while self.head_node!=None:
string_list=str(self.head_node.value)+"\n"+string_list
self.head_node=self.head_node.next_node
return string_list
Test your code by uncommenting the statements below - did your list print to the terminal?
ll = LinkedList(5)
ll.insert_beginning(70)
ll.insert_beginning(5675)
ll.insert_beginning(90)
print(ll.stringify_list())
Can anyone tell me the issue with my stringify_list() method? I am getting the desired output but the exercise is throwing error saying “sla was [’ ']”
I’m not certain what sla
is supposed to mean but there may be an issue with extra characters in that output. I can see '90 '
for example with an extra whitespace and there’s an empty string at the end which is unnecessary. A lot of the string checking tests must be exact, excess whitespace and empty strings probably shouldn’t be in the solution.
Hi @aljose0701.
Could you please format your code as per the guidance shown here as it’s hard to see what is going on without indentation-
Have a close look at the statements inside your while loop. What happens to that value with repeated iterations?
Thank you for your reply. But this is the error I am getting from CodeCademy. Hence, unable to proceed further. Removed all white spaces, and tried again. Still no luck. Its throwing the same error. SOS.
Here is the indented code for your reference:
def stringify_list(self):
string_list=""
while self.head_node!=None:
string_list=str(self.head_node.value)+string_list
self.head_node=self.head_node.next_node
return string_list
# Test your code by uncommenting the statements below - did your list print to the terminal?
ll = LinkedList(5)
ll.insert_beginning(70)
ll.insert_beginning(5675)
ll.insert_beginning(90)
print(ll.stringify_list())```
Thanks for formatting it. Have a look at what happens inside that while loop (use prints statements or similar if necessary). What is being added to what when you attempt to build your list of strings?
Is there any difference between doing these things:
def stringify_list(self):
stringified_list = ""
current_node = self.get_head_node()
while current_node:
stringified_list += f"{current_node.get_value()}\n"
current_node = current_node.get_next_node()
return stringified_list
def stringify_list(self):
stringified_list = ""
current_node = self.head_node
while current_node:
stringified_list += f"{current_node.value}\n"
current_node = current_node.next_node
return stringified_list
I don’t know if it’s just better for performance not using the attributes directly or if it’s some kind of ‘Clean Code’ but I couldn’t see any changes between both
It’s a bit complex as the use of getters and setters in Python is a little different to some languages, especially since everything is public in Python. You can read up on why they’re used in other languages such as Java- https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors/1568230#1568230
If you’re looking for the Pythonic equivalent see the following which goes into a little detail on the @property decorator-
https://www.python-course.eu/python3_properties.php
If the instructions suggest using them then it might be best to just follow them for now to avoid any issues with future lessons making use of them. Worth being aware of them and their alternative at the very least.
I also don’t get how “while” works in this case. I get that we need to use a loop to go through the linked_list object, but how does while work in this exercise? Why is a Node Object == True, and how does the loop terminate??
Very disappointed with Codecademy, why leave it up to volunteer users to explain the solution for this exercise, and not provide a clear solution. I know going through forums is part of the process of learning how to work with Python (I’m not stranger going to StackOverflow or watching Youtube videos), but I expect to get proper help if I’m paying for a subscription on Codecademy.
Did you happen to skip over the Python 3 introduction course? It covers while and for loop basics. Jumping into an advanced unit right out of the blocks is like jumping into deep end and not being able to tread water. Even the basics will throw you off. Start from the beginning, not the end.
I get that we need to use a loop to go through the linked_list object, but how does while work in this exercise? Why is a Node Object == True, and how does the loop terminate??
Like monochromaticmau pointed out, I understand:
> x=0
> while x <3:
> print(x)
> x += 1
But how should I then understand how the following works:
while current_node:
That depends upon the node having a value other than None
, I suspect.
I really do appreciate your help and that from other users discussing this exercise, like this one:
These posts go a long way to understanding that an object of a class also has a default boolean value.
I just think Codecademy itself could have done better job covering the usage of “while” in this context.
@mtf
Can you please tell me how these two codes are different, for some reason the CodeAcademy runner complains about it:
This is my code:
``
def stringify_list(self):
to_return = ""
while self.head_node.get_next_node() != None:
to_return += str(self.head_node.get_value()) + "\n"
self.head_node = self.head_node.get_next_node()
to_return += str(self.head_node.get_value())
return to_return
This is CodeAcademy’s Code:
def stringify_list(self):
string_list = ""
current_node = self.get_head_node()
while current_node:
if current_node.get_value() != None:
string_list += str(current_node.get_value()) + "\n"
current_node = current_node.get_next_node()
return string_list
The results are the same but it still comlains by giving me the following error:
sla was[‘5’]
Did the instructions ask us to use a specified variable name?
I understand that I have to iterate over the nodes until I reach a node without reference to another node (end of the list). But in order to do this iteration I need to have those nodes somewhere. Like when I iterate over the list I have a list with all elements present there. So where would the nodes to iterate over come from? Are they all stored in an instance of a class? Like the instance ll in the code?
In short, no. They are independent instances of the Node class. Only the head node knows where the next node in the list is. That node knows where the node following is. It is the next
property of the Node objects that keeps the list alive. There is no contiguous block of memory. The objects occur in memory wherever the system instantiates them.
Maintenance of such a structure is confined to updating of the next
property in order to remove, or orphan a node. We would never sort this type of structure, only manage the order in which nodes are added.
As I see it, the LinkedList class is not a storage implementation but a method implementation given to instances of that class in order to be able to link them to each other. Those instances each have Node class instances.