Skip to content

Commit 9b8b22e

Browse files
committed
Move files here from Google Drive
1 parent 2b387a9 commit 9b8b22e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2880
-0
lines changed

Cracking_the_coding_interview/1.1.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''Question:
2+
implement an algorithm to determine if a string has all unique characters.
3+
What if you can not use additional data structures?
4+
'''
5+
6+
'''Algorithm Design
7+
Confusions:
8+
1. What do you mean by "all unique characters"? I assume it is ACSII string with value 0~255.
9+
2. What do you mena by "cannot use addtional data structures"? I assume I can only use string.
10+
Examples:
11+
1) agshdhfkd -> False
12+
2) agsdhfk -> True
13+
Design:
14+
Create a hash function to map all unique characters
15+
'''
16+
17+
def hash_function(char):
18+
return ord(char)
19+
20+
21+
def all_unique_string(string):
22+
table = [0 for x in range(256)]
23+
for char in string:
24+
index = hash_function(char)
25+
if table[index] == 0:
26+
table[index] = 1
27+
else:
28+
return False
29+
return True
30+
31+
if __name__ == '__main__':
32+
print all_unique_string('agshdhfkd')
33+
print all_unique_string('agsdhfk')
34+
35+
36+
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
Write code to reverse a C-Style String. (C-String means that 'abcd' is represented as five characters, including the null character.)
3+
4+
5+
In C and C++, strings are typically represented as char arrays that have a null terminator. A null terminator means that the string ends with a '\0' character (which has ASCII code 0)
6+
7+
'''
8+
9+
10+
# clarification questions:
11+
# 1) input is a string ending with '\0'
12+
# 2) i cannot use .reverer(), right?
13+
import copy
14+
def strReverse(string):
15+
string = copy.deepcopy(string)
16+
string = string[:-1]
17+
lst = list(string)
18+
lst.reverse() # the same as: reverse(lst)
19+
lst.append('\0')
20+
return str(lst)
21+
22+
# the same as list's reverse() function
23+
def reverse(lst):
24+
# swap from the beginning to the middle point of the array
25+
for i in range(len(lst)/2):
26+
tmp = lst[i]
27+
right = len(lst) - 1 - i
28+
lst[i] = lst[right]
29+
lst[right] = tmp
30+
31+
return None
32+
33+
if __name__ == '__main__':
34+
print strReverse('abocd\0')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'''
2+
Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.
3+
FOLLOW UP
4+
Write the test cases for this method.
5+
'''
6+
7+
# clarification question:
8+
# 1) does order matter? If not simple use ''.join(set(string) will work (yes, it matters)
9+
# 2) for duplicates, we keep its first occurance. is that correct? (yes)
10+
# 3) what do you mean by 'An extra copy of the array'?
11+
# i plan to use a dictionary to store unique chars, is this allowed?
12+
# (yes)
13+
# 4) can I use python's set? (yes)
14+
# 5) can I use python's collections library? (yes)
15+
16+
# solution 1: use Set
17+
def remove_duplicates_1(string):
18+
uniques = list(set(string))
19+
lst = list(string)
20+
for i in range(len(lst)):
21+
char = lst[i]
22+
if char in uniques:
23+
uniques.remove(char)
24+
else:
25+
lst[i] = ''
26+
return ''.join(lst)
27+
28+
# solution 2: use OrderedDict
29+
from collections import OrderedDict
30+
def remove_duplicates_2(string):
31+
return ''.join(OrderedDict.fromkeys(string))
32+
33+
34+
# solution 3: scan from the beginning, remove all duplicates of index - O(n^2)
35+
def remove_duplicates_3(string):
36+
lst = list(string)
37+
for i in range(len(lst)-1):
38+
char = lst[i]
39+
for j in range(i+1,len(lst)):
40+
if lst[j] == char:
41+
lst[j] = ''
42+
return ''.join(lst)
43+
44+
# solution 4: if order does not matter, use Set
45+
def remove_duplicates_4(string):
46+
return ''.join(set(string))
47+
48+
49+
# solution 5: if order does not matter (cannot use Set):
50+
# 1) merge sort O(nlogn)
51+
# 2) remove duplicates O(n)
52+
def remove_duplicates_5(string):
53+
lst = list(string)
54+
lst.sort()
55+
cur = None
56+
for i in range(len(lst)):
57+
if lst[i] != cur:
58+
cur = lst[i]
59+
else:
60+
lst[i] = ''
61+
return ''.join(lst)
62+
63+
64+
if __name__ == '__main__':
65+
print remove_duplicates_5('abdcda')
66+
print remove_duplicates_5('ab')
67+
print remove_duplicates_5('aa')
68+
print remove_duplicates_5('a')
69+
print remove_duplicates_5('')
70+
71+
72+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
19.1
3+
Write a function to swap two numbers in place without temporary variables.
4+
5+
'''
6+
def foo(a,b):
7+
a +=b
8+
b = a-b
9+
a -= b
10+
return a,b
11+
12+
13+
def foo2(a,b):
14+
a = a^b
15+
b = a^b
16+
a = a^b
17+
return a,b
18+
19+
if __name__ == '__main__':
20+
print foo(10,19)
21+
print foo2(10,19)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
'''
2+
Write code to remove duplicates from an unsorted linked list. FOLLOW UP
3+
How would you solve this problem if a temporary buffer is not allowed?
4+
'''
5+
6+
# clarification questions:
7+
# 1) what's the return value (assume no return value)
8+
# 2) the input is the head node.
9+
10+
class Node:
11+
def __init__(self,data,next):
12+
self.data = data
13+
self.next = next
14+
15+
# solution 1: if can use a buffer, store key in a dictionary
16+
def removeDuplicate_hash(node):
17+
if node == None:
18+
return
19+
20+
hash_table = [node.data]
21+
22+
23+
pre = node
24+
cur = node
25+
while cur.next != None:
26+
cur = cur.next
27+
if cur.data in hash_table:
28+
pre.next = cur.next
29+
cur.next = None
30+
cur = pre
31+
else:
32+
hash_table.append(cur.data)
33+
pre = cur
34+
35+
36+
37+
38+
# solution 2: cannot use a buffer, scan from the head, remove duplicates after
39+
def removeDuplicate(node):
40+
if node == None:
41+
return
42+
43+
val = node.data
44+
45+
cur = node
46+
pre = node
47+
while cur.next != None:
48+
cur = cur.next
49+
if cur.data == val:
50+
pre.next = cur.next
51+
cur.next = None
52+
cur = pre
53+
else:
54+
pre = cur
55+
56+
if node.next != None:
57+
removeDuplicate(node.next)
58+
59+
if __name__ == '__main__':
60+
print '\nTest case 1: 4 -> 8 -> 2-> 4'
61+
node4 = Node(4,None)
62+
node3 = Node(2,node4)
63+
node2 = Node(8,node3)
64+
node1 = Node(4,node2)
65+
removeDuplicate(node1)
66+
while True:
67+
print node1.data
68+
if node1.next==None:break
69+
node1 = node1.next
70+
71+
print '\nTest case 2: 4 -> 4'
72+
node2 = Node(4,None)
73+
node1 = Node(4,node2)
74+
removeDuplicate(node1)
75+
while True:
76+
print node1.data
77+
if node1.next==None:break
78+
node1 = node1.next
79+
80+
print '\nTest case 3: 4'
81+
node1 = Node(4,None)
82+
removeDuplicate(node1)
83+
while True:
84+
print node1.data
85+
if node1.next==None:break
86+
node1 = node1.next
87+
88+
print '\nTest case 4: None'
89+
node1 = None
90+
print removeDuplicate(node1)
91+
92+
print '\nTest case 5: 4 -> 2 -> 2-> 4'
93+
node4 = Node(4,None)
94+
node3 = Node(2,node4)
95+
node2 = Node(2,node3)
96+
node1 = Node(4,node2)
97+
removeDuplicate(node1)
98+
while True:
99+
print node1.data
100+
if node1.next==None:break
101+
node1 = node1.next
102+
103+
104+
print '\nTest case 6: 4 -> 2 -> 3-> 4 -> 8 -> 6 -> 6 -> 2'
105+
node8 = Node(2,None)
106+
node7 = Node(6,node8)
107+
node6 = Node(6,node7)
108+
node5 = Node(8,node6)
109+
node4 = Node(4,node5)
110+
node3 = Node(3,node4)
111+
node2 = Node(2,node3)
112+
node1 = Node(4,node2)
113+
removeDuplicate_hash(node1)
114+
while True:
115+
print node1.data
116+
if node1.next==None:break
117+
node1 = node1.next
118+
119+
120+
121+
122+
123+
124+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/python
2+
'''
3+
Implement an algorithm to fnd the nth to last element of a singly linked list.
4+
5+
assume n is valid (will not exceed the length of the list)
6+
'''
7+
8+
# clarification questions:
9+
# 1) what's the return value (assume no return value)
10+
# 2) the input is the head node.
11+
12+
class Node:
13+
def __init__(self,data,next):
14+
self.data = data
15+
self.next = next
16+
17+
# 1,2,3,4,5,6,7
18+
# n=3, goal = 5
19+
# 1,4
20+
# 5, None
21+
def foo(node,n):
22+
pt1 = node
23+
pt2 = node
24+
for i in range(n):
25+
pt2 = pt2.next
26+
while pt2 != None:
27+
pt1 = pt1.next
28+
pt2 = pt2.next
29+
return pt1
30+
31+
32+
if __name__ == '__main__':
33+
34+
print '\nTest case 6: 4 -> 2 -> 3-> 4 -> 8 -> 6 -> 6 -> 2'
35+
node8 = Node(2,None)
36+
node7 = Node(6,node8)
37+
node6 = Node(6,node7)
38+
node5 = Node(8,node6)
39+
node4 = Node(4,node5)
40+
node3 = Node(3,node4)
41+
node2 = Node(2,node3)
42+
node1 = Node(4,node2)
43+
print foo(node1,3).data
44+
print foo(node1,1).data
45+
print foo(node1,8).data
46+
'''while True:
47+
print node1.data
48+
if node1.next==None:break
49+
node1 = node1.next '''
50+
51+
52+
53+
54+
55+
56+

0 commit comments

Comments
 (0)