-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.py
185 lines (160 loc) · 5.24 KB
/
string.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# String Operation
# Always consider edge cases (empty, numeric, space)
# Always use test cases to test the code
# Always use and, or, not
# If use while loop, don't forget to increment i
# Read the question at least twice
# Add some explanation comments at the end
# string and tuple are immutable
# Question: Find the longest palindromic substring in a string
def longestPalindrome(string):
for length in range(len(string), 0, -1):
for i in range(len(string) - length + 1):
if isPalindrome(string[i:i+length]):
return string[i:i+length]
else:
continue
return 'No palindrome found.'
def isPalindrome(string):
if len(string) == 1:
return False
else:
start = 0
end = len(string)-1
while start < end:
if string[start] != string[end]:
return False
start += 1
end -= 1
return True
print(longestPalindrome("1234567654321"))
print(longestPalindrome("12345676543212"))
print(longestPalindrome("12321123454321"))
# Question: Determine if a string has unique characters
def isUniqueChar(str):
dict = {}
for char in str:
if char in dict and dict[char]:
return False
else:
dict[char] = True
return True
print(isUniqueChar('abcdef'))
print(isUniqueChar('aabcdef'))
# Question: Reverse a string
def reverseStr(str):
str_new = ''
for i in range(len(str)-1, -1, -1):
str_new += str[i]
return str_new
print(reverseStr('abcdefg'))
print(reverseStr(''))
# Question: Given two strings, write a method to decide if one is a substring of the other
def isSubstringEasy(str1, str2):
return str1 in str2
def isSubstring(str1, str2):
# check if str1 is a substring of str2
if len(str2) < len(str1):
return False
i = 0
while i+len(str1) < len(str2):
if str2[i:i+len(str1)] == str1:
return True
else:
i += 1
return False
print(isSubstringEasy('dogs', 'dogs are awesome!'))
print(isSubstringEasy('dogsa', 'dogs are awesome!'))
print(isSubstring('dogs', 'dogs are awesome!'))
print(isSubstring('dogsa', 'dogs are awesome!'))
# Question: Given two strings, write a method to decide if one is a permutation of the other
# Assume it is case sensitive and white spaces are significant
def isPermutation(str1, str2):
len1 = len(str1)
len2 = len(str2)
if len1 != len2:
return False
char_count = {} # to store the count of each char in str1
for char in str1:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
for char in str2:
if char not in char_count:
return False
else:
char_count[char] -= 1
if char_count[char] < 0:
return False
return True
print(isPermutation('dog is amazing', 'god amazing is'))
print(isPermutation('', ''))
# Question: Replace part of the string with another string
def replaceStr(str, targetStr, replaceStr):
strReplaced = ''
i = 0
while i < len(str):
if str[i:i+len(targetStr)] == targetStr:
strReplaced += replaceStr
i += len(targetStr)
else:
strReplaced += str[i]
i += 1
return strReplaced
print(replaceStr('Tomorrow is another day!', 'another', 'new'))
# Question: Compress a string, e.g. aabbccc -> a2b2c3
def compressStr(string):
char_count = {}
for char in string:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
compressed_str = ''
for key, value in char_count.items():
compressed_str += str(key)+str(value)
if len(compressed_str) > len(string):
return string
else:
return compressed_str
print(compressStr('aabbccc'))
print(compressStr(''))
# Question: Print all filenames of a given directory with a recursive approach
def printFile(path):
for child in os.listdir(path):
childPath = os.path.join(path,child)
if os.path.isdir(childPath):
printFile(childPath)
else:
print childPath
printFile("/Users/johnnycao/Development/johnny/Crack")
# Question: Return the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count.
#count_code('aaacodebbb') → 1
#count_code('codexxcode') → 2
#count_code('cozexxcope') → 2
def count_code(str):
if len(str) <4: return 0
count = 0
i = 0
while i <= len(str):
if len(str[i:]) <4: return count
if str[i:i+2] == 'co' and str[i+3] == 'e':
count +=1
i +=4
else:
i +=1
return count
# Question: Return True if the string "cat" and "dog" appear the same number of times in the given string.
#cat_dog('catdog') → True
#cat_dog('catcat') → False
#cat_dog('1cat1cadodog') → True
def cat_dog(str):
cat_count = 0
dog_count = 0
if len(str) < 3: return True
for i in range(len(str)-1):
if len(str[i:]) < 3 : break
if str[i:i+3] == 'cat': cat_count+=1
elif str[i:i+3] == 'dog': dog_count+=1
return cat_count == dog_count