diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2022-11-20 14:22:01 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2022-11-20 14:22:01 -0500 |
| commit | dd682dfee966fe63cbfbbbf6a9cb903b1d831416 (patch) | |
| tree | a71619e10c8dcd29fc13a08beb1325f4a7bc5a84 /challenge-191/ealvar3z/python | |
| parent | d6d01468fd7a5647b9ba96ebf7a0157ff79f3352 (diff) | |
| parent | bde0adaf7b8dfe99c4e494c932d8702eb8cf9a56 (diff) | |
| download | perlweeklychallenge-club-dd682dfee966fe63cbfbbbf6a9cb903b1d831416.tar.gz perlweeklychallenge-club-dd682dfee966fe63cbfbbbf6a9cb903b1d831416.tar.bz2 perlweeklychallenge-club-dd682dfee966fe63cbfbbbf6a9cb903b1d831416.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-191/ealvar3z/python')
| -rw-r--r-- | challenge-191/ealvar3z/python/ch-1.py | 32 | ||||
| -rw-r--r-- | challenge-191/ealvar3z/python/ch-2.py | 39 |
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-191/ealvar3z/python/ch-1.py b/challenge-191/ealvar3z/python/ch-1.py new file mode 100644 index 0000000000..6785f8112e --- /dev/null +++ b/challenge-191/ealvar3z/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +from functools import reduce +import unittest + + +class TestSolution(unittest.TestCase): + cases = [ + [1, 2, 3, 4], + [1, 2, 0, 5], + [2, 6, 3, 1], + [4, 5, 2, 3], + ] + + def twice_largest(self, lst): + s = sorted(lst) + head = reduce(lambda x, y: x+y, s[:-1]) + tail = s[-1] + + if head > tail: + return -1 + else: + return 1 + + def test_twice_largest(self): + self.assertEqual(self.twice_largest(self.cases[0]), -1, 'example 1') + self.assertEqual(self.twice_largest(self.cases[1]), 1, 'example 2') + self.assertEqual(self.twice_largest(self.cases[2]), 1, 'example 3') + self.assertEqual(self.twice_largest(self.cases[3]), -1, 'example 4') + + +if __name__ == "__main__": + unittest.main(verbosity=True) diff --git a/challenge-191/ealvar3z/python/ch-2.py b/challenge-191/ealvar3z/python/ch-2.py new file mode 100644 index 0000000000..e013d84da8 --- /dev/null +++ b/challenge-191/ealvar3z/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +import unittest + + +class TestSolutionII(unittest.TestCase): + + def cute_list(self, n): + bitset_total = 2**n + bitset = [[0 for i in range(bitset_total)] for j in range(n+1)] + # set the base case + bitset[0][0] = 1 + + # iterate over all of the positions + for i in range(1, n+1): + # iterate over all of the subsets + for j in range(bitset_total): + # iterate over all of the numbers w/in each subset + for k in range(n): + # for each number in the subset + # check if the number exists? + visit = (j & (1 << k)) # is the bit set? + condition_one_holds = ((k+1) % i == 0) + condition_two_holds = (i % (k+1) == 0) + if visit and condition_one_holds or condition_two_holds: + # if one of these conditions hold, and we have seen the + # value, then the list is cute. + # Grab the count of what we've seen and return it. + # if this bit was set, unset it + visited = (j ^ (1 << k)) + saw_it = bitset[i-1][visited] + count = bitset[i][j] = bitset[i][j] + saw_it + return count + + def test_cute_list(self): + self.assertEqual(self.cute_list(2), 2) + + +if __name__ == "__main__": + unittest.main(verbosity=True) |
