This article provides a sample solution on Generating Calendar in Python Plus other Homework Solutions.
Here's What You'll Learn
TogglePermalink: https://thestudycorp.com/essay-examples/generating-calendar-in-python
As you continue, thestudycorp.com has the top and most qualified writers to help with any of your assignments. All you need to do is place an order with us. (Generating Calendar in Python)
Solution
Homework 1: Generate Calendar
<pre>def calendar(n, s):
#this will convert string value to a corresponding integer
if s == “Su”:
d = 0
elif s == “Mo”:
d = 1
elif s == “Tu”:
d = 2
elif s == “We”:
d = 3
elif s == “Th”:
d = 4
elif s == “Fr”:
d = 5
elif s == “Sa”:
d = 6
print (“Su Mo Tu We Th Fr Sa”)
for j in range(d):
print (” “, end = ” “),
i = 1
while i <= n:
if i < 10:
print (“”,i,end=” “),
else:
print(i,end=” “),
if(i + d) % 7 == 0:
print (” “)
i = i + 1
n = int(input(“Input the number of days in the month (28-31): “))
s = input(“Input the starting day (Su=Sun, Mo=Mon,…): “)
calendar(n,s)</pre>
1b) Year 2020 Calendar
# Program to display 2020 calendar and User to enter the specific month to display
# importing calendar module
import calendar
yy = 2020 # year
# To take month input from the user
mm= int(input(“Enter year: “))
# display the calendar
print(calendar.month(yy, mm))
1c) A short program for January 2020 Calendar
# Program to display Jan 2020 calendar
# importing calendar module
import calendar
yy = 2020 # year
mm = 1 #month
# the header will be January 2020
print(calendar.month(yy, mm))
2, loop over 30 days of September
def date_iter(year, month):
for i in range(1, calendar.monthlen(year, month) + 1):
yield date(year, month, i)
for d in date_iter(2020, 9):
print(d)
As you continue, thestudycorp.com has the top and most qualified writers to help with any of your assignments. All you need to do is place an order with us. (Generating Calendar in Python)
b) Genialize your calendar to 100 year
days = [“su”, “mo”, “tu”, “we”, “th”, “fr”, “sa”]
months = [“January”, “February”, “March”, “April”, “May”,
“June”, “July”, “August”, “September”, “October”, “November”, “December”]
old_length_s = 0
length_current_month = 0
month_header = “”
title = “”
data = dict()
for year in range(startYear, endYear):
s = “|”
# data[year] = dict()
for month in months:
old_length_s = len(s)
s += ” “
# data[year][month] = dict()
cal_data = subprocess.Popen(“cal ” + str(month) +
” ” + str(year),shell=True, stdout=subprocess.PIPE)
week_loop = 0
for line in iter(cal_data.stdout.readline, b””):
if week_loop > 1: #this is after month name and day_names
char_num = 0
day = 0
date = “”
for c in line:
date += c
if char_num > 1: #every three characters
char_num = 0
if len(date.strip()) > 0: #somefields are blank in each month’s first week
# data[year][month][int(date)] = days[day]
if days[day] is “su”:
if int(date) < 10:
s += ” * “
else:
s += ” * “
else:
s += ” ” + str(date.strip()) + ” “
date = “”
day += 1
else:
char_num += 1
week_loop += 1
# print len(s)
if month is “February” and len(s) is 223:
s += ” “
s += ” |”
if first_year is True:
length_current_month = len(s) – old_length_s
blankspace = (length_current_month/2) – len(month)/2
if len(month)%2 is not 0:
month_header += ” “*blankspace + month + ” “*(blankspace-1)
As you continue, thestudycorp.com has the top and most qualified writers to help with any of your assignments. All you need to do is place an order with us. (Generating Calendar in Python)
else:
month_header += ” “*blankspace + month + ” “*blankspace
if month is “June”:
s += “| ” + str(year) + ” ||”
month_header += ” “
actual_title = str(startYear) + ” to ” + str(endYear-1)
title = ” “*((len(s)-7)-(len(actual_title)/2 -1 )) + actual_title
# print s
if first_year is True:
first_year = False
print(title)
print(“\n”*5)
print(month_header)
print(“\n”*3)
for c in s:
if c is not “|”:
dividing_line += “-“
decade_divider += “=”
else:
dividing_line += c
decade_divider += c
if str(year)[-1] is “0”:
print(decade_divider)
print(“\n”)
print(decade_divider)
else:
print(dividing_line)
print(“\n”)
print(s)
print(“\n”)
print(dividing_line)
print(“\n”*2)
print(“* = sunday”)
print(“\n”*2)
writer.close()
def Main():
startYear = int(sys.argv[1])
year_interval = int(sys.argv[2])
endYear = startYear + year_interval
getDaysData(startYear, endYear)