diff options
| author | Santiago Leyva <99155189+DevSanti12@users.noreply.github.com> | 2024-07-25 23:57:49 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-25 23:57:49 -0500 |
| commit | db40d75f603f3fae34944cb3a762b290b18577d8 (patch) | |
| tree | 1205c733ae416bcbce12e225a5252841316c5540 /challenge-279/pokgopun/python/ch-1.py | |
| parent | 350f495b09a4b6b6db8506533e2dccc68d9fc3f4 (diff) | |
| parent | 3d6cbce024396f3950dad9b40151458e4134202d (diff) | |
| download | perlweeklychallenge-club-db40d75f603f3fae34944cb3a762b290b18577d8.tar.gz perlweeklychallenge-club-db40d75f603f3fae34944cb3a762b290b18577d8.tar.bz2 perlweeklychallenge-club-db40d75f603f3fae34944cb3a762b290b18577d8.zip | |
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-279/pokgopun/python/ch-1.py')
| -rw-r--r-- | challenge-279/pokgopun/python/ch-1.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-279/pokgopun/python/ch-1.py b/challenge-279/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..d68c18c09b --- /dev/null +++ b/challenge-279/pokgopun/python/ch-1.py @@ -0,0 +1,53 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-279/ +""" + +Task 1: Sort Letters + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two arrays, @letters and @weights. + + Write a script to sort the given array @letters based on the @weights. + +Example 1 + +Input: @letters = ('R', 'E', 'P', 'L') + @weights = (3, 2, 1, 4) +Output: PERL + +Example 2 + +Input: @letters = ('A', 'U', 'R', 'K') + @weights = (2, 4, 1, 3) +Output: RAKU + +Example 3 + +Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T') + @weights = (5, 4, 2, 6, 1, 3) +Output: PYTHON + +Task 2: Split String +""" +### solution by pokgopun@gmail.com + +def sortLetters(letters,weights): + return "".join( + e[1] for e in sorted( + (weights[i],letters[i]) for i in range(len(letters)) + ) + ) + +import unittest + +class TestSortLetters(unittest.TestCase): + def test(self): + for (letters,weights),otpt in { + (('R', 'E', 'P', 'L'),(3, 2, 1, 4)): "PERL", + (('A', 'U', 'R', 'K'),(2, 4, 1, 3)): "RAKU", + (('O', 'H', 'Y', 'N', 'P', 'T'),(5, 4, 2, 6, 1, 3)): "PYTHON", + }.items(): + self.assertEqual(sortLetters(letters,weights),otpt) + +unittest.main() |
