Skip to content

Commit

Permalink
[Python]Challenge_8(Pending) (YearOfProgramming#307)
Browse files Browse the repository at this point in the history
[Python]Challenge_8(ReadyForMerge)
  • Loading branch information
MJUIUC committed Jan 9, 2017
1 parent 8dc5e4c commit 9b1bb49
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 10 deletions.
2 changes: 1 addition & 1 deletion challenge_6/python/mjuiuc/Solution.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Writing solution for this range problem

class solution(object):
def compRange(self,nums):
def compRange(nums):
# nums is of type list()
# nums contains only integers in ascending order
# this function should return the list of collapsed ranges
Expand Down
13 changes: 5 additions & 8 deletions challenge_6/python/mjuiuc/test.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion challenge_7/python/mjuiuc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

I think I got it figured out. All I had to do was take the sum of the length of the list and then the sum of all the numbers in the list, find the difference betweend those results, and then return the difference. The function meets runtime and space requirements.

I included some test cases with my solution.
I included some test cases in my solution.

6 changes: 6 additions & 0 deletions challenge_8/python/mjuiuc/README.md
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.
5 changes: 5 additions & 0 deletions challenge_8/python/mjuiuc/listNode.py
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
27 changes: 27 additions & 0 deletions challenge_8/python/mjuiuc/solution.py
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
93 changes: 93 additions & 0 deletions challenge_8/python/mjuiuc/test.py
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()

0 comments on commit 9b1bb49

Please sign in to comment.