Skip to content

Commit a6a9c89

Browse files
authored
Merge pull request #30 from camilosalazar98/master
updates
2 parents 171cd81 + f2015a3 commit a6a9c89

File tree

8 files changed

+673
-1
lines changed

8 files changed

+673
-1
lines changed

Python/chapter01/1.3 - URLify/camilo_solution_1.3.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@
55
(Note: If implementing in Java,please use a character array so that you can
66
perform this operation in place.)
77
EXAMPLE
8-
Input: "Mr John Smith ", 13 Output: "Mr%20John%20Smith"
8+
Input: "Mr John Smith ", 13 Output: "Mr%20John%20Smith"
99
"""
1010

1111
def urlify(str):
12+
len_ = len(str) #gets the lenght of str
13+
new_str = ""#build a new string
14+
for i in range(len_-1):
15+
if str[i] == " " and str[i+1]==" ":#if two space aheah are space then break
16+
break
17+
if str[i] == " ": #if we find a space just add %20
18+
new_str+= "%20"
19+
20+
else: #just add the chars
21+
new_str+= str[i]
22+
23+
return new_str
24+
25+
print(urlify("MY house is on fire "))
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
"""
2+
Sum Lists: You have two numbers represented by a linked list, where each node
3+
contains a single digit.
4+
The digits are stored in reverse order, such that the 1 's digit is at the head
5+
of the list. Write a function that adds the two numbers and returns the sum as
6+
a linked list.
7+
8+
EXAMPLE
9+
Input:(7-> 1 -> 6) + (5 -> 9 -> 2).That is,617 + 295. Output:2 -> 1 -> 9.That is,912.
10+
FOLLOW UP
11+
Suppose the digits are stored in forward order. Repeat the above problem. EXAMPLE
12+
lnput:(6 -> 1 -> 7) + (2 -> 9 -> 5).That is, 617 + 295. Output:9 -> 1 -> 2.Thatis,912.
13+
Hints: #7, #30, #71, #95, #109
14+
15+
16+
USE THIS IF YOUR SUPERDUPER STUCK
17+
https://www.youtube.com/watch?v=dm9drjU-DuM
18+
19+
"""
20+
21+
22+
23+
class Node:
24+
#Singly link list
25+
def __init__(self,data = None):
26+
self.data = data
27+
self.next = None
28+
29+
class linklist:
30+
#linkList class
31+
def __init__(self):
32+
self.head = None
33+
self.size = 0
34+
35+
def push(self,data):
36+
node = Node(data)# create a new node
37+
38+
if self.head == None: #check to see if the head node is empty
39+
self.head = node # If its empty add it to the new node
40+
self.size = 1
41+
return
42+
#if the head of the linklist is filled
43+
44+
current = self.head
45+
while current.next is not None:#Check the current postion is empty
46+
#Move to the next line if nothing is there
47+
current = current.next
48+
49+
50+
current.next = node #point self.head to a new node
51+
self.size+=1
52+
53+
def lenght(self):
54+
#note the count doesn't start at zero
55+
cur = self.head
56+
counter = 0
57+
while cur is not None:
58+
counter+=1
59+
cur = cur.next
60+
print('Linklist len: '+str(counter))
61+
return counter
62+
63+
def printList(self):
64+
curr = self.head
65+
elem = []
66+
67+
while(curr != None):
68+
elem.append(curr.data)
69+
curr = curr.next
70+
print(elem)
71+
#1->2->3
72+
def remove_node(self,data):
73+
#1->2->3
74+
curr = self.head
75+
if curr is not None and curr.data == data:
76+
self.head = curr.next
77+
curr = None
78+
#look for the node that has the data we are looking for
79+
while curr is not None:
80+
if curr.data == data:
81+
break
82+
prev = curr
83+
curr = curr.next
84+
85+
#if data isn't found just reutrn
86+
if(curr == None):
87+
return
88+
89+
#allow us to unlink the nodes
90+
prev.next = curr.next
91+
curr = None
92+
93+
94+
def sum_forward_backwards(self,llist_B):
95+
if llist_B.head == None:
96+
return "llist_B is None"
97+
98+
sum_list = linklist()
99+
head_b = llist_B.head
100+
curr = self.head #List A
101+
carry = 0
102+
while curr or head_b:
103+
if curr == None and head_b == None:
104+
i,j = 0
105+
else:
106+
i = curr.data
107+
j = head_b.data
108+
109+
sum_ = i + j + carry
110+
if sum_ >= 10:
111+
112+
carry = 1
113+
r = sum_ % 10
114+
sum_list.push(r)
115+
116+
else:
117+
carry = 0
118+
sum_list.push(sum_)
119+
if curr:
120+
curr = curr.next
121+
if head_b:
122+
head_b = head_b.next
123+
124+
self.head = sum_list.head
125+
126+
127+
128+
129+
130+
link_A = linklist()
131+
link_B = linklist()
132+
link_A.push(7)
133+
link_A.push(1)
134+
link_A.push(6)
135+
136+
link_B.push(5)
137+
link_B.push(9)
138+
link_B.push(2)
139+
link_A.sum_forward_backwards(link_B)
140+
link_A.printList()
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""
2+
Palindrome: Implement a function to check if a linked list is a
3+
palindrome. Hints:#5, #13, #29, #61, #101
4+
"""
5+
class Node:
6+
#Singly link list
7+
def __init__(self,data = None):
8+
self.data = data
9+
self.next = None
10+
11+
class linklist:
12+
#linkList class
13+
def __init__(self):
14+
self.head = None
15+
self.size = 0
16+
17+
def push(self,data):
18+
node = Node(data)# create a new node
19+
20+
if self.head == None: #check to see if the head node is empty
21+
self.head = node # If its empty add it to the new node
22+
self.size = 1
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+
32+
current.next = node #point self.head to a new node
33+
self.size+=1
34+
35+
def lenght(self):
36+
#note the count doesn't start at zero
37+
cur = self.head
38+
counter = 0
39+
while cur is not None:
40+
counter+=1
41+
cur = cur.next
42+
print('Linklist len: '+str(counter))
43+
return 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 palindromeCheck(self):
76+
curr = self.head
77+
str1 = ""
78+
str2 = ""
79+
while curr is not None:
80+
str1 += curr.data
81+
curr = curr.next
82+
83+
for i in range(len(str1)):
84+
str2+=str1[len(str1) - 1 - i]
85+
86+
87+
if str2 == str1:
88+
return True
89+
else:
90+
return False
91+
92+
93+
94+
#anna
95+
llist = linklist()
96+
llist.push('a')
97+
llist.push('n')
98+
llist.push('n')
99+
llist.push('a')
100+
print(llist.palindromeCheck())

0 commit comments

Comments
 (0)