diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2020-09-29 21:23:29 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2020-09-29 21:23:29 +0200 |
| commit | 7de8a43add8580e5f34224658970520d989ea77e (patch) | |
| tree | 93aeee1f301ed719041862e902515dd7f12af454 /challenge-080/lubos-kolouch | |
| parent | b4c10c949c9ecade1377baf7f6468a0c0d77bcbe (diff) | |
| download | perlweeklychallenge-club-7de8a43add8580e5f34224658970520d989ea77e.tar.gz perlweeklychallenge-club-7de8a43add8580e5f34224658970520d989ea77e.tar.bz2 perlweeklychallenge-club-7de8a43add8580e5f34224658970520d989ea77e.zip | |
Python solutions Challenge 080 LK
Diffstat (limited to 'challenge-080/lubos-kolouch')
| -rw-r--r-- | challenge-080/lubos-kolouch/python/ch-1.py | 43 | ||||
| -rw-r--r-- | challenge-080/lubos-kolouch/python/ch-2.py | 34 |
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-080/lubos-kolouch/python/ch-1.py b/challenge-080/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..812337313a --- /dev/null +++ b/challenge-080/lubos-kolouch/python/ch-1.py @@ -0,0 +1,43 @@ +#!/bin/env python +""" +#=============================================================================== +# +# FILE: ch_1.py +# +# USAGE: ./ch_1.py +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/ +# Challenge #1 +# Smallest Positive Number Bits +# +# AUTHOR: Lubos Kolouch +#=============================================================================== +""" + + +def get_smallest_missing(arr: list): + """ Find if there is any number missing and return it """ + + # Let's see if are lucky and the minimum -1 is >0 + arr_min = min(n for n in arr if n > 0) + + if arr_min - 1 > 0: + return arr_min-1 + + # Not lucky, arr_min is 0, so need to iterate + # NOTE: the excercise does not say what to do if there is nothing missing + # so let's just return 0 as per Twitter confirmation + + while arr_min < max(arr): + arr_min += 1 + + if arr_min not in arr: + return arr_min + return 0 + + +assert get_smallest_missing([5, 2, -2, 0]) == 1 +assert get_smallest_missing([1, 8, -1]) == 2 +assert get_smallest_missing([2, 0, -1]) == 1 +assert get_smallest_missing([2, 0, 1]) == 0 +assert get_smallest_missing([2, 0, 1, 3, 4, 5]) == 0 diff --git a/challenge-080/lubos-kolouch/python/ch-2.py b/challenge-080/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..1496b407d5 --- /dev/null +++ b/challenge-080/lubos-kolouch/python/ch-2.py @@ -0,0 +1,34 @@ +#!/bin/env pythoon +""" +#=============================================================================== +# +# FILE: ch_2.py +# +# USAGE: ./ch_2.py +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/ +# Challenge #2 +# Count Candies +# +# AUTHOR: Lubos Kolouch +#=============================================================================== +""" + + +def get_candle_count(arr: list): + """ Count the candles """ + + # We need to give 1 candy to everyone + count = len(arr) + + # and then find out number of unique elements as they will be certainly + # bigger than neighbor... -1 (the initial poor one) + + count += len(set(arr)) + count -= 1 + + return count + + +assert get_candle_count([1, 2, 2]) == 4 +assert get_candle_count([1, 4, 3, 2]) == 7 |
