@mtf/

linked list

Python

testing user code https://discuss.codecademy.com/t/creating-and-printing-single-linked-list/432402

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#https://www.codecademy.com/courses/linear-data-structures/lessons/learn-linked-lists-python/exercises/linked-lists-python-review

class Node:
  def __init__(self, value, next_node=None):
    self.value = value
    self.next_node = next_node
    
  def get_value(self):
    return self.value
  
  def get_next_node(self):
    return self.next_node
  
  def set_next_node(self, next_node):
    self.next_node = next_node

class LinkedList:
  def __init__(self, value=None):
    self.head_node = Node(value)
  
  def get_head_node(self):
    return self.head_node
  
  def insert_beginning(self, new_value):
    new_node = Node(new_value)
    new_node.set_next_node(self.head_node)
    self.head_node = new_node
    
  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
  
  def remove_node(self, value_to_remove):
    current_node = self.get_head_node()
    if current_node.get_value() == value_to_remove:
      self.head_node = current_node.get_next_node()
    else:
      while current_node:
        next_node = current_node.get_next_node()
        if next_node.get_value() == value_to_remove:
          current_node.set_next_node(next_node.get_next_node())
          current_node = None
        else:
          current_node = next_node

  # https://discuss.codecademy.com/t/faq-linked-lists-python-linked-list-review/373552/8
  def remove_all(self, value_to_remove):
    # First while loop: check if head node is to be removed.
    # If so, remove, and reset head node to next node.
    # Iterate until (self.head_node.get_value() != value_to_remove)
    head = self.get_head_node()
    while head:
      if head.get_value() == value_to_remove:
        self.head_node = head.get_next_node()
        head = self.head_node
      else:
        head = None
    # Now the computer knows the head node is not to be removed.
    # Second while loop: iterate through the nodes in the list.
    # If there is no next node:
    #   Return 'None' to close the loop.
    # Else, if the next node has the value to be removed: 
    #   Set the next node of the current node to be the NEXT
    #   next node rather than the next node. (Remove next node.)
    # Else:
    #   Move to the next node in the linked list and repeat.
    current_node = self.get_head_node()
    while current_node:
      next_node = current_node.get_next_node()
      if next_node == None:
        current_node = next_node
      elif next_node.get_value() == value_to_remove:
        current_node.set_next_node(next_node.get_next_node())
      else:
        current_node = next_node
  


ll = LinkedList(5)
ll.insert_beginning(90)
ll.insert_beginning(70)
ll.insert_beginning(70)
ll.insert_beginning(5675)
ll.insert_beginning(90)
ll.insert_beginning(5)
ll.insert_beginning(90)
ll.insert_beginning(90)

print(ll.stringify_list())

ll.remove_all(90)

# Print output after ll.remove_all(90)
print(ll.stringify_list())
'''
# original in topic

class Node:
    def __init__(self, value, next_node=None):
        self.value = value
        self.next_node = next_node

    def get_value(self):
        return self.value

    def get_next_node(self):
        #return self.get_next_node
        return self.next_node

    def set_next_node(self, next_node):
        self.next_node = next_node

class LinkedList:
    def __init__(self, value=None):
        self.head_node = Node(value)

    def get_head_node(self):
        #print (isinstance(self.head_node, Node))  # True
        return self.head_node

    def insert_beginning(self, new_value):
        new_node = Node(new_value)
        new_node.set_next_node(self.head_node)
        self.head_node = new_node

    def print_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"
                string_list += "{}\n".format(str(current_node.get_value()))
            current_node = current_node.get_next_node()
        return string_list

test = LinkedList()
test.insert_beginning(20)
test.insert_beginning(40)
print(test.print_list())

'''
# Code by @ionatan on Codecademy.com

from multiset import Multiset

# pip install (--user if global) multiset


def addends(nums, target):
    first = {what: where for where, what in enumerate(nums)}
    last = {what: where for where, what in list(enumerate(nums))[::-1]}
    want = Multiset(target - x for x in nums)
    have = Multiset(nums)
    match = want & have
    if len(match) > 1:
        solution = next(iter(match))
        complement = target - solution
        return [first[solution], last[complement]]
    return []


print(addends([5, 5], 10))