diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-16 11:43:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 11:43:14 +0100 |
| commit | 7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2 (patch) | |
| tree | 3add2739160fb596d1a24b4d0b455ebadc8f18b4 /challenge-278/pokgopun/python/ch-1.py | |
| parent | 6d0af37dbeba795de3a812874f1d711995d16c2d (diff) | |
| parent | a92039a93c92921d5522ca3e43eee35b3a29ea75 (diff) | |
| download | perlweeklychallenge-club-7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2.tar.gz perlweeklychallenge-club-7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2.tar.bz2 perlweeklychallenge-club-7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2.zip | |
Merge pull request #10435 from pokgopun/pwc278
Pwc278
Diffstat (limited to 'challenge-278/pokgopun/python/ch-1.py')
| -rw-r--r-- | challenge-278/pokgopun/python/ch-1.py | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/challenge-278/pokgopun/python/ch-1.py b/challenge-278/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..302f04325b --- /dev/null +++ b/challenge-278/pokgopun/python/ch-1.py @@ -0,0 +1,67 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-278/ +""" + +Task 1: Sort String + +Submitted by: [51]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a shuffle string, $str. + + Write a script to return the sorted string. + + A string is shuffled by appending word position to each word. + +Example 1 + +Input: $str = "and2 Raku3 cousins5 Perl1 are4" +Output: "Perl and Raku are cousins" + +Example 2 + +Input: $str = "guest6 Python1 most4 the3 popular5 is2 language7" +Output: "Python is the most popular guest language" + +Example 3 + +Input: $str = "Challenge3 The1 Weekly2" +Output: "The Weekly Challenge" + +Task 2: Reverse Word +""" +### solution by pokgopun@gmail.com + +''' +def sortString(string: str): + return " ".join( + e[:-1] for e in sorted( + string.split(), key=lambda x: x[-1] + ) + ) +''' + +def rankWord(string: str): + i = 0 + while string[i-1].isdigit(): + i -= 1 + return (int(string[i:]),string[:i]) + +def sortString(string: str): + return " ".join( + e[1] for e in sorted( + rankWord(x) for x in string.split() + ) + ) + +import unittest + +class TestSortString(unittest.TestCase): + def test(self): + for inpt, otpt in { + "and2 Raku3 cousins5 Perl1 are4": "Perl and Raku are cousins", + "guest6 Python1 most4 the3 popular5 is2 language7": "Python is the most popular guest language", + "Challenge3 The1 Weekly2": "The Weekly Challenge", + }.items(): + self.assertEqual(sortString(inpt),otpt) + +unittest.main() |
