Skip to content

Commit c2d1f39

Browse files
2 permutations check
1 parent ee1d3fc commit c2d1f39

File tree

1 file changed

+65
-0
lines changed
  • Python/chapter01/1.1 - Is Unique

1 file changed

+65
-0
lines changed
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()

0 commit comments

Comments
 (0)