repl.it
@shahinism/

Permutation Python

Python

No description

fork
loading
Files
  • main.py
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.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
from nose.tools import assert_equal
from collections import defaultdict


class Permutations(object):
    def is_permutation(self, str1, str2):
        if not (isinstance(str1, str) and isinstance(str2, str)):
            return False

        if not len(str1) == len(str2):
            return False

        str1 = set(str1)
        str2 = set(str2)
        return not str1.symmetric_difference(str2)


class PermutationsAlt(object):

    def is_permutation(self, str1, str2):
        if not (isinstance(str1, str) and isinstance(str2, str)):
            return False

        unique_counts1 = defaultdict(int)
        unique_counts2 = defaultdict(int)
        for char in str1:
            unique_counts1[char] += 1
        for char in str2:
            unique_counts2[char] += 1
        return unique_counts1 == unique_counts2


class TestPermutation(object):
    def test_permutation(self, func):
        assert_equal(func(None, "foo"), False)
        assert_equal(func("", "foo"), False)
        assert_equal(func("Nib", "bin"), False)
        assert_equal(func("act", "cat"), True)
        assert_equal(func("a ct", "ca t"), True)
        assert_equal(func("dog", "doggo"), False)
        print("Success: test_permutation")


def main():
    test = TestPermutation()
    permutations = Permutations()
    test.test_permutation(permutations.is_permutation)
    try:
        permutations_alt = PermutationsAlt()
        test.test_permutation(permutations_alt.is_permutation)
    except NameError:
        # Alternate solutions are only defined
        # in the solutions file
        pass


if __name__ == "__main__":
    main()
Fetching token
?