From 05f597b12c1518da66b104fca8875aaf6ad00bd9 Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Tue, 4 Jan 2022 14:37:22 -0300 Subject: [PATCH 1/6] Added Calendar first implementation it has some formatting problems --- 21_Calendar/python/calendar.py | 69 ++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 21_Calendar/python/calendar.py diff --git a/21_Calendar/python/calendar.py b/21_Calendar/python/calendar.py new file mode 100644 index 00000000..6f3cb191 --- /dev/null +++ b/21_Calendar/python/calendar.py @@ -0,0 +1,69 @@ + +def calendar(weekday, leap_year): + """ + function to print a year's calendar. + + input: + _weekday_: int - the initial day of the week (0=SUN, -1=MON, -2=TUES...) + _leap_year_: bool - indicates if the year is a leap year + """ + months_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + days = ' S M T W T F S' + sep = "*" * 59 + years_day = 365 + d = weekday + + if leap_year: + months_days[2] = 29 + years_day = 366 + + months_names = [" JANUARY ", + " FEBRUARY", + " MARCH ", + " APRIL ", + " MAY ", + " JUNE ", + " JULY ", + " AUGUST ", + "SEPTEMBER", + " OCTOBER ", + " NOVEMBER", + " DECEMBER"] + + print(" "*32 + "CALENDAR") + print(" "*15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") + + days_count = 0 # S in the original program + + # main loop + for n in range(1, 13): + days_count += months_days[n-1] + print("** {} ****************** {} ****************** {} **".format(days_count, + months_names[n-1], years_day-days_count)) + print(days) + print(sep) + print("\n") + + for w in range(1, 7): + print("\n") + for g in range(1, 8): + d += 1 + d2 = d - days_count + + if d2 > months_days[n]: + break + + if d2 > 0: + print("{}".format(d2),end=' ') + else: + print("{}".format(''),end=' ') + if d2 >= months_days[n]: + break + + if d2 > months_days[n]: + d -= g + + print("\n") + +if __name__ == "__main__": + calendar(-1, False) \ No newline at end of file From a8f0bb10c6fe8acfcfb5c5cb59fd35370292d566 Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Tue, 4 Jan 2022 15:10:08 -0300 Subject: [PATCH 2/6] formatting text updated --- 21_Calendar/python/calendar.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/21_Calendar/python/calendar.py b/21_Calendar/python/calendar.py index 6f3cb191..6c803771 100644 --- a/21_Calendar/python/calendar.py +++ b/21_Calendar/python/calendar.py @@ -8,7 +8,7 @@ def calendar(weekday, leap_year): _leap_year_: bool - indicates if the year is a leap year """ months_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] - days = ' S M T W T F S' + days = 'S M T W T F S\n' sep = "*" * 59 years_day = 365 d = weekday @@ -32,17 +32,16 @@ def calendar(weekday, leap_year): print(" "*32 + "CALENDAR") print(" "*15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - + print("\n"*11) days_count = 0 # S in the original program # main loop for n in range(1, 13): days_count += months_days[n-1] - print("** {} ****************** {} ****************** {} **".format(days_count, + print("** {} ****************** {} ****************** {} **\n".format(days_count, months_names[n-1], years_day-days_count)) print(days) print(sep) - print("\n") for w in range(1, 7): print("\n") @@ -54,9 +53,13 @@ def calendar(weekday, leap_year): break if d2 > 0: - print("{}".format(d2),end=' ') + if d2 < 10: + print(" {}".format(d2), end=' ') + else: + print("{}".format(d2), end=' ') else: - print("{}".format(''),end=' ') + print("{}".format(' '), end=' ') + if d2 >= months_days[n]: break @@ -65,5 +68,6 @@ def calendar(weekday, leap_year): print("\n") + if __name__ == "__main__": - calendar(-1, False) \ No newline at end of file + calendar(-6, False) From 873ca419e1846ab5e49e350f181a408875eb51fb Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Tue, 4 Jan 2022 15:12:41 -0300 Subject: [PATCH 3/6] formatting logic updated --- 21_Calendar/python/calendar.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/21_Calendar/python/calendar.py b/21_Calendar/python/calendar.py index 6c803771..c90b1b62 100644 --- a/21_Calendar/python/calendar.py +++ b/21_Calendar/python/calendar.py @@ -39,7 +39,7 @@ def calendar(weekday, leap_year): for n in range(1, 13): days_count += months_days[n-1] print("** {} ****************** {} ****************** {} **\n".format(days_count, - months_names[n-1], years_day-days_count)) + months_names[n-1], years_day-days_count)) print(days) print(sep) @@ -52,13 +52,12 @@ def calendar(weekday, leap_year): if d2 > months_days[n]: break - if d2 > 0: - if d2 < 10: - print(" {}".format(d2), end=' ') - else: - print("{}".format(d2), end=' ') - else: + if d2 <= 0: print("{}".format(' '), end=' ') + elif d2 < 10: + print(" {}".format(d2), end=' ') + else: + print("{}".format(d2), end=' ') if d2 >= months_days[n]: break From 6517975e250a0fa4f3d84936498877a5dd6a0f24 Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Tue, 4 Jan 2022 15:15:02 -0300 Subject: [PATCH 4/6] Updated formatting to look like the original program --- 21_Calendar/python/calendar.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/21_Calendar/python/calendar.py b/21_Calendar/python/calendar.py index c90b1b62..b59d09f4 100644 --- a/21_Calendar/python/calendar.py +++ b/21_Calendar/python/calendar.py @@ -58,6 +58,7 @@ def calendar(weekday, leap_year): print(" {}".format(d2), end=' ') else: print("{}".format(d2), end=' ') + print() if d2 >= months_days[n]: break @@ -67,6 +68,8 @@ def calendar(weekday, leap_year): print("\n") + print("\n") + if __name__ == "__main__": calendar(-6, False) From 9b44692b8ccaa3fca3dee78781b006ce67b660ec Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Tue, 4 Jan 2022 15:31:16 -0300 Subject: [PATCH 5/6] Added parse function for starting day of the week of the year and boolean to check if it's a leap year structured code --- 21_Calendar/python/calendar.py | 56 +++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/21_Calendar/python/calendar.py b/21_Calendar/python/calendar.py index b59d09f4..63c2741f 100644 --- a/21_Calendar/python/calendar.py +++ b/21_Calendar/python/calendar.py @@ -1,4 +1,46 @@ +def parse_input(): + """ + function to parse input for weekday and leap year boolean + """ + + days_mapping = { + "sunday": 0, + "monday": -1, + "tuesday": -2, + "wednesday": -3, + "thursday": -4, + "friday": -5, + "saturday": -6 + } + + day = 0 + leap_day = False + + correct_day_input = False + while not correct_day_input: + weekday = input("INSERT THE STARTING DAY OF THE WEEK OF THE YEAR:") + + for day_k in days_mapping.keys(): + if weekday.lower() in day_k: + day = days_mapping[day_k] + correct_day_input = True + break + + while True: + leap = input("IS IT A LEAP YEAR?:") + + if 'y' in leap.lower(): + leap_day = True + break + + if 'n' in leap.lower(): + leap_day = False + break + + return day, leap_day + + def calendar(weekday, leap_year): """ function to print a year's calendar. @@ -30,9 +72,6 @@ def calendar(weekday, leap_year): " NOVEMBER", " DECEMBER"] - print(" "*32 + "CALENDAR") - print(" "*15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") - print("\n"*11) days_count = 0 # S in the original program # main loop @@ -71,5 +110,14 @@ def calendar(weekday, leap_year): print("\n") +def main(): + print(" "*32 + "CALENDAR") + print(" "*15 + "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") + print("\n"*11) + + day, leap_year = parse_input() + calendar(day, leap_year) + + if __name__ == "__main__": - calendar(-6, False) + main() From f673a3740f0bac5552e9359f8a7072e759558c69 Mon Sep 17 00:00:00 2001 From: Alvaro Frias Garay Date: Tue, 4 Jan 2022 15:45:12 -0300 Subject: [PATCH 6/6] Added porting notes and original program commentary --- 21_Calendar/python/calendar.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/21_Calendar/python/calendar.py b/21_Calendar/python/calendar.py index 63c2741f..6f3f572b 100644 --- a/21_Calendar/python/calendar.py +++ b/21_Calendar/python/calendar.py @@ -1,3 +1,24 @@ +######################################################## +# Calendar +# +# From: BASIC Computer Games (1978) +# Edited by David Ahl# +# +# This program prints out a calendar +# for any year. You must specify the +# starting day of the week of the year in +# statement 130. (Sunday(0), Monday +# (-1), Tuesday(-2), etc.) You can determine +# this by using the program WEEKDAY. +# You must also make two changes +# for leap years in statement 360 and 620. +# The program listing describes the necessary +# changes. Running the program produces a +# nice 12-month calendar. +# The program was written by Geofrey +# Chase of the Abbey, Portsmouth, Rhode Island. +# +######################################################## def parse_input(): """ @@ -121,3 +142,15 @@ def main(): if __name__ == "__main__": main() + +######################################################## +# +######################################################## +# +# Porting notes: +# +# It has been added an input at the beginning of the +# program so the user can specify the first day of the +# week of the year and if the year is leap or not. +# +########################################################