diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2021-07-11 18:32:03 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2021-07-11 18:32:03 +0200 |
| commit | eb25d7d64b549d92977c3f3a28aead725cffefb2 (patch) | |
| tree | 4ed13637792e34201bb364eacafe89e9afc46d41 /challenge-120 | |
| parent | c64e679033a4e92ab286486db649f5a488b654d3 (diff) | |
| download | perlweeklychallenge-club-eb25d7d64b549d92977c3f3a28aead725cffefb2.tar.gz perlweeklychallenge-club-eb25d7d64b549d92977c3f3a28aead725cffefb2.tar.bz2 perlweeklychallenge-club-eb25d7d64b549d92977c3f3a28aead725cffefb2.zip | |
Challenge 120 LK Python
Diffstat (limited to 'challenge-120')
| -rw-r--r-- | challenge-120/lubos-kolouch/python/ch-1.py | 28 | ||||
| -rw-r--r-- | challenge-120/lubos-kolouch/python/ch-2.py | 36 |
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-120/lubos-kolouch/python/ch-1.py b/challenge-120/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..9fb89c1f0f --- /dev/null +++ b/challenge-120/lubos-kolouch/python/ch-1.py @@ -0,0 +1,28 @@ +# =============================================================================== +# +# FILE: ch-2.pl +# +# USAGE: ./ch-2.pl +# +# DESCRIPTION: Perl Weekly Challenge #120 +# Task 1 - Swap Odd/Even bits +# +# AUTHOR: Lubos Kolouch +# CREATED: 20210710 04:44:33 PM +# =============================================================================== + + +def swap_bits(what: int): + + binary_what = str(bin(what)[2:]) + + if len(binary_what) % 2 == 1: + binary_what = "0" + binary_what + + arr = [binary_what[i:i+2] for i in range(0, len(binary_what), 2)] + rev_arr = list(map(lambda x: x[::-1], arr)) + return int("".join(rev_arr), 2) + + +assert swap_bits(101) == 154 +assert swap_bits(18) == 33 diff --git a/challenge-120/lubos-kolouch/python/ch-2.py b/challenge-120/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..312f006329 --- /dev/null +++ b/challenge-120/lubos-kolouch/python/ch-2.py @@ -0,0 +1,36 @@ +# =============================================================================== +# +# FILE: ch-2.py +# +# USAGE: ./ch-2.py +# +# DESCRIPTION: Perl Weekly Challenge #120 +# Task 2 - Clock Angle +# +# AUTHOR: Lubos Kolouch +# CREATED: 20210710 04:44:33 PM +# =============================================================================== + + +def calc_angle(what: str): + + hour, minute = map(int, what.split(':')) + + # each minute is 6 degrees + angle_min = 6 * minute + + # hour hand has moved 30 * whole hours + 0.5 * mins + angle_hour = 30 * hour + 0.5 * minute + + result_angle = abs(angle_hour - angle_min) + + # the challenge is asking for the smaller angle + if result_angle > 180: + return 360-result_angle + + return result_angle + + +assert calc_angle("03:10") == 35 +assert calc_angle("04:00") == 120 +assert calc_angle("10:14") == 137 |
