diff options
| author | Pok <pok@goyangi> | 2025-05-24 06:03:01 +1000 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-05-24 06:03:01 +1000 |
| commit | 9b545863337dfe4469644e1a201176d976343fef (patch) | |
| tree | 5f40c865c2f962b10da512e9e0e26e9055e8ae43 | |
| parent | d9a8a0d7aba084dc687dd97473f8739db9387b5c (diff) | |
| download | perlweeklychallenge-club-9b545863337dfe4469644e1a201176d976343fef.tar.gz perlweeklychallenge-club-9b545863337dfe4469644e1a201176d976343fef.tar.bz2 perlweeklychallenge-club-9b545863337dfe4469644e1a201176d976343fef.zip | |
pwc322 solution in python
| -rw-r--r-- | challenge-322/pokgopun/python/ch-1.py | 62 | ||||
| -rw-r--r-- | challenge-322/pokgopun/python/ch-2.py | 60 |
2 files changed, 122 insertions, 0 deletions
diff --git a/challenge-322/pokgopun/python/ch-1.py b/challenge-322/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..5a01b7908d --- /dev/null +++ b/challenge-322/pokgopun/python/ch-1.py @@ -0,0 +1,62 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-322/ +""" + +Task 1: String Format + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string and a positive integer. + + Write a script to format the string, removing any dashes, in groups of + size given by the integer. The first group can be smaller than the + integer but should have at least one character. Groups should be + separated by dashes. + +Example 1 + +Input: $str = "ABC-D-E-F", $i = 3 +Output: "ABC-DEF" + +Example 2 + +Input: $str = "A-BC-D-E", $i = 2 +Output: "A-BC-DE" + +Example 3 + +Input: $str = "-A-B-CD-E", $i = 4 +Output: "A-BCDE" + +Task 2: Rank Array +""" +### solution by pokgopun@gmail.com + +def sf(string: str, n: int) -> str: + slt = "" + i = len(string) + l = 0 + while i > 0: + i -= 1 + if string[i] == "-": + continue + if l < n: + slt = string[i] + slt + l += 1 + else: + slt = string[i] + "-" + slt + l = 1 + return slt + +import unittest + +class TestSf(unittest.TestCase): + def test(self): + for (string,n), otpt in { + ("ABC-D-E-F", 3):"ABC-DEF", + ("A-BC-D-E", 2): "A-BC-DE", + ("-A-B-CD-E", 4): "A-BCDE", + }.items(): + self.assertEqual(sf(string,n),otpt) + +unittest.main() diff --git a/challenge-322/pokgopun/python/ch-2.py b/challenge-322/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..830e4888a2 --- /dev/null +++ b/challenge-322/pokgopun/python/ch-2.py @@ -0,0 +1,60 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-322/ +""" + +Task 2: Rank Array + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to return an array of the ranks of each element: the + lowest value has rank 1, next lowest rank 2, etc. If two elements are + the same then they share the same rank. + +Example 1 + +Input: @ints = (55, 22, 44, 33) +Output: (4, 1, 3, 2) + +Example 2 + +Input: @ints = (10, 10, 10) +Output: (1, 1, 1) + +Example 3 + +Input: @ints = (5, 1, 1, 4, 3) +Output: (4, 1, 1, 3, 2) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 25th May 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def ra(ints: tuple[int]) -> tuple[int]: + a = [(ints[i],i) for i in range(len(ints)) ] + a.sort() + r = 1 + b = [(a[0][1],r)] + for i in range(1,len(a)): + if a[i][0] > a[i-1][0]: + r += 1 + b.append((a[i][1],r)) + return tuple(e[1] for e in sorted(b)) + +import unittest + +class TestRa(unittest.TestCase): + def test(self): + for inpt, otpt in { + (55, 22, 44, 33):(4, 1, 3, 2), + (10, 10, 10):(1, 1, 1), + (5, 1, 1, 4, 3): (4, 1, 1, 3, 2), + }.items(): + self.assertEqual(ra(inpt),otpt) + +unittest.main() |
