diff options
| author | Michael Manring <michael@manring> | 2024-06-08 05:14:55 +1000 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-06-08 11:56:39 +1000 |
| commit | 7f74b9b11f864730d4d2ccd5e6b7ac5d2c8ab2b0 (patch) | |
| tree | bcc37c312288811389bd68c68854436506da7c0a | |
| parent | b1a98b4180e62798cef122039206b379451b56ef (diff) | |
| download | perlweeklychallenge-club-7f74b9b11f864730d4d2ccd5e6b7ac5d2c8ab2b0.tar.gz perlweeklychallenge-club-7f74b9b11f864730d4d2ccd5e6b7ac5d2c8ab2b0.tar.bz2 perlweeklychallenge-club-7f74b9b11f864730d4d2ccd5e6b7ac5d2c8ab2b0.zip | |
pwc272 solution in go
| -rw-r--r-- | challenge-272/pokgopun/go/ch-1.go | 59 | ||||
| -rw-r--r-- | challenge-272/pokgopun/go/ch-2.go | 98 |
2 files changed, 157 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 + } +} |
