diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-10-22 18:11:56 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-10-22 18:11:56 +0100 |
| commit | 45c413ded143a3d7548e837e2cc459f4fabcda41 (patch) | |
| tree | b82f0097d81f98326854e90cddbfaa9ff9613c15 /challenge-178 | |
| parent | c3edc9149c2c1d435eecf347139ca83094e1d86e (diff) | |
| download | perlweeklychallenge-club-45c413ded143a3d7548e837e2cc459f4fabcda41.tar.gz perlweeklychallenge-club-45c413ded143a3d7548e837e2cc459f4fabcda41.tar.bz2 perlweeklychallenge-club-45c413ded143a3d7548e837e2cc459f4fabcda41.zip | |
Add Python solutions
Diffstat (limited to 'challenge-178')
| -rw-r--r-- | challenge-178/paulo-custodio/perl/ch-1.pl | 8 | ||||
| -rw-r--r-- | challenge-178/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-178/paulo-custodio/python/ch-1.py | 41 | ||||
| -rw-r--r-- | challenge-178/paulo-custodio/python/ch-2.py | 46 | ||||
| -rw-r--r-- | challenge-178/paulo-custodio/t/test-1.yaml | 5 |
5 files changed, 92 insertions, 10 deletions
diff --git a/challenge-178/paulo-custodio/perl/ch-1.pl b/challenge-178/paulo-custodio/perl/ch-1.pl index 75bbc20fcd..96fbd0cfea 100644 --- a/challenge-178/paulo-custodio/perl/ch-1.pl +++ b/challenge-178/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 178 # @@ -14,7 +14,6 @@ # $number_quater_imaginary_base = 10300 use Modern::Perl; -use Test::More; sub dec_to_base2i { my($n) = @_; @@ -49,6 +48,5 @@ sub base2i_to_dec { return $result; } -is dec_to_base2i(4), 10300; -is base2i_to_dec(10300), 4; -done_testing; +dec_to_base2i(4) == 10300 or die; +base2i_to_dec(10300) == 4 or die; diff --git a/challenge-178/paulo-custodio/perl/ch-2.pl b/challenge-178/paulo-custodio/perl/ch-2.pl index 6e3e10ce37..53263eb7fd 100644 --- a/challenge-178/paulo-custodio/perl/ch-2.pl +++ b/challenge-178/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 178 # diff --git a/challenge-178/paulo-custodio/python/ch-1.py b/challenge-178/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..a27f6c780d --- /dev/null +++ b/challenge-178/paulo-custodio/python/ch-1.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +# Challenge 178 +# +# Task 1: Quater-imaginary Base +# Submitted by: Mohammad S Anwar +# +# Write a script to convert a given number (base 10) to quater-imaginary base +# number and vice-versa. For more informations, please checkout wiki page. +# +# For example, +# +# $number_base_10 = 4 +# $number_quater_imaginary_base = 10300 + +def dec_to_base2i(n): + base = -4 + result = "" + while n != 0: + i = n % base + n //= base + if i < 0: + i += abs(base) + n += 1 + result = "0" + str(i) + result # imaginary is zero + result = result.lstrip('0') + return result + +def base2i_to_dec(n2i): + base = -4 + result = 0 + digits = list(reversed(n2i)) + for i in range(len(digits)): + if i % 2 == 0: # real part + result += int(digits[i]) * (base ** (i // 2)) + elif digits[i] != '0': # imaginary part + raise ValueError(f"number {n2i} has imaginary component") + return result + +assert(dec_to_base2i(4) == '10300') +assert(base2i_to_dec('10300') == 4) diff --git a/challenge-178/paulo-custodio/python/ch-2.py b/challenge-178/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..e05c344ec8 --- /dev/null +++ b/challenge-178/paulo-custodio/python/ch-2.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +# Challenge 178 +# +# Task 2: Business Date +# Submitted by: Mohammad S Anwar +# +# You are given $timestamp (date with time) and $duration in hours. +# +# Write a script to find the time that occurs $duration business hours after +# $timestamp. For the sake of this task, let us assume the working hours is 9am +# to 6pm, Monday to Friday. Please ignore timezone too. +# +# For example, +# +# Suppose the given timestamp is 2022-08-01 10:30 and the duration is 4 hours. +# Then the next business date would be 2022-08-01 14:30. +# +# Similar if the given timestamp is 2022-08-01 17:00 and the duration is 3.5 hours. +# Then the next business date would be 2022-08-02 11:30. + +from datetime import datetime, timedelta +import sys + +def next_business_date(date_text, time_text, hours): + date_str = f"{date_text} {time_text}" + date = datetime.strptime(date_str, "%Y-%m-%d %H:%M") + + minutes = hours * 60 + while True: + end_day = date.replace(hour=18, minute=0) + remain = (end_day - date).total_seconds() / 60 + if minutes < remain: + date += timedelta(minutes=minutes) + return date + else: + minutes -= remain + date = end_day + timedelta(hours=(24 - 18) + 9) + while date.weekday() >= 5: # skip weekend + date += timedelta(days=1) + +if len(sys.argv) != 4: + raise ValueError("usage: ch-2.py yyyy-mm-dd HH:MM hours") + +result_date = next_business_date(sys.argv[1], sys.argv[2], float(sys.argv[3])) +print(result_date.strftime("%Y-%m-%d %H:%M")) diff --git a/challenge-178/paulo-custodio/t/test-1.yaml b/challenge-178/paulo-custodio/t/test-1.yaml index 5c2d74364b..6e4d99f6fb 100644 --- a/challenge-178/paulo-custodio/t/test-1.yaml +++ b/challenge-178/paulo-custodio/t/test-1.yaml @@ -2,7 +2,4 @@ cleanup: args: input: - output: | - |ok 1 - |ok 2 - |1..2 + output: |
