Skip to content

Commit e617845

Browse files
authored
Merge pull request #13 from camilosalazar98/master
Adding 2.1,2.2,2.3
2 parents dd40249 + fcbd0f2 commit e617845

File tree

6 files changed

+535
-7
lines changed

6 files changed

+535
-7
lines changed

Python/chapter01/1.8 - Zero Matrix/camilo_solution_1.8.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,67 @@
22
Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0,
33
its entire row and column are set to 0.
44
Hints:#17, #74, #702
5+
6+
7+
8+
I've been stuck on this for a couple of days..
9+
Really good resource:
10+
https://www.youtube.com/watch?v=ZzpJgRvqSJQ&t=113s
11+
https://www.youtube.com/watch?v=qWeNXOCff3o&t=408s
512
"""
613

714

815

916
def zeroMatrix(matrix):
10-
m = len(matrix) - 1
11-
n = len(matrix[0]) - 1
12-
for i in range(m):
13-
for j in range(n):
14-
if matrix[i][j] == 0:
15-
matrix[i+1][j] = 0
17+
rowZero = False
18+
colZero = False
19+
20+
for i in range(0,len(matrix)):
21+
if matrix[i][0] == 0:
22+
colZero = True
23+
24+
for i in range(0,len(matrix[0])):
25+
if matrix[0][i] == 0:
26+
rowZero = True
27+
28+
for i in range(1,len(matrix)):
29+
for j in range(1,len(matrix[0])):
30+
if matrix[i][j] == 0:
31+
matrix[0][j] == 0
32+
matrix[i][0] == 0
33+
34+
35+
for i in range(1,len(matrix)):
36+
if(matrix[i][0] == 0):
37+
for j in range(1,len(matrix[0])):
38+
matrix[i][j] = 0
39+
40+
for i in range(1,len(matrix[0])):
41+
if(matrix[0][i] == 0):
42+
for j in range(1,len(matrix)):
43+
matrix[i][j] = 0
44+
45+
if rowZero:
46+
for i in range(0,len(matrix[0])):
47+
matrix[0][i] = 0
1648

49+
if colZero:
50+
for i in range(0,len(matrix)):
51+
matrix[i][0] = 0
1752

18-
matrix = [[0,1,2],[3,4,5],[6,7,8]]
1953

2054

55+
56+
57+
58+
59+
60+
61+
matrix = [[0,1,1],
62+
[1,1,1],
63+
[1,1,1]]
64+
2165
zeroMatrix(matrix)
66+
67+
2268
print(matrix)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
String Rotation:Assume you have a method isSubstring which checks if one word is a
3+
substring of another. Given two strings, sl and s2, write code to check if s2 is
4+
a rotation of sl using only one call to isSubstring (e.g.,"waterbottle" is a
5+
rotation of"erbottlewat").
6+
"""
7+
8+
def isSubstring(s1,s2):
9+
if s2 in s1: #check to see if the seqence of s2 is found in s1
10+
return True
11+
return False
12+
13+
def Rotation(s1,s2):
14+
"""
15+
if they are substring of each other then we first check its lenght
16+
"""
17+
if len(s1) != len(s2): #if there not the same lenght then there different woulds
18+
return False
19+
20+
else:
21+
s1+=s1 #waterbottlewaterbottle Anyroataion can be found here
22+
return isSubstring(s1,s2)
23+
24+
s1 = "waterbottle"
25+
s2 = "erbottlewat"
26+
27+
print(Rotation(s1,s2))
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
"""Romove Dups! Write code to remove duplicates from an unsorted linked list.
2+
FOLLOW UP
3+
How would you solve this problem if a temporary
4+
buffer is not allowed? Hints: #9, #40
5+
"""
6+
7+
class Node:
8+
#Singly link list
9+
def __init__(self,data):
10+
self.data = data
11+
self.next = None
12+
13+
class linklist:
14+
#linkList class
15+
def __init__(self):
16+
self.head = None
17+
18+
def push(self,data):
19+
node = Node(data)# create a new node
20+
21+
if self.head == None: #check to see if the head node is empty
22+
self.head = node # If its empty add it to the new node
23+
return
24+
#if the head of the linklist is filled
25+
26+
current = self.head
27+
while current.next is not None:#Check the current postion is empty
28+
#Move to the next line if nothing is there
29+
current = current.next
30+
31+
current.next = node #point self.head to a new node
32+
33+
34+
35+
36+
def lenght(self):
37+
#note the count doesn't start at zero
38+
cur = self.head
39+
counter = 0
40+
while cur is not None:
41+
counter+=1
42+
cur = cur.next
43+
print('Linklist len: '+str(counter))
44+
45+
def printList(self):
46+
curr = self.head
47+
elem = []
48+
49+
while(curr != None):
50+
elem.append(curr.data)
51+
curr = curr.next
52+
print(elem)
53+
#1->2->3
54+
def remove_node(self,data):
55+
#1->2->3
56+
curr = self.head
57+
if curr is not None and curr.data == data:
58+
self.head = curr.next
59+
curr = None
60+
#look for the node that has the data we are looking for
61+
while curr is not None:
62+
if curr.data == data:
63+
break
64+
prev = curr
65+
curr = curr.next
66+
67+
#if data isn't found just reutrn
68+
if(curr == None):
69+
return
70+
71+
#allow us to unlink the nodes
72+
prev.next = curr.next
73+
curr = None
74+
75+
def remove_dups(self):
76+
d_ = set()# create a set where only unquine element can be in
77+
curr = self.head
78+
while curr is not None:
79+
d_.add(curr.data)#add all the elements to the set
80+
curr = curr.next
81+
d_ = list(d_)#make theem in to a list
82+
self.head = None#clear the orignal link list
83+
for i in range(len(d_)):
84+
self.push(d_[i])#Use the element in the set a build a linklist with no dups
85+
86+
87+
88+
89+
90+
91+
#Testing to see if my class works
92+
llist = linklist()
93+
llist.push(2)
94+
llist.push(2)
95+
llist.push(2)
96+
llist.push(2)
97+
llist.push(2)
98+
llist.printList()
99+
llist.lenght()
100+
#Testing 1->2->1->3->1 result should only be 1->2->3
101+
llist.remove_dups()
102+
llist.printList()
103+
llist.lenght()
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
Return Kth to Last: Implement an algorithm to find the kth to last element of
3+
a singly linked list.
4+
Hints:#8, #25, #41, #67, #126
5+
"""
6+
7+
class Node:
8+
#Singly link list
9+
def __init__(self,data):
10+
self.data = data
11+
self.next = None
12+
13+
class linklist:
14+
#linkList class
15+
def __init__(self):
16+
self.head = None
17+
self.size = 0
18+
19+
def push(self,data):
20+
node = Node(data)# create a new node
21+
22+
if self.head == None: #check to see if the head node is empty
23+
self.head = node # If its empty add it to the new node
24+
self.size = 1
25+
return
26+
#if the head of the linklist is filled
27+
28+
current = self.head
29+
while current.next is not None:#Check the current postion is empty
30+
#Move to the next line if nothing is there
31+
current = current.next
32+
33+
34+
current.next = node #point self.head to a new node
35+
self.size+=1
36+
37+
38+
39+
40+
def lenght(self):
41+
#note the count doesn't start at zero
42+
cur = self.head
43+
counter = 0
44+
while cur is not None:
45+
counter+=1
46+
cur = cur.next
47+
print('Linklist len: '+str(counter))
48+
return counter
49+
50+
def printList(self):
51+
curr = self.head
52+
elem = []
53+
54+
while(curr != None):
55+
elem.append(curr.data)
56+
curr = curr.next
57+
print(elem)
58+
#1->2->3
59+
def remove_node(self,data):
60+
#1->2->3
61+
curr = self.head
62+
if curr is not None and curr.data == data:
63+
self.head = curr.next
64+
curr = None
65+
#look for the node that has the data we are looking for
66+
while curr is not None:
67+
if curr.data == data:
68+
break
69+
prev = curr
70+
curr = curr.next
71+
72+
#if data isn't found just reutrn
73+
if(curr == None):
74+
return
75+
76+
#allow us to unlink the nodes
77+
prev.next = curr.next
78+
curr = None
79+
80+
def Kth_to_Last(self,num):
81+
counter = 1 # We need to keep track where we are
82+
curr = self.head
83+
goal = self.size - num# kth to last element
84+
while curr is not None:
85+
if goal == counter:
86+
return curr.data
87+
counter+=1
88+
curr = curr.next
89+
90+
91+
92+
93+
94+
95+
llist = linklist()
96+
llist.push(1)
97+
llist.push(2)
98+
llist.push(3)
99+
llist.push(4)
100+
llist.push(5)
101+
llist.printList()
102+
103+
llist.lenght()
104+
print(llist.Kth_to_Last(1))# should return 4
105+
print(llist.Kth_to_Last(2))# should return 4
106+
print(llist.Kth_to_Last(3))# should return 4

0 commit comments

Comments
 (0)