Advertisement
Kchewz

Untitled

Feb 28th, 2019
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. Pycharm error where it is running hidden in the background
  2. du -d 1
  3. rm -rf ./Pycharm.../
  4.  
  5. rm -rf .PyCharmCE2013.2/system
  6. --------------------------------------------------------------------------------------------------------------------------
  7. This lab is to give more practice on recursion and linkedlists.
  8. To start with task 1, you need the code completed from last weeks RecursiveList class so if there are any methods you didnt implement, let me know and ill give them to you.
  9.  
  10. So task 1 is implementing a linkedlist with an iterator. An iterator is the value that changes within while and for loops such as "for i in range()", i is the iterator. So the goal of task 1 is to build another linkedlist and make it as similar to for loop as possible in terms of traversing through the nodes.
  11.  
  12. Remember that for Linkedlists, each index or item isnt a specific value. Its a node that holds a value.
  13. Let me demonstrate what task 1 should look like.
  14. >>> lst = LinkedList([1, 2, 3])
  15. >>> for x in lst:
  16. ... print(x)
  17.  
  18. 1
  19. 2
  20. 3
  21. ********************************************
  22. TASK 1:------------------------------------------------
  23. So what we are building for Task 1 is essentially that 'int i' variable that starts at 0, or in the linkedlists case, the head or first node and we keep traversing down the linkedlist for however many times. In this case, our iterator 'i' is an object and thus we need to make a class for it, our LinkedListIterator. So this if we need to create
  24. def __iter__(self) -> LinkedListIterator:
  25. """Return an iterator for this linked list.
  26.  
  27. It should be straightforward to initialize the iterator here
  28. (see the class documentation below). Just remember to initialize
  29. it to the first node in this linked list.
  30. """
  31. return LinkedListIterator(self._first)
  32.  
  33.  
  34. class LinkedListIterator:
  35. _curr: Optional[_Node]
  36.  
  37. def __init__(self, first_node: Optional[_Node]) -> None:
  38. self._curr = first_node
  39.  
  40. def __next__(self) -> Any:
  41. if self._curr is None:
  42. raise StopIteration
  43.  
  44. item = self._curr.item
  45. self._curr = self._curr.next
  46. return item
  47.  
  48. TASK 2:------------------------------------------------
  49. This task is rather difficult so by all means feel free to spend time on task two finishing up last weeks insert and insert_first methods that I gave for homework.
  50.  
  51. So copy your code into the last weeks labs recursivelist class and we begin developing the method. Again we want to return a list of recursive lists so we know we need an accumulator for the end result list as well as a temporary recursive list that we can build up and append to our accumulator list.
  52.  
  53. There are two examples given to you already but I just want to give another example.
  54. Say we have a list with one element [10]. What collections should there be? [] and [10].
  55. Say we have the list with two elements [5, 10]. What collections should there be? [], [5], [10], [5, 10].
  56.  
  57. Notice a trend?
  58. 0 elements gives us 1 selection
  59. 1 element gives us 2 selections
  60. 2 elements gives us 4 selections
  61. 3 elements gives us 8 selections
  62.  
  63. See the pattern? We always expect 2^n selections where n is the number of element in our list. Make sure that every selection is not a list, but is a recursivelist(). So with 0 elements, need to build 1 recursivelist that is empty. With 1 element, need to build 2 recursivelists and so on.
  64.  
  65. def selections(self) -> List[RecursiveList]:
  66. """Return a list of all selections from this list.
  67.  
  68. You can return the selections in any order.
  69.  
  70. >>> lst1 = RecursiveList([])
  71. >>> selections1 = lst1.selections()
  72. >>> len(selections1)
  73. 1
  74. >>> selections1[0].is_empty()
  75. True
  76. >>> lst2 = RecursiveList([1, 2, 3])
  77. >>> len(lst2.selections())
  78. 8
  79. """
  80. if self.is_empty():
  81. ...
  82. else:
  83. rest_selections = self._rest.selections()
  84. ...
  85. ---------------------------------------------------------------------------------------------------------------------------
  86. Quiz 7:
  87. This question is tricky but if you remember the nested_list_equal() function from the last lab, it follows a similar idea.
  88. Apparently you have see this something similar to this in lecture but with constructs (such as any or all). Here you are to try and complete the function without either of these. You also shouldnt need any list comprehension methods either such as append and pop. HOWEVER, you can use logical operators and will need them. Those operators consist of not, or and and.
  89.  
  90. There are multiple solutions to this problem and I do recommend going on python visualizer and tracing the code. Bogdan mentions there is also a solution with two different loops so by all means compare this code with yours on a doctest and give it a go. On the exam, if you are not given doctests thus having to create your own doctests AND the code you write fulfills your doctests, you will get part marks. But to be safe, make sure to clarify with TAs.
  91.  
  92. def semi_homogeneous(obj) -> Bool:
  93. """
  94. >>> semi_homogeneous([])
  95. True
  96. >>> semi_homogeneous(1)
  97. True
  98. >>> semi_homogeneous([1, 2, 3])
  99. True
  100. >>> semi_homogeneous([1, []])
  101. False
  102. """
  103. if isinstance(obj, int):
  104. return True
  105. elif obj == []:
  106. return True
  107. else:
  108. all_ints = True
  109. all_lists = True
  110. all_semi = True
  111. # check all ints
  112. for sublist in obj:
  113. if isinstance(sublist, int):
  114. all_lists = False
  115. elif isinstance(sublist, list):
  116. all_ints = False
  117. all_semi = semi_homogeneous(sublist)
  118.  
  119. return (all_ints or all_lists) and all_semi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement