Skip to content

Commit 17c4f7e

Browse files
authored
Merge pull request #6 from nickolasteixeira/nick
Nick
2 parents 35831a8 + 898d4a0 commit 17c4f7e

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/python3
2+
'''
3+
Module that implements an algorithm to determine if a string has all unique characters. What if you
4+
cannot use additional data structures?
5+
'''
6+
7+
import unittest
8+
9+
def unique(string):
10+
'''checks if string input is unique
11+
Args:
12+
string (str): string to check
13+
'''
14+
return len(set(string)) is len(string)
15+
16+
def unique2(string):
17+
'''checks if string input is unique
18+
args:
19+
string (str): string to check
20+
'''
21+
letters = {}
22+
for letter in string:
23+
letters[letter] = letters.get(letter, 0) + 1
24+
if letters.get(letter) is not None and letters[letter] > 1:
25+
return False
26+
return True
27+
28+
def unique3(string):
29+
'''checks if string input is unique, no new data structures
30+
args:
31+
string (str): string to check
32+
'''
33+
new_string = sorted(string)
34+
for idx in range(len(new_string) - 1):
35+
if new_string[idx] == new_string[idx + 1]:
36+
return False
37+
38+
return True
39+
40+
41+
class Test(unittest.TestCase):
42+
def test1(self):
43+
all_true = ["Nick", "world"]
44+
all_false = ["eee", "hello"]
45+
46+
for idx in range(len(all_true)):
47+
self.assertTrue(unique(all_true[idx]))
48+
self.assertFalse(unique(all_false[idx]))
49+
50+
def test2(self):
51+
all_true = ["Nick", "world", "Sue", "John", "abcdefghijklmnopqrstuvwxyz"]
52+
all_false = ["eee", "hello", "nicki", "johnny", "suuu"]
53+
54+
for idx in range(len(all_true)):
55+
self.assertTrue(unique2(all_true[idx]))
56+
self.assertFalse(unique2(all_false[idx]))
57+
58+
def test3(self):
59+
all_true = ["Nick", "world", "Sue", "John", "abcdefghijklmnopqrstuvwxyz"]
60+
all_false = ["eee", "hello", "nicki", "johnny", "suuu"]
61+
62+
for idx in range(len(all_true)):
63+
self.assertTrue(unique3(all_true[idx]))
64+
self.assertFalse(unique3(all_false[idx]))
65+
66+
if __name__ == '__main__':
67+
unittest.main()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/python3
2+
'''
3+
Module that Given two strings, write a method to decide if one is a permutation of the
4+
other.
5+
6+
'''
7+
8+
import unittest
9+
10+
def permutation(string1, string2):
11+
'''
12+
Checks two strings if one is a permutation of the other
13+
Args:
14+
string1 (str): a string
15+
string2 (str): another string
16+
'''
17+
if len(string1) is not len(string2):
18+
return False
19+
20+
n1, n2 = sorted(string1), sorted(string2)
21+
return n1 == n2
22+
23+
def permutation2(string1, string2):
24+
'''
25+
Checks two strings if one is a permutation of the other
26+
Args:
27+
string1 (str): a string
28+
string2 (str): another string
29+
'''
30+
if len(string1) is not len(string2):
31+
return False
32+
33+
d1, d2 = {}, {}
34+
for idx in range(len(string1)):
35+
d1[string1[idx]] = d1.get(string1[idx], 0) + 1
36+
d2[string2[idx]] = d2.get(string2[idx], 0) + 1
37+
38+
return d1 == d2
39+
40+
def permutation3(string1, string2):
41+
'''
42+
Checks two strings if one is a permutation of the other
43+
Args:
44+
string1 (str): a string
45+
string2 (str): another string
46+
'''
47+
48+
49+
class Test(unittest.TestCase):
50+
def test1(self):
51+
t1 = "nickiiii"
52+
t2 = "iiiiinck"
53+
x1, x2 = "hello", "world"
54+
self.assertTrue(permutation(t1,t2))
55+
self.assertFalse(permutation(x1, x2))
56+
57+
def test2(self):
58+
self.assertFalse(permutation2("nickjjj", "nickkki"))
59+
self.assertTrue(permutation2("iiiiinn", "nniiiii"))
60+
61+
def test3(self):
62+
pass
63+
64+
if __name__ == '__main__':
65+
unittest.main()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/python3
2+
'''
3+
Module that Write a method to replace all spaces in a string with '%20'. You may assume that the string
4+
has sufficient space at the end to hold the additional characters, and that you are given the "true"
5+
length of the string. (Note: If implementing in Java, please use a character array so that you can
6+
perform this operation in place.)
7+
'''
8+
9+
import unittest
10+
11+
def URLify(string, char):
12+
'''
13+
Function that replace all spaces in a string with '%20'.
14+
Args:
15+
string (str): a string to modify
16+
'''
17+
return string[:char + 1].replace(" ", "%20")
18+
19+
class Test(unittest.TestCase):
20+
def test1(self):
21+
input_string = "Mr Johns Smith "
22+
char = 13
23+
output_string = "Mr%20John%20Smith"
24+
25+
self.assertTrue(URLify(input_string, 13), output_string)
26+
27+
if __name__ == '__main__':
28+
unittest.main()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python3
2+
'''
3+
Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome.
4+
A palindrome is a word or phrase that is the same forwards and backwards. A permutation
5+
is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words
6+
'''
7+
8+
import unittest
9+
10+
11+
def palperm(string):
12+
'''
13+
Function that checks if it is a permutation of a palindrome
14+
Args:
15+
string (str): a string to check for all palindrome permutations
16+
'''
17+
table = {}
18+
count = 0
19+
for letter in string.lower().replace(" ", ""):
20+
table[letter] = table.get(letter, 0) + 1
21+
22+
for key in table:
23+
if count > 1:
24+
return False
25+
if table[key] % 2 == 1:
26+
count += 1
27+
return True
28+
29+
30+
class Test(unittest.TestCase):
31+
def test1(self):
32+
input_string ="Tact Coa"
33+
input_string2 ="nick"
34+
input_string3 ="saippuakivikauppias"
35+
input_string4 = "iasppaukivikauppias"
36+
self.assertTrue(palperm(input_string))
37+
self.assertFalse(palperm(input_string2))
38+
self.assertTrue(palperm(input_string3))
39+
self.assertTrue(palperm(input_string4))
40+
41+
if __name__ == '__main__':
42+
unittest.main()

0 commit comments

Comments
 (0)