We can use and array, and just implement our own API for stack-things.Īrrays are flexible and that can be great! But what if you have important data that will always and forever be last in first out? If you actually want to restrict the manner in which thing go in and out to LIFO of your list, a stack might be the way to go. So that when you run the module the usual way, it will trigger the test cases.Return self. And call that function from a main() guard: if _name_ = '_main_': You don't want the test cases to run automatically whenever the module is imported, so don't put the code at toplevel. So that it can be called when appropriate. Stick your test code in a main() function Take advantage of falsiness of None: while current != None: ![]() You always set both the item and the next, so pass those both in. Why not make a better constructor for Node? ![]() These are short, triple-quoted strings right after the def line of each function that explain the purpose of the function, and what the parameters mean. Be sure to include docstrings on all public methods.You do this most of the time, but try to be completely consistent. One space around the binary operators like = and =.PEP-8 is the standard for writing Python code. Its implementation is that of a stack - you can only add at one end, and searches start from that end. Its interface is that of a set - the only options supported are add item, and check for membership. Item = int(raw_input("Enter a number to search for: ")) Implement your test code using standard Python idioms, to prove it's working: llist = LinkedList() Implement special dundermethods (method names with "double-underscores" in them: double-under-methods, or "dundermethods") as needed to make standard Python idioms work: def _contains_(self, item): Use the standard method names where possible: clear, extend, append, remove, etc. Implement as many of the mutable sequence operations as possible. Conforming to an existing interface is a good way to do that!Ĭreate an initializer that takes a sequence. In fact, if you want other people to use your code, you should try to produce as few surprises as possible. And how the Mutable Sequence Types are expected to work.īecause your code is implementing a "mutable sequence type." So there's no reason that your code shouldn't work the same way. "How can I do this better," you ask? Well, post it on CodeReview of course! But also, consider how the Python list class (and set, and dict, and tuple, and. That's a lot of letters to get 4 numbers into a list. The next thing that struck me about your code was this: llist = Linkedlist() (Python provides a mechanism, for dealing with the case where you want to turn a member access into a function call. ![]() Instead, you can just write this: class Node:Īnd let people access node.data and node.next directly. What that means is the bizarre fetish for mutators/accessors/getters and setters that permeates Java and C# doesn't exist in Python. Python is a "consenting adult language." (Watch Raymond Hettinger's talk for more.) Item = int(raw_input("Enter the number you want to search : "))įirst, you need to realize that Python is not Java. I am given a task to ask for a number and then check if it is present within a linked list or not.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |