From f0bae065da415ed8133134815b26e8965fd93cf2 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 15 Jul 2024 22:51:52 +1000 Subject: pwc278 solution in python --- challenge-278/pokgopun/python/ch-1.py | 67 +++++++++++++++++++++++++++++++++++ challenge-278/pokgopun/python/ch-2.py | 55 ++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 challenge-278/pokgopun/python/ch-1.py create mode 100644 challenge-278/pokgopun/python/ch-2.py 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() diff --git a/challenge-278/pokgopun/python/ch-2.py b/challenge-278/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..a5e1e34059 --- /dev/null +++ b/challenge-278/pokgopun/python/ch-2.py @@ -0,0 +1,55 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-278/ +""" + +Task 2: Reverse Word + +Submitted by: [52]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a word, $word and a character, $char. + + Write a script to replace the substring up to and including $char with + its characters sorted alphabetically. If the $char doesn’t exist then + DON'T do anything. + +Example 1 + +Input: $str = "challenge", $char = "e" +Ouput: "acehllnge" + +Example 2 + +Input: $str = "programming", $char = "a" +Ouput: "agoprrmming" + +Example 3 + +Input: $str = "champion", $char = "b" +Ouput: "champion" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 21st July 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def reverseWord(word, char): + i = word.find(char) + if i < 0: + return word + return "".join(sorted(word[:i+1]))+word[i+1:] + +import unittest + +class TestReverseWord(unittest.TestCase): + def test(self): + for (word,char), otpt in { + ("challenge", "e"): "acehllnge", + ("programming", "a"):"agoprrmming", + ("champion", "b"): "champion", + }.items(): + self.assertEqual(reverseWord(word,char), otpt) + +unittest.main() -- cgit