Commit 22583114 authored by La Fée Verte's avatar La Fée Verte

cleaning up rough code

parent 14b236eb
#!/usr/bin/python3 #!/usr/bin/python3
""" Find beginning of overlapping list """ """ Find beginning of overlapping list
""" Wrote my own linked list for this. There is no error checking and all Wrote my own linked list for this. There is no error checking and all
assumptions are optimistic. This may be a fun project to flesh out with assumptions are optimistic. This may be a fun project to flesh out with
some exception handling and proper error checking. """ some exception handling and proper error checking.
"""
class Node: class Node:
""" The node will be the basis of a singly linked list """ """ The node will be the basis of a singly linked list """
def __init__(self, val, next=None): def __init__(self, val, next_node=None):
self.val = str(val); self.val = val
self.next = next self.next = next_node
def __str__(self):
return str(self.val)
class LList: class LList:
...@@ -17,97 +21,79 @@ class LList: ...@@ -17,97 +21,79 @@ class LList:
self.head = head self.head = head
self.current = head self.current = head
def rewind(self): def __iter__(self):
""" return current pointer to the head of the list """
self.current = self.head self.current = self.head
return self.current return self
def next(self): def __next__(self):
""" advance the current pointer """ temp = self.current
if not temp:
raise StopIteration
self.current = self.current.next self.current = self.current.next
return self.current return temp
def show(self): def __str__(self):
""" ouput in list format a linked list """ return '[ ' + ' -> '.join([str(node) for node in self]) + ' ]'
n = self.head
print("[", end = '')
comma = ''
while n:
print(comma, n.val if n.val[0] != '~' else n.val[1:], end = '')
comma = ','
n = n.next
print(" ]")
def reverse(self):
""" reverse a list in place """
prev = None
n = self.head
while n:
temp = n.next
n.next = prev
prev = n
n = temp
if n:
self.head = n
def main(): def main():
""" Main driver create lists, reverse them, find beginning overlap """ """ Main driver create lists, reverse them, find beginning overlap """
# This is just setup # This is just setup
cnode = Node(8,Node(10)) cnode = Node(8, Node(10))
mlist = LList(Node(3,Node(7,cnode))) mlist = LList(Node(3, Node(7, cnode)))
nlist = LList(Node(99,Node(1,cnode))) nlist = LList(Node(99, Node(1, cnode)))
# nlist = LList(Node(99,Node(1)))
#
# cnode = Node(3, Node(7, Node(8, Node(10))))
# mlist = LList(cnode)
# nlist = LList(cnode)
mlist.show() print(mlist)
nlist.show() print(nlist)
non_destructive_find(mlist, nlist)
destructive_find(mlist, nlist)
# solution using 2 "pointers" solves the O(m+n) in constant space
# aspect of the problem. def non_destructive_find(mlist, nlist):
""""
solution using 2 "pointers" solves the O(m+n) in constant space
aspect of the problem.
"""
mwalker = mlist.head mwalker = mlist.head
nwalker = nlist.head nwalker = nlist.head
while mwalker.val != nwalker.val: count_rollovers = 0
while mwalker != nwalker:
mwalker = mwalker.next mwalker = mwalker.next
nwalker = nwalker.next nwalker = nwalker.next
if not mwalker: if not mwalker:
mwalker = nlist.head mwalker = nlist.head
count_rollovers += 1
if not nwalker: if not nwalker:
nwalker = mlist.heead nwalker = mlist.head
print("The overlap = ", nwalker.val); count_rollovers += 1
if count_rollovers > 2:
# Getting the actual list, though not part of the problem print("There was no overlap")
# is pretty much free break
print("The overlap list is:", end ='') else:
LList(nwalker).show(); print("The overlap = ", nwalker)
# this solution is partially destructive in that it # Getting the actual list, though not part of the problem
# modifies the val. For this I changed it to a string # is pretty much free
# and added a ~ to ones visited in one list. when traversing print("The overlap list is:", end='')
# the other list finding the ~ shows the overlap. The show() print(LList(nwalker))
# was modified to not show the ~. Certainly a cleanup could be
# employed to go through an remove all the ~.
# This is also O(n+m) def destructive_find(mlist, nlist):
mnode = mlist.rewind() """
while mnode: this solution is destructive as it sets
mnode.val = "~" + str(mnode.val) all the "next" pointers to None so once the
mnode = mnode.next overlap is found you are done, but you no longer
have the rest of the list
nnode = nlist.rewind() """
while nnode.val[0] != '~': mnode = mlist.head
nnode = nlist.next()
overlap = nnode
print("The overlap = ", overlap.val[1:])
print("The overlap list : ", end ='')
LList(overlap).show()
# this solution is destructive as it sets
# all the "next" pointers to None so once the
# overlap is found you are done, but you no longer
# have the rest of the list
mnode = mlist.rewind()
while mnode: while mnode:
temp = mnode.next temp = mnode.next
mnode.next = None mnode.next = None
...@@ -115,16 +101,15 @@ def main(): ...@@ -115,16 +101,15 @@ def main():
# constant space # constant space
# O(m+n) time # O(m+n) time
nnode = nlist.rewind()
overlap = None overlap = None
while nnode.next: for overlap in nlist:
nnode = nlist.next() pass
overlap = nnode
print("The overlap = ", overlap.val[1:]) print("The overlap = ", overlap.val)
print("The overlap list : ", end ='') print("The overlap list : ", end='')
LList(overlap).show() print(LList(overlap))
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment