forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Python]Challenge_8(Pending) (YearOfProgramming#307)
[Python]Challenge_8(ReadyForMerge)
- Loading branch information
Showing
7 changed files
with
139 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#Challenge 8 Solution in Python | ||
|
||
Well, the problem itself wasn't too bad I don't think. Just deep copying a list right? The harder part was copying over the random pointers which is something I thought might be easy until I actually started doing it. I sort of cheated and used a dictionary to map all of the pointers of the old list to the pointers in the copied list. I ran through the original list once and copied all the nodes to a new list, then I went through it again to assign all of the random pointers... I don't know how I would've done this without pythons dictionary. I'm sure there's a nice way to do it, but I couldn't figure it out. Oh well :/ | ||
|
||
#Testing | ||
I created a unit test whith three seperate test cases. To use them, simply copy my folder and re write the code under the solution function in the solution.py file. To run the test cases, run the test.py file. The test basically creates a new linked list. Each test case has a list of different lengths as you can see. The linked list that it creates assigns random pointers using pythons random package. It also spits out a runtime of the function for each case. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class listNode: | ||
def __init__(self,val=None,next=None,random=None): | ||
self.val = val | ||
self.next = next | ||
self.random = random |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from listNode import listNode | ||
|
||
|
||
def solution(head): | ||
# by keeping track of the pointers rather than their values, we can insure that | ||
# there a duplicates in the linked list. | ||
lop = dict() | ||
newHead = listNode() | ||
temp = head | ||
temp2 = newHead | ||
# copy the list first | ||
while temp.next != None: | ||
temp2.val = temp.val | ||
lop[temp] = temp2 | ||
temp2.next = listNode() | ||
temp2 = temp2.next | ||
temp = temp.next | ||
temp2.next = None | ||
# assign the random pointers | ||
temp3 = head | ||
temp4 = newHead | ||
while temp3.next != None: | ||
temp4.random = lop[temp3.random] | ||
temp3 = temp3.next | ||
temp4 = temp4.next | ||
|
||
return newHead |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import unittest | ||
import random | ||
import time | ||
from listNode import listNode | ||
from solution import solution | ||
|
||
class Tests(unittest.TestCase): | ||
def test1(self): | ||
# create linked list | ||
temp = listNode() | ||
head = temp | ||
lop = list() | ||
for i in xrange(1,11): | ||
lop.append(temp) | ||
temp.val = i | ||
temp.next = listNode() | ||
temp = temp.next | ||
# assign random pointers | ||
temp2 = head | ||
while temp2 != None: | ||
temp2.random = lop[random.randint(0,9)] | ||
temp2 = temp2.next | ||
# get the copied list head | ||
t1 = time.time() | ||
copiedHead = solution(head) | ||
t2 = time.time() | ||
# can't figure out a nice way to check the random pointers.. | ||
for i in lop: | ||
# the actual test happens here | ||
assert copiedHead.val == head.val, "error in value" | ||
assert copiedHead.random.val == head.random.val, "error in random value" | ||
copiedHead = copiedHead.next | ||
head = head.next | ||
print 'Runtime of test1: ' + str(t2 - t1) | ||
|
||
def test2(self): | ||
# create linked list | ||
temp = listNode() | ||
head = temp | ||
lop = list() | ||
for i in xrange(1,10001): | ||
lop.append(temp) | ||
temp.val = i | ||
temp.next = listNode() | ||
temp = temp.next | ||
# assign random pointers | ||
temp2 = head | ||
while temp2 != None: | ||
temp2.random = lop[random.randint(0,9999)] | ||
temp2 = temp2.next | ||
# get the copied list head | ||
t1 = time.time() | ||
copiedHead = solution(head) | ||
t2 = time.time() | ||
# can't figure out a nice way to check the random pointers.. | ||
for i in lop: | ||
# the actual test happens here | ||
assert copiedHead.val == head.val, "error in value" | ||
assert copiedHead.random.val == head.random.val, "error in random value" | ||
copiedHead = copiedHead.next | ||
head = head.next | ||
print 'Runtime of test2: ' + str(t2 - t1) | ||
|
||
def test3(self): | ||
# create linked list | ||
temp = listNode() | ||
head = temp | ||
lop = list() | ||
for i in xrange(1,100001): | ||
lop.append(temp) | ||
temp.val = i | ||
temp.next = listNode() | ||
temp = temp.next | ||
# assign random pointers | ||
temp2 = head | ||
while temp2 != None: | ||
temp2.random = lop[random.randint(0,99999)] | ||
temp2 = temp2.next | ||
# get the copied list head | ||
t1 = time.time() | ||
copiedHead = solution(head) | ||
t2 = time.time() | ||
# can't figure out a nice way to check the random pointers.. | ||
for i in lop: | ||
# the actual test happens here | ||
assert copiedHead.val == head.val, "error in value" | ||
assert copiedHead.random.val == head.random.val, "error in random value" | ||
copiedHead = copiedHead.next | ||
head = head.next | ||
print 'Runtime of test3: ' + str(t2 - t1) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |