Program of the Day #8

Write a program that prints a month of a calendar, formatted as in the example below. The program takes two command line arguments. The first is an integer number of days in the month (you can assume this will be 28, 29, 30, or 31). The second is a number between 0 and 6 representing the day of the week that the first day of the month falls on, 0 being Sunday, 1 being Monday, and so on up to 6 for Saturday. Your program can rely on the number of days being between 28–31 (inclusive) and the day of the week being 0–6 (inclusive). The following example prints a calendar for October of 2025, which has 31 days and starts on a Tuesday.

>>> %Run P08_calendar.py 31 2
su mo tu we th fr sa
       1  2  3  4  5 
 6  7  8  9 10 11 12 
13 14 15 16 17 18 19 
20 21 22 23 24 25 26 
27 28 29 30 31 
>>>

To pass the tests, the formatting must match the above example exactly, except that any amount of trailing whitespace (e.g., one or more spaces) at the end of each line of output will be ignored.

Other Practice Problems

  1. What does the following program print?

    for value in [1, 16, 4]:
        print(value, end=" ")
        value = value * 10

    Hypothesize an answer, then try it out in Thonny to make sure you’re right. Make sure you understand why this loop behaves the way it does.

  2. FizzBuzz The following problem is a commonly-used interview question for programming jobs: Write a program that prints the numbers from 1 to 100. But for multiples of three print Fizz instead of the number and for the multiples of five print Buzz. For numbers which are multiples of both three and five print FizzBuzz. The first few lines of the output look like this:

    1
    2
    Fizz
    4
    Buzz
    Fizz
  3. Recall this program, which we saw in last lecture’s Exercises. Write a program that uses for loops to print the same output.

    i = 0
    while i < 4:
      j = 1
      while j < 11:
        print('*', end='')
        j += 1
      i += 1
      print()
  4. How many elements are in the range range(a, b, c)? Assume a <= b and c > 0, and write your answer in terms of a, b, and c.