Commit 14b236eb authored by La Fée Verte's avatar La Fée Verte

fixing broken solution and adding 2 more

parent 4260de3a
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/
!_TAG_PROGRAM_NAME Exuberant Ctags //
!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/
!_TAG_PROGRAM_VERSION 5.9~svn20110310 //
LList main.py /^class LList:$/;" c
Node main.py /^class Node:$/;" c
__init__ main.py /^ def __init__(self, head):$/;" m class:LList
__init__ main.py /^ def __init__(self, val, next=None):$/;" m class:Node
main main.py /^def main():$/;" f
next main.py /^ def next(self):$/;" m class:LList
reverse main.py /^ def reverse(self):$/;" m class:LList
rewind main.py /^ def rewind(self):$/;" m class:LList
show main.py /^ def show(self):$/;" m class:LList
......@@ -7,7 +7,7 @@ some exception handling and proper error checking. """
class Node:
""" The node will be the basis of a singly linked list """
def __init__(self, val, next=None):
self.val = val;
self.val = str(val);
self.next = next
......@@ -33,7 +33,7 @@ class LList:
print("[", end = '')
comma = ''
while n:
print(comma, n.val, end = '')
print(comma, n.val if n.val[0] != '~' else n.val[1:], end = '')
comma = ','
n = n.next
print(" ]")
......@@ -54,45 +54,14 @@ class LList:
def main():
""" Main driver create lists, reverse them, find beginning overlap """
# This is just setup
mlist = LList(Node(3,Node(7,Node(8,Node(10)))))
nlist = LList(Node(99,Node(1,Node(8,Node(10)))))
cnode = Node(8,Node(10))
mlist = LList(Node(3,Node(7,cnode)))
nlist = LList(Node(99,Node(1,cnode)))
mlist.show()
nlist.show()
# list reversal in place solution
# this is in constant space but the solution is actually
# in O(m+n+l) where l is the final length of the overlapped
# string. Still in constant space since the reversal is in place
nlist.reverse()
mlist.reverse()
# constant space
# O(m+n) time
nnode = nlist.rewind()
mnode = mlist.rewind()
overlap = None
while nnode.val == mnode.val:
overlap = nnode
nnode = nlist.next()
mnode = mlist.next()
print("The overlap = ", overlap.val)
# At this point the list would have to be reversed again to actually
# get the true overlapped list, though not part of the problem it
# doubles the effort to achieve
nlist.reverse()
mlist.reverse()
print("The overlap list : ", end ='')
LList(overlap).show()
# solution using 2 "pointers" solves the O(m+n) in constant space
# aspect of the problem.
mwalker = mlist.head
......@@ -107,11 +76,56 @@ def main():
nwalker = mlist.heead
print("The overlap = ", nwalker.val);
# Getting the actual list, though not part of the problem no longer
# takes double the processing.
# Getting the actual list, though not part of the problem
# is pretty much free
print("The overlap list is:", end ='')
LList(nwalker).show();
# this solution is partially destructive in that it
# modifies the val. For this I changed it to a string
# and added a ~ to ones visited in one list. when traversing
# the other list finding the ~ shows the overlap. The show()
# 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)
mnode = mlist.rewind()
while mnode:
mnode.val = "~" + str(mnode.val)
mnode = mnode.next
nnode = nlist.rewind()
while nnode.val[0] != '~':
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:
temp = mnode.next
mnode.next = None
mnode = temp
# constant space
# O(m+n) time
nnode = nlist.rewind()
overlap = None
while nnode.next:
nnode = nlist.next()
overlap = nnode
print("The overlap = ", overlap.val[1:])
print("The overlap list : ", end ='')
LList(overlap).show()
if __name__ == "__main__":
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