From cff3bfa6eda5aa6f58717e02e7b7c4ee2e0bf853 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 21 Jul 2022 23:38:45 +0100 Subject: - Added solutions to the task "Permutation Ranking" of week 174. --- challenge-174/mohammad-anwar/python/ch-2.py | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 challenge-174/mohammad-anwar/python/ch-2.py (limited to 'challenge-174/mohammad-anwar/python/ch-2.py') diff --git a/challenge-174/mohammad-anwar/python/ch-2.py b/challenge-174/mohammad-anwar/python/ch-2.py new file mode 100644 index 0000000000..96b0492d4f --- /dev/null +++ b/challenge-174/mohammad-anwar/python/ch-2.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + +''' + +Week 174: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-174 + +Task #2: Permutation Ranking + + You are given a list of integers with no duplicates, e.g. [0, 1, 2]. + + Write two functions, permutation2rank() which will take the list + and determine its rank (starting at 0) in the set of possible + permutations arranged in lexicographic order, and rank2permutation() + which will take the list and a rank number and produce just that + permutation. + +''' + +import unittest +import itertools + +def permutation2rank(array): + array.sort() + return list(itertools.permutations(array)) + +def rank2permutation(array, rank): + p2r = permutation2rank(array) + return p2r[rank] + +# +# +# Unit test class + +class TestPermutationRanking(unittest.TestCase): + def test_permutation2rank(self): + exp = [ (0, 1, 2), + (0, 2, 1), + (1, 0, 2), + (1, 2, 0), + (2, 0, 1), + (2, 1, 0), + ] + got = permutation2rank([1, 0, 2]) + self.assertEqual(exp, got) + + def test_rank2permutation(self): + exp = (0, 2, 1) + got = rank2permutation([1, 0, 2], 1) + self.assertEqual(exp, got) + +unittest.main() -- cgit