Skip to content

Commit 17c983f

Browse files
authored
Merge pull request #7 from camilosalazar98/master
Updates to some solutions
2 parents 7aeec86 + 96a73d4 commit 17c983f

File tree

9 files changed

+211
-2
lines changed

9 files changed

+211
-2
lines changed

Python/chapter01/1.1 - Is Unique/camilo_sol.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# Implement an algorithm to determine if a string has all unique charactersself.
2-
# What if you cannot use additional data structures.
1+
"""
2+
Is Unique: Implement an algorithm to determine if a string has all unique
3+
characters. What if you cannot use additional data structures?
4+
"""
35

46

57
def isUnique(string_):

Python/chapter01/1.2 - Check Perm/camilo_sol_1.2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
"""
2+
Check Permutation: Given two strings,write a method to decide if
3+
one is a permutation of the other.
4+
"""
5+
6+
7+
8+
19
def perm_check(str_1,str_2):
210
if len(str_1) != len(str_2):#If both str are diiferent lenghts return false
311
return False
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
URLify: Write a method to replace all spaces in a string with '%20'. You may
3+
assume that the string has sufficient space at the end to hold the additional
4+
characters,and that you are given the "true" length of the string.
5+
(Note: If implementing in Java,please use a character array so that you can
6+
perform this operation in place.)
7+
EXAMPLE
8+
Input: "Mr John Smith ", 13 Output: "Mr%20John%20Smith"
9+
"""
10+
11+
def urlify(str):

Python/chapter01/1.3 - URLify/solution.py

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Palindrome Permutation: Given a string, write a function to check if it is a
3+
permutation of a palin­ drome. A palindrome is a word or phrase that is the same
4+
forwards and backwards. A permutation is a rearrangement of letters.
5+
The palindrome does not need to be limited to just dictionary words.
6+
EXAMPLE
7+
Input: Tact Coa
8+
Output: True (permutations: "taco cat", "atco eta", etc.)
9+
"""
10+
11+
def PalinPerm(str):
12+
d = {}
13+
str = str.replace(' ','').lower()#Take out the spaces
14+
for i in str:
15+
if i in d:
16+
d[i] += 1
17+
else:
18+
d[i] = 1
19+
odd_count = 0 # pop ppo opp
20+
for keys,value in d.items():
21+
if value % 2 != 0 and odd_count == 0:# Remeber there should only be 1 where key equals when val%2
22+
odd_count += 1
23+
elif value % 2 != 0 and odd_count != 0:
24+
return False
25+
return True
26+
27+
print(PalinPerm('taco cat'))#True
28+
print(PalinPerm('taco catt'))#False
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
One Away: There are three types of edits that can be performed on strings:
3+
insert a character, remove a character, or replace a character. Given two
4+
strings, write a function to check if they are one edit (or zero edits) away.
5+
EXAMPLE
6+
pale, ple -> true
7+
pales, pale -> true
8+
pale, bale -> true
9+
pale, bake -> false
10+
Hints:#23, #97, #130
11+
"""
12+
13+
def oneAway(str1,str2):
14+
str1 = str1.lower()
15+
str2 = str2.lower()
16+
d = {}
17+
for i in str1:
18+
if i in d:
19+
d[i] =+ 1
20+
else:
21+
d[i] = 1
22+
23+
for j in str2:
24+
if j in d:
25+
d[j] -= 1
26+
27+
else:
28+
d[j] = 1
29+
count = 0# count the number of times
30+
31+
32+
"""
33+
My thought on how to do this:
34+
So knowing the pale and ple
35+
when we subtract it from the hash table we know there should be a value of 1
36+
for removal of one letter
37+
38+
The insertation of one letter should only leave the key of one
39+
pales and pale
40+
41+
The interseting stuff happens when you have a change of letter.
42+
The letter gets counting when you place it in the hash table
43+
pale bale.
44+
We use count to count the number of time count can be one. You'll find
45+
that replaceing a letter is going to make count two v's of one
46+
47+
replacing two letter will make v's count more onces
48+
49+
You'll see this when you print k and v
50+
51+
"""
52+
for k,v in d.items():
53+
#print(k,v)
54+
if v == 1:
55+
count += 1
56+
57+
if count <= 2 : #
58+
return True
59+
60+
else:
61+
return False
62+
63+
64+
65+
66+
67+
68+
69+
70+
71+
print(oneAway('pale', 'ple'))#True
72+
print(oneAway('pales', 'pale'))#True
73+
print(oneAway('pale', 'bale'))#True
74+
75+
print(oneAway('pale', 'bake'))#False
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
1.6:String Compression: Implement a method to perform basic string compression using
3+
the counts of repeated characters. For example, the string aabcccccaaa would
4+
become a2blc5a3. If the "compressed" string would not become smaller than the
5+
original string, your method should return
6+
the original string. You can assume the string has only uppercase and lowercase
7+
letters (a - z).
8+
"""
9+
10+
11+
12+
def string_compression(str1):
13+
count = 1
14+
new_str = ""
15+
for i in range(len(str1)-1):
16+
if(str1[i] == str1[i+1]):
17+
count+=1
18+
else:
19+
new_str += str1[i] + str(count)
20+
count = 1
21+
new_str += str1[i] + str(count)
22+
23+
if len(new_str) < len(str1):
24+
return new_str
25+
26+
else:
27+
return str1
28+
29+
30+
31+
32+
33+
34+
35+
36+
print(string_compression('aabcccccaaa'))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Rotate Matrix: Given an image represented by an NxN matrix, where each pixel
3+
in the image is 4 bytes, write a method to rotate the image by 90 degrees.
4+
Can you do this in place?
5+
Hints:#51, #100
6+
7+
"""
8+
9+
10+
def rotate_matrix(matrix):
11+
matrix.reverse()# we frist reverse the matrix then we take its transpose
12+
13+
m = len(matrix)
14+
n = len(matrix[0])
15+
for i in range(m):
16+
for j in range(i+1,n):
17+
temp = matrix[i][j]
18+
matrix[i][j] = matrix[j][i]
19+
matrix[j][i] = temp
20+
21+
22+
matrix = [[1,2,3],[4,5,6],[7,8,9]]
23+
rotate_matrix(matrix)
24+
#[[7, 4, 1], [8, 5, 2], [9, 6, 3]]
25+
26+
27+
print(matrix)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0,
3+
its entire row and column are set to 0.
4+
Hints:#17, #74, #702
5+
"""
6+
7+
8+
9+
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
16+
17+
18+
matrix = [[0,1,2],[3,4,5],[6,7,8]]
19+
20+
21+
zeroMatrix(matrix)
22+
print(matrix)

0 commit comments

Comments
 (0)