Skip to content

Instantly share code, notes, and snippets.

@copyleftdev
Last active October 25, 2016 17:26
Show Gist options
  • Save copyleftdev/7014a04d089ff527dcfd1bd04077850c to your computer and use it in GitHub Desktop.
Save copyleftdev/7014a04d089ff527dcfd1bd04077850c to your computer and use it in GitHub Desktop.
Permutation Solution - With Tests
import itertools
import unittest
def permutation_solution(list_object):
result_set = set()
permuation_generator = itertools.permutations(list_object)
for each_ele in permuation_generator:
result_set.add(each_ele)
return result_set
class TestPermutationSolution(unittest.TestCase):
def test_permutation_with_1_list_item(self):
'''Test permutations with only 1 item'''
assertVector = 1
test_list = [1]
result = permutation_solution(test_list)
self.assertEqual(len(result), assertVector)
def test_permutation_range_0_to_3_set_compare(self):
'''Test permutations and compare sets'''
assertVector = set([(2, 1, 0), (0, 1, 2), (1, 0, 2),
(2, 0, 1), (0, 2, 1), (1, 2, 0)])
test_list = [0, 1, 2]
result = permutation_solution(test_list)
self.assertEqual(result, assertVector)
def test_permutation_range_0_to_10(self):
'''asserting 3,628,800 permutations in range 0-10'''
assertVector = 3628800
test_list = range(0, 10)
result = permutation_solution(test_list)
self.assertEqual(len(result), assertVector)
def test_purmutions_with_alpha_numeric_characters(self):
'''asserting 760 permutations in range ['A', 'B', 'C', 1, 2, 3]'''
assertVector = 720
test_list = ['A', 'B', 'C', 1, 2, 3]
result = permutation_solution(test_list)
self.assertEqual(len(result), assertVector)
def test_permutation_with_empty_list(self):
'''asserting empty list will have length of 1'''
assertVector = 1
test_list = []
result = permutation_solution(test_list)
self.assertEqual(len(result), assertVector)
def test_purmutions_with_punctuations(self):
'''asserting punctuations'''
assertVector = 6
test_list = ["!", "@", "$"]
result = permutation_solution(test_list)
self.assertEqual(len(result), assertVector)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment