From 9ff9513860cd4e4fa87ecd33f9c7c50cab82721f Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 12 Aug 2024 11:22:50 +1000 Subject: pwc282 solution in python --- challenge-282/pokgopun/python/ch-1.py | 59 ++++++++++++++++++++++++++++++ challenge-282/pokgopun/python/ch-2.py | 68 +++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 challenge-282/pokgopun/python/ch-1.py create mode 100644 challenge-282/pokgopun/python/ch-2.py diff --git a/challenge-282/pokgopun/python/ch-1.py b/challenge-282/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..cdc088340f --- /dev/null +++ b/challenge-282/pokgopun/python/ch-1.py @@ -0,0 +1,59 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-282/ +""" + +Task 1: Good Integer + +Submitted by: [57]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a positive integer, $int, having 3 or more digits. + + Write a script to return the Good Integer in the given integer or -1 if + none found. + + A good integer is exactly three consecutive matching digits. + +Example 1 + +Input: $int = 12344456 +Output: "444" + +Example 2 + +Input: $int = 1233334 +Output: -1 + +Example 3 + +Input: $int = 10020003 +Output: "000" + +Task 2: Changing Keys +""" +### solution by pokgopun@gmail.com + +def gi(num: int): + count = 0 + digit = "" + for d in str(num): + if digit == d: + count += 1 + else: + if count == 3: + return digit*3 + digit = d + count = 1 + return -1 + +import unittest + +class TestGi(unittest.TestCase): + def test(self): + for inpt,otpt in { + 12344456: "444", + 1233334: -1, + 10020003: "000", + }.items(): + self.assertEqual(gi(inpt),otpt) + +unittest.main() diff --git a/challenge-282/pokgopun/python/ch-2.py b/challenge-282/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..787dc17450 --- /dev/null +++ b/challenge-282/pokgopun/python/ch-2.py @@ -0,0 +1,68 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-282/ +""" + +Task 2: Changing Keys + +Submitted by: [58]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an alphabetic string, $str, as typed by user. + + Write a script to find the number of times user had to change the key + to type the given string. Changing key is defined as using a key + different from the last used key. The shift and caps lock keys won’t be + counted. + +Example 1 + +Input: $str = 'pPeERrLl' +Ouput: 3 + +p -> P : 0 key change +P -> e : 1 key change +e -> E : 0 key change +E -> R : 1 key change +R -> r : 0 key change +r -> L : 1 key change +L -> l : 0 key change + +Example 2 + +Input: $str = 'rRr' +Ouput: 0 + +Example 3 + +Input: $str = 'GoO' +Ouput: 1 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 18th August + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def ck(string: str): + string = string.lower() + count = 0 + for i in range(len(string)-1): + if string[i]!=string[i+1]: + count += 1 + return count + +import unittest + +class TestCk(unittest.TestCase): + def test(self): + for inpt, otpt in { + 'pPeERrLl': 3, + 'rRr': 0, + 'GoO': 1, + }.items(): + self.assertEqual(ck(inpt),otpt) + +unittest.main() + -- cgit From 5fd8ee56bd06542ea11d5112e1b1a7d0237d4f40 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 12 Aug 2024 12:05:39 +1000 Subject: pwc282 solution in go --- challenge-282/pokgopun/go/ch-1.go | 80 ++++++++++++++++++++++++++++++++++++ challenge-282/pokgopun/go/ch-2.go | 85 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 challenge-282/pokgopun/go/ch-1.go create mode 100644 challenge-282/pokgopun/go/ch-2.go diff --git a/challenge-282/pokgopun/go/ch-1.go b/challenge-282/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..7254c6cde8 --- /dev/null +++ b/challenge-282/pokgopun/go/ch-1.go @@ -0,0 +1,80 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-282/ +/*# + +Task 1: Good Integer + +Submitted by: [57]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a positive integer, $int, having 3 or more digits. + + Write a script to return the Good Integer in the given integer or -1 if + none found. + + A good integer is exactly three consecutive matching digits. + +Example 1 + +Input: $int = 12344456 +Output: "444" + +Example 2 + +Input: $int = 1233334 +Output: -1 + +Example 3 + +Input: $int = 10020003 +Output: "000" + +Task 2: Changing Keys +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "strconv" + + "github.com/google/go-cmp/cmp" +) + +type answer struct { + Val_str string + Err_code int +} + +func goodInteger(num int) answer { + var ( + c int + r rune + ) + for _, v := range strconv.Itoa(num) { + if r == v { + c++ + } else { + if c == 3 { + return answer{Val_str: string([]rune{r, r, r})} + } + r = v + c = 1 + } + } + return answer{Err_code: -1} +} + +func main() { + for _, data := range []struct { + input int + output answer + }{ + {12344456, answer{Val_str: "444"}}, + {1233334, answer{Err_code: -1}}, + {10020003, answer{Val_str: "000"}}, + } { + io.WriteString(os.Stdout, cmp.Diff(goodInteger(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-282/pokgopun/go/ch-2.go b/challenge-282/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..4d11b56d8b --- /dev/null +++ b/challenge-282/pokgopun/go/ch-2.go @@ -0,0 +1,85 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-282/ +/*# + +Task 2: Changing Keys + +Submitted by: [58]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an alphabetic string, $str, as typed by user. + + Write a script to find the number of times user had to change the key + to type the given string. Changing key is defined as using a key + different from the last used key. The shift and caps lock keys won’t be + counted. + +Example 1 + +Input: $str = 'pPeERrLl' +Ouput: 3 + +p -> P : 0 key change +P -> e : 1 key change +e -> E : 0 key change +E -> R : 1 key change +R -> r : 0 key change +r -> L : 1 key change +L -> l : 0 key change + +Example 2 + +Input: $str = 'rRr' +Ouput: 0 + +Example 3 + +Input: $str = 'GoO' +Ouput: 1 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 18th August + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func ck(str string) int { + var a, b byte + c := -1 + for i := range len(str) { + if str[i] < 97 { + a = str[i] + 32 + } + if i > 0 { + if a != b { + c++ + } + b = a + } + } + return c +} + +func main() { + for _, data := range []struct { + input string + output int + }{ + {"pPeERrLl", 3}, + {"rRr", 0}, + {"GoO", 1}, + } { + io.WriteString(os.Stdout, cmp.Diff(ck(data.input), data.output)) // blank if ok, otherwise show the differece + } +} -- cgit