diff options
Diffstat (limited to 'challenge-178/paulo-custodio/python')
| -rw-r--r-- | challenge-178/paulo-custodio/python/ch-1.py | 41 | ||||
| -rw-r--r-- | challenge-178/paulo-custodio/python/ch-2.py | 46 |
2 files changed, 87 insertions, 0 deletions
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")) |
