diff options
| -rw-r--r-- | challenge-272/pokgopun/go/ch-1.go | 59 | ||||
| -rw-r--r-- | challenge-272/pokgopun/go/ch-2.go | 98 | ||||
| -rw-r--r-- | challenge-272/pokgopun/python/ch-1.py | 42 | ||||
| -rw-r--r-- | challenge-272/pokgopun/python/ch-2.py | 87 |
4 files changed, 286 insertions, 0 deletions
diff --git a/challenge-272/pokgopun/go/ch-1.go b/challenge-272/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..10a9c05be0 --- /dev/null +++ b/challenge-272/pokgopun/go/ch-1.go @@ -0,0 +1,59 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-272/ +/*# + +Task 1: Defang IP Address + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a valid IPv4 address. + + Write a script to return the defanged version of the given IP address. + + A defanged IP address replaces every period “.” with “[.]". + +Example 1 + +Input: $ip = "1.1.1.1" +Output: "1[.]1[.]1[.]1" + +Example 2 + +Input: $ip = "255.101.1.0" +Output: "255[.]101[.]1[.]0" + +Task 2: String Score +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func dfip(str string) string { + var rs []rune + for _, v := range str { + if v == '.' { + rs = append(rs, '[', '.', ']') + } else { + rs = append(rs, v) + } + } + return string(rs) +} + +func main() { + for _, data := range []struct { + input, output string + }{ + {"1.1.1.1", "1[.]1[.]1[.]1"}, + {"255.101.1.0", "255[.]101[.]1[.]0"}, + } { + io.WriteString(os.Stdout, cmp.Diff(dfip(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-272/pokgopun/go/ch-2.go b/challenge-272/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..a66313e197 --- /dev/null +++ b/challenge-272/pokgopun/go/ch-2.go @@ -0,0 +1,98 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-272/ +/*# + +Task 2: String Score + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str. + + Write a script to return the score of the given string. + + The score of a string is defined as the sum of the absolute + difference between the ASCII values of adjacent characters. + +Example 1 + +Input: $str = "hello" +Output: 13 + +ASCII values of characters: +h = 104 +e = 101 +l = 108 +l = 108 +o = 111 + +Score => |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| + => 3 + 7 + 0 + 3 + => 13 + +Example 2 + +Input: "perl" +Output: 30 + +ASCII values of characters: +p = 112 +e = 101 +r = 114 +l = 108 + +Score => |112 - 101| + |101 - 114| + |114 - 108| + => 11 + 13 + 6 + => 30 + +Example 3 + +Input: "raku" +Output: 37 + +ASCII values of characters: +r = 114 +a = 97 +k = 107 +u = 117 + +Score => |114 - 97| + |97 - 107| + |107 - 117| + => 17 + 10 + 10 + => 37 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 9th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func ss(str string) int { + var c int + for i := range len(str) - 1 { + c += int(max(str[i], str[i+1]) - min(str[i], str[i+1])) + } + return c +} + +func main() { + for _, data := range []struct { + input string + output int + }{ + {"hello", 13}, + {"perl", 30}, + {"raku", 37}, + } { + io.WriteString(os.Stdout, cmp.Diff(ss(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-272/pokgopun/python/ch-1.py b/challenge-272/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..2b328144e9 --- /dev/null +++ b/challenge-272/pokgopun/python/ch-1.py @@ -0,0 +1,42 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-272/ +""" + +Task 1: Defang IP Address + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a valid IPv4 address. + + Write a script to return the defanged version of the given IP address. + + A defanged IP address replaces every period “.” with “[.]". + +Example 1 + +Input: $ip = "1.1.1.1" +Output: "1[.]1[.]1[.]1" + +Example 2 + +Input: $ip = "255.101.1.0" +Output: "255[.]101[.]1[.]0" + +Task 2: String Score +""" +### solution by pokgopun@gmail.com + +def dfip(string: str): + return string.replace(".","[.]") + +import unittest + +class TestDfip (unittest.TestCase): + def test(self): + for inpt, otpt in { + "1.1.1.1": "1[.]1[.]1[.]1", + "255.101.1.0": "255[.]101[.]1[.]0", + }.items(): + self.assertEqual(dfip(inpt),otpt) + +unittest.main() diff --git a/challenge-272/pokgopun/python/ch-2.py b/challenge-272/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..065e0b1609 --- /dev/null +++ b/challenge-272/pokgopun/python/ch-2.py @@ -0,0 +1,87 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-272/ +""" + +Task 2: String Score + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str. + + Write a script to return the score of the given string. + + The score of a string is defined as the sum of the absolute + difference between the ASCII values of adjacent characters. + +Example 1 + +Input: $str = "hello" +Output: 13 + +ASCII values of characters: +h = 104 +e = 101 +l = 108 +l = 108 +o = 111 + +Score => |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| + => 3 + 7 + 0 + 3 + => 13 + +Example 2 + +Input: "perl" +Output: 30 + +ASCII values of characters: +p = 112 +e = 101 +r = 114 +l = 108 + +Score => |112 - 101| + |101 - 114| + |114 - 108| + => 11 + 13 + 6 + => 30 + +Example 3 + +Input: "raku" +Output: 37 + +ASCII values of characters: +r = 114 +a = 97 +k = 107 +u = 117 + +Score => |114 - 97| + |97 - 107| + |107 - 117| + => 17 + 10 + 10 + => 37 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 9th June 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def ss(string: str): + return sum( + abs(ord(p[0])-ord(p[1])) for p in + (string[i:i+2] for i in range(len(string)-1)) + ) + +import unittest + +class TestSs(unittest.TestCase): + def test(self): + for inpt, otpt in { + "hello": 13, + "perl": 30, + "raku": 37, + }.items(): + self.assertEqual(ss(inpt),otpt) + +unittest.main() |
