diff options
| author | David Ferrone <zapwai@gmail.com> | 2024-03-04 11:33:02 -0500 |
|---|---|---|
| committer | David Ferrone <zapwai@gmail.com> | 2024-03-04 11:33:02 -0500 |
| commit | 29dfd30903b2aa7ed1fa7d90d52425841e0ddaa0 (patch) | |
| tree | f99efe1fd15d52cf1823cef6af58ac58f9775188 /challenge-259/zapwai/python | |
| parent | 317868d8b68301864db17e9dac3f9b15ffa8b448 (diff) | |
| download | perlweeklychallenge-club-29dfd30903b2aa7ed1fa7d90d52425841e0ddaa0.tar.gz perlweeklychallenge-club-29dfd30903b2aa7ed1fa7d90d52425841e0ddaa0.tar.bz2 perlweeklychallenge-club-29dfd30903b2aa7ed1fa7d90d52425841e0ddaa0.zip | |
Week 259
Diffstat (limited to 'challenge-259/zapwai/python')
| -rw-r--r-- | challenge-259/zapwai/python/ch-1.py | 72 | ||||
| -rw-r--r-- | challenge-259/zapwai/python/ch-2.py | 38 |
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-259/zapwai/python/ch-1.py b/challenge-259/zapwai/python/ch-1.py new file mode 100644 index 0000000000..502f126cbb --- /dev/null +++ b/challenge-259/zapwai/python/ch-1.py @@ -0,0 +1,72 @@ +base_year = 2000 +days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + +def is_leap_year(year): + if year % 400 == 0: + return True + if year % 100 == 0: + return False + if year % 4 == 0: + return True + return False + +def weekday(year, month, day): + year = int(year); month = int(month); day = int(day) + skip_days = year - base_year + for yr in range(base_year, year): + if is_leap_year(yr): + skip_days += 1 + if is_leap_year(year) and month > 2: + skip_days += 1 + for m in range(1, 13): + if m < month: + skip_days += days_in_month[m - 1] + skip_days += int(day) - 1 + days = ["sat", "sun", "mon", "tue", "wed", "thu", "fri"] + return days[skip_days % 7] + +def is_weekend(year, month, day): + today = weekday(year, month, day) + if today == "sat" or today == "sun": + return True + else: + return False + +def is_holiday(bank_holidays, year, month, day): + for date in bank_holidays: + (hol_year, hol_month, hol_day) = date.split("-") + if year == hol_year and month == hol_month and day == hol_day: + return True + return False + +def proc(start_date, offset, bank_holidays): + (year, month, day) = start_date.split("-") + (new_year, new_month, new_day) = (year, month, day) + steps = offset + while steps > 0: + leap = 0 + if is_leap_year(int(year)) and month == "02": + leap = 1 + if int(new_day) + 1 > leap + days_in_month[int(new_month)-1]: + if new_month == "12": + new_year = str(int(new_year) + 1) + new_month = '{:02}'.format(int(new_month) + 1) + if new_month == "13": + new_month = "01" + new_day = "01" + else: + new_day = '{:02}'.format(int(new_day) + 1) + if not (is_holiday(bank_holidays, new_year, new_month, new_day) or is_weekend(new_year, new_month, new_day)): + steps -= 1 + new_date = new_year + "-" + new_month + "-" + new_day + print("Input:", start_date, "Offset:", offset, "Holidays: ", end='') + for d in bank_holidays: + print(d,'', end='') + print("\nOutput:", new_date) + +start_date = '2018-06-28' +offset = 3 +bank_holidays = ["2018-07-03"] + +proc(start_date, offset, bank_holidays) + diff --git a/challenge-259/zapwai/python/ch-2.py b/challenge-259/zapwai/python/ch-2.py new file mode 100644 index 0000000000..663034efc7 --- /dev/null +++ b/challenge-259/zapwai/python/ch-2.py @@ -0,0 +1,38 @@ +def proc(line): + if line[0] == '{': + line = line[3:-3] + name = line.split("=")[0].split()[0] + if name[0:3] == "end": + return + line = line[len(name)+1:] + field = [] + chunk = line.split("=") + for i in range(len(chunk) - 1): + field.append(chunk[i].split().pop()) + value = [] + for i in range(1, len(chunk) - 1): + s = chunk[i][:-len(field[i])] + value.append(s) + value.append(chunk[len(chunk)-1]) + print("Name =>", name) + print("Fields =>") + for i in range(len(field)): + print("\t",field[i],"->",value[i]) + print() + else: + print("Text =>", line) + + +line1 = '{% id field1="value1" field2="value2" field3=42 %}' +line2 = '{% youtube title="Title \"quoted\" done" foo="bar" baz=31 %}' +line3 = '{% youtube title="Title with escaped backslash \\" %}' +line4 = '{% id field="val1" field2="val2" %}' +line5 = '{% test field1="value1" df=42 %}' +line6 = 'LINES' +line7 = 'More Lines' +line8 = '{% endtest %}' + +lines = [line1, line2, line3, line4, line5, line6, line7, line8] + +for line in lines: + proc(line) |
