Skip to content

Commit 378adae

Browse files
committedJan 4, 2017
Optimize file CheckPermutation.py to be more Pythonic.
Also renamed chapter folders to have the same naming style.
1 parent 1b46e77 commit 378adae

File tree

19 files changed

+43
-35
lines changed

19 files changed

+43
-35
lines changed
 

‎Chapter 1/2_Check Permutation/CheckPermutation.py

-35
This file was deleted.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# O(N)
2+
import unittest
3+
from collections import Counter
4+
5+
6+
def check_permutation(str1, str2):
7+
if len(str1) != len(str2):
8+
return False
9+
counter = Counter()
10+
for c in str1:
11+
counter[c] += 1
12+
for c in str2:
13+
if counter[c] == 0:
14+
return False
15+
counter[c] -= 1
16+
return True
17+
18+
19+
class Test(unittest.TestCase):
20+
dataT = (
21+
('abcd', 'bacd'),
22+
('3563476', '7334566'),
23+
('wef34f', 'wffe34'),
24+
)
25+
dataF = (
26+
('abcd', 'd2cba'),
27+
('2354', '1234'),
28+
('dcw4f', 'dcw5f'),
29+
)
30+
31+
def test_cp(self):
32+
# true check
33+
for test_strings in self.dataT:
34+
result = check_permutation(*test_strings)
35+
self.assertTrue(result)
36+
# false check
37+
for test_strings in self.dataF:
38+
result = check_permutation(*test_strings)
39+
self.assertFalse(result)
40+
41+
42+
if __name__ == "__main__":
43+
unittest.main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)
Please sign in to comment.