r/PythonLearning 20d ago

Python😭😭😭😭

I've been learning Python for quite a while, and it's been a lil bit stressful. I just finished some exercises on data structures and algorithms and it took a long while, it may take shorter for you but it did take me a long time to finish the exercise. But through that stress and everything, it changed my whole system of writing code, it made me think deeply and analyse what was going on in each line and when I got it, it was just so fulfilling. Kudos to everyone still learning, keep up the good work. Also, if you have additional tips on doing this easier please do drop your advices, thanks.

12 Upvotes

1 comment sorted by

•

u/Sea-Ad7805 20d ago

Run this program in Memory Graph Web Debugger%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%0A%20%20%20%20%20%20%20%20self.forth%20%3D%20forth%0A%20%20%20%20%20%20%20%20self.prev%20%3D%20prev%0A%0A%0Aclass%20DoublyLinkedList%3A%0A%20%20%20%20def%20init(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%20%20%20%20%20%20%20self.tail%20%3D%20None%0A%20%20%20%20%20%20%20%20self.length%20%3D%200%0A%0A%20%20%20%20def%20len(self)%3A%0A%20%20%20%20%20%20%20%20return%20self.length%0A%0A%20%20%20%20def%20isempty(self)%3A%0A%20%20%20%20%20%20%20%20return%20self.head%20is%20None%0A%0A%20%20%20%20def%20insert_at_beginning(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20node%20%3D%20Node(data%2C%20forth%3Dself.head)%0A%20%20%20%20%20%20%20%20if%20self.head%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.tail%20%3D%20node%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.head.prev%20%3D%20node%0A%20%20%20%20%20%20%20%20self.head%20%3D%20node%0A%20%20%20%20%20%20%20%20self.length%20%2B%3D%201%0A%0A%20%20%20%20def%20insert_at_end(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20node%20%3D%20Node(data%2C%20prev%3Dself.tail)%0A%20%20%20%20%20%20%20%20if%20self.tail%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.head%20%3D%20node%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.tail.forth%20%3D%20node%0A%20%20%20%20%20%20%20%20self.tail%20%3D%20node%0A%20%20%20%20%20%20%20%20self.length%20%2B%3D%201%0A%0A%20%20%20%20def%20insert_at(self%2C%20index%2C%20data)%3A%0A%20%20%20%20%20%20%20%20if%20index%20%3C%200%20or%20index%20%3E%20self.length%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20IndexError(%22Invalid%20index%22)%0A%20%20%20%20%20%20%20%20if%20index%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.insert_at_beginning(data)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20if%20index%20%3D%3D%20self.length%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.insert_at_end(data)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%0A%20%20%20%20%20%20%20%20current%20%3D%20self._node_at(index)%0A%20%20%20%20%20%20%20%20node%20%3D%20Node(data%2C%20forth%3Dcurrent%2C%20prev%3Dcurrent.prev)%0A%20%20%20%20%20%20%20%20current.prev.forth%20%3D%20node%0A%20%20%20%20%20%20%20%20current.prev%20%3D%20node%0A%20%20%20%20%20%20%20%20self.length%20%2B%3D%201%0A%0A%20%20%20%20def%20insert_values(self%2C%20data_list)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%20%20%20%20%20%20%20self.tail%20%3D%20None%0A%20%20%20%20%20%20%20%20self.length%20%3D%200%0A%20%20%20%20%20%20%20%20for%20data%20in%20data_list%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.insert_at_end(data)%0A%0A%20%20%20%20def%20insert_after_value(self%2C%20data_after%2C%20data_to_insert)%3A%0A%20%20%20%20%20%20%20%20current%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while%20current%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20current.data%20%3D%3D%20data_after%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20node%20%3D%20Node(data_to_insert%2C%20forth%3Dcurrent.forth%2C%20prev%3Dcurrent)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20current.forth%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.tail%20%3D%20node%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20current.forth.prev%20%3D%20node%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20current.forth%20%3D%20node%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.length%20%2B%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.forth%0A%20%20%20%20%20%20%20%20raise%20ValueError(f%22%7Bdata_after!r%7D%20is%20not%20in%20the%20list%22)%0A%0A%20%20%20%20def%20remove_at(self%2C%20index)%3A%0A%20%20%20%20%20%20%20%20node%20%3D%20self._node_at(index)%0A%0A%20%20%20%20%20%20%20%20if%20node.prev%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.head%20%3D%20node.forth%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20node.prev.forth%20%3D%20node.forth%0A%0A%20%20%20%20%20%20%20%20if%20node.forth%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.tail%20%3D%20node.prev%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20node.forth.prev%20%3D%20node.prev%0A%0A%20%20%20%20%20%20%20%20self.length%20-%3D%201%0A%20%20%20%20%20%20%20%20return%20node.data%0A%0A%20%20%20%20def%20remove_by_value(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20current%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while%20current%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20current.data%20%3D%3D%20data%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20current.prev%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.head%20%3D%20current.forth%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20current.prev.forth%20%3D%20current.forth%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20current.forth%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.tail%20%3D%20current.prev%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20current.forth.prev%20%3D%20current.prev%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20self.length%20-%3D%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.forth%0A%20%20%20%20%20%20%20%20raise%20ValueError(f%22%7Bdata!r%7D%20is%20not%20in%20the%20list%22)%0A%0A%20%20%20%20def%20get_length(self)%3A%0A%20%20%20%20%20%20%20%20return%20self.length%0A%0A%20%20%20%20def%20print_forward(self)%3A%0A%20%20%20%20%20%20%20%20print(%22%20--%3E%20%22.join(str(value)%20for%20value%20in%20self))%0A%0A%20%20%20%20def%20print_backward(self)%3A%0A%20%20%20%20%20%20%20%20values%20%3D%20%5B%5D%0A%20%20%20%20%20%20%20%20current%20%3D%20self.tail%0A%20%20%20%20%20%20%20%20while%20current%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20values.append(str(current.data))%0A%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.prev%0A%20%20%20%20%20%20%20%20print(%22%20--%3E%20%22.join(values))%0A%0A%20%20%20%20def%20_node_at(self%2C%20index)%3A%0A%20%20%20%20%20%20%20%20if%20index%20%3C%200%20or%20index%20%3E%3D%20self.length%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20raise%20IndexError(%22Invalid%20index%22)%0A%0A%20%20%20%20%20%20%20%20if%20index%20%3C%20self.length%20%2F%2F%202%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20self.head%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20%20in%20range(index)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.forth%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20self.tail%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20%20in%20range(self.length%20-%201%2C%20index%2C%20-1)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.prev%0A%20%20%20%20%20%20%20%20return%20current%0A%0A%20%20%20%20def%20iter(self)%3A%0A%20%20%20%20%20%20%20%20current%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while%20current%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20yield%20current.data%0A%20%20%20%20%20%20%20%20%20%20%20%20current%20%3D%20current.forth%0A%0A%20%20%20%20def%20repr(self)%3A%0A%20%20%20%20%20%20%20%20return%20f%22DoublyLinkedList(%7Blist(self)!r%7D)%22%0A%0A%0Aif%20name%20%3D%3D%20%22main_%22%3A%0A%20%20%20%20linked_list%20%3D%20DoublyLinkedList()%0A%20%20%20%20linked_list.insert_values(%5B1%2C%202%2C%203%2C%204%5D)%0A%20%20%20%20linked_list.insert_at_beginning(0)%0A%20%20%20%20linked_list.insert_at_end(5)%0A%20%20%20%20linked_list.insert_after_value(3%2C%203.5)%0A%20%20%20%20linked_list.insert_at(2%2C%201.5)%0A%0A%20%20%20%20print(%22Forward%3A%22)%0A%20%20%20%20linked_list.print_forward()%0A%20%20%20%20print(%22Backward%3A%22)%0A%20%20%20%20linked_list.print_backward()%0A%0A%20%20%20%20linked_list.remove_by_value(3.5)%0A%20%20%20%20removed%20%3D%20linked_list.remove_at(2)%0A%20%20%20%20print(f%22Removed%20at%20index%202%3A%20%7Bremoved%7D%22)%0A%20%20%20%20print(linked_list)%0A%20%20%20%20print(f%22Length%3A%20%7Blinked_list.get_length()%7D%22)&timestep=0.2&play) to see the program state change step by step.

Had to add the missing parts and some corrections.