diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-11-22 18:34:47 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-22 18:34:47 +0000 |
| commit | 382d33cc44a6d23087f67196305fb59c17c25ce9 (patch) | |
| tree | a9d8722df53fa6db8e076b275c68101cf03daec7 | |
| parent | 6986144644b739ad3bff30d0102a9c992e2d4d39 (diff) | |
| parent | ce95e7e0863d508763fa725107de7596b9581862 (diff) | |
| download | perlweeklychallenge-club-382d33cc44a6d23087f67196305fb59c17c25ce9.tar.gz perlweeklychallenge-club-382d33cc44a6d23087f67196305fb59c17c25ce9.tar.bz2 perlweeklychallenge-club-382d33cc44a6d23087f67196305fb59c17c25ce9.zip | |
Merge pull request #7139 from ealvar3z/challenge-192
go & python solutions for week 192
| -rw-r--r-- | challenge-192/ealvar3z/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-192/ealvar3z/go/ch-1.go | 15 | ||||
| -rw-r--r-- | challenge-192/ealvar3z/go/ch-1_test.go | 22 | ||||
| -rw-r--r-- | challenge-192/ealvar3z/go/ch-2.go | 22 | ||||
| -rw-r--r-- | challenge-192/ealvar3z/go/ch-2_test.go | 22 | ||||
| -rw-r--r-- | challenge-192/ealvar3z/python/ch-1.py | 19 | ||||
| -rw-r--r-- | challenge-192/ealvar3z/python/ch-2.py | 39 |
7 files changed, 140 insertions, 0 deletions
diff --git a/challenge-192/ealvar3z/blog.txt b/challenge-192/ealvar3z/blog.txt new file mode 100644 index 0000000000..77f296fe1c --- /dev/null +++ b/challenge-192/ealvar3z/blog.txt @@ -0,0 +1 @@ +https://alvar3z.com/posts/bytethematics-perl-weekly-challenge-192/ diff --git a/challenge-192/ealvar3z/go/ch-1.go b/challenge-192/ealvar3z/go/ch-1.go new file mode 100644 index 0000000000..7946a5af68 --- /dev/null +++ b/challenge-192/ealvar3z/go/ch-1.go @@ -0,0 +1,15 @@ +package main + +import "math/bits" + +func max(x, y int) int { + if x > y { + return x + } + return y +} + +func BinaryFlip(n int) int { + mask := max(1, (1<<(bits.Len(uint(n))) - 1)) + return n ^ mask +} diff --git a/challenge-192/ealvar3z/go/ch-1_test.go b/challenge-192/ealvar3z/go/ch-1_test.go new file mode 100644 index 0000000000..11c2e783a8 --- /dev/null +++ b/challenge-192/ealvar3z/go/ch-1_test.go @@ -0,0 +1,22 @@ +package main + +import ( + "testing" +) + +func TestSolution(t *testing.T) { + cases := []struct { + in int + out int + example string + }{ + {5, 2, "example 1"}, + {4, 3, "example 2"}, + {6, 1, "example 3"}, + } + for _, c := range cases { + if got := BinaryFlip(c.in); got != c.out { + t.Errorf("got %d, want %d, on %s", got, c.out, c.example) + } + } +} diff --git a/challenge-192/ealvar3z/go/ch-2.go b/challenge-192/ealvar3z/go/ch-2.go new file mode 100644 index 0000000000..4b662753d3 --- /dev/null +++ b/challenge-192/ealvar3z/go/ch-2.go @@ -0,0 +1,22 @@ +package main + +import "math" + +func Mean(l []float64) float64 { + if len(l) == 0 { + return math.NaN() + } + m := 0.0 + for i, v := range l { + m += (v - m) / float64(i+1) + } + return m +} + +func equalDistro(l []float64) int { + m := int(Mean(l)) + if m == 0 { + return -1 + } + return m + m +} diff --git a/challenge-192/ealvar3z/go/ch-2_test.go b/challenge-192/ealvar3z/go/ch-2_test.go new file mode 100644 index 0000000000..f5dfcce1ce --- /dev/null +++ b/challenge-192/ealvar3z/go/ch-2_test.go @@ -0,0 +1,22 @@ +package main + +import ( + "testing" +) + +func TestSolutionII(t *testing.T) { + cases := []struct { + in []float64 + out int + example string + }{ + {[]float64{1, 0, 5}, 4, "example 1"}, + {[]float64{0, 2, 0}, -1, "example 2"}, + {[]float64{0, 3, 0}, 2, "example 3"}, + } + for _, c := range cases { + if got := equalDistro(c.in); got != c.out { + t.Errorf("got %d, want %d, on %s", got, c.out, c.example) + } + } +} diff --git a/challenge-192/ealvar3z/python/ch-1.py b/challenge-192/ealvar3z/python/ch-1.py new file mode 100644 index 0000000000..47fcd5a0f0 --- /dev/null +++ b/challenge-192/ealvar3z/python/ch-1.py @@ -0,0 +1,19 @@ +import unittest + + +class TestSolutionI(unittest.TestCase): + + def binary_flip(self, n): + mask = max(1, (1 << n.bit_length()) - 1) + return n ^ mask + + def test_binary_flip(self): + self.assertEqual(self.binary_flip(5), 2, "example 1") + self.assertEqual(self.binary_flip(4), 3, "example 2") + self.assertEqual(self.binary_flip(6), 1, "example 3") + self.assertEqual(self.binary_flip(0), 1, "nasty edge case") + self.assertEqual(self.binary_flip(-10), -7, "negative num edge case") + + +if __name__ == "__main__": + unittest.main(verbosity=True) diff --git a/challenge-192/ealvar3z/python/ch-2.py b/challenge-192/ealvar3z/python/ch-2.py new file mode 100644 index 0000000000..a9f4b37bec --- /dev/null +++ b/challenge-192/ealvar3z/python/ch-2.py @@ -0,0 +1,39 @@ +from statistics import mean +from unittest import main, TestCase + +""" +Example 1: + Input: @list = (1, 0, 5) + Output: 4 +Example 2: + Input: @list = (0, 2, 0) + Output: -1 +Example 3: + Input: (0, 3, 0) + Output: 2 +""" + +cases = [ + ([1, 0, 5], 4), + ([0, 2, 0], -1), + ([0, 3, 0], 2), +] + + +class TestSolutionII(TestCase): + + def equal_distro(self, _list): + m = int(mean(_list)) + if m == 0: + return -1 + else: + return m + m + + def test_equal_distribution(self): + for input, output in cases: + with self.subTest(input=output): + self.assertEqual(self.equal_distro(input), output) + + +if __name__ == "__main__": + main(verbosity=2) |
