Problem Set 0

Problem 0

Problem 0 (30 pts)

Write a function print_grid that takes an integer argument size and prints a number grid that conforms to the following pattern based on the value of size:

print_grid(7)

[ 7 8 9 ]
[ 6 7 8 ]
[ 5 6 7 ]
[ 4 5 6 ]
[ 3 4 5 ]

print_grid(5)

[ 5 6 7 ]
[ 4 5 6 ]
[ 3 4 5 ]
[ 2 3 4 ]
[ 1 2 3 ]

print_grid(4)

[ 4 5 6 ]
[ 3 4 5 ]
[ 2 3 4 ]
[ 1 2 3 ]
[ 0 1 2 ]

Use this starter code:

def print_grid(size):
    # your code goes here

print_grid(7)
print_grid(5)
print_grid(4)

Submit this program as grid_numbers.py.

If you are having difficulty solving (or simply approaching) a problem, start with a more simple sub-problem.

For instance, begin Problem 1 by figuring out how to print 7 8 9 using a loop:

for j in range(7, 10):
    print(j, end=" ")
7 8 9 

Next, figure out how to add the brackets [ and ]

print("[ ",end="")
for j in range(7, 10):
    print(j, end=" ")
print("]")
[ 7 8 9 ]

Continue to build a solution to the full problem by incrementally solving sub-problems.

Problem 1

Problem 1 (30 pts)

Write a program that defines a function called box, which produces a box comprised of + characters, of height size and width twice size.

For example, with size = 5, the output would be:

++++++++++
+        +
+        +
+        +
++++++++++

With size = 8, the output would be:

++++++++++++++++
+              +
+              +
+              +
+              +
+              +
+              +
++++++++++++++++

Use the following starter code, and turn in your program using this format.

def box(size):
    # your code goes here

box(4)

Submit this program as box.py.

Note: Pay careful attention to your output! Match the examples.

Problem 2

Problem 2 (20 pts)

A program intends to print the following:

"From an essay a student was reading on the RER, between 
Chatelet-les-Halles and Luxembourg, one sentence stood out:
"Truth is related to reality.""
- Annie Ernaux

"Other people's life stories are not a topic for debate.
One should hear them out, and reciprocate in the same coin." - Olga Tokarczuk

"I stumble on this phrase of George Sand's: 
"Poor workers or sick people, you must always struggle against those who tell you: 
'Work hard to live badly.'" " - Laurent Binet

Here is the draft program. It contains several bugs. Fix them.

print(""From an essay a student was reading on the RER,\n between Chatelet-les-Halles and Luxembourg, one setence stood out:\n\"Truth is related to reality."" - Annie Ernaux'')
print('"Other people's life stories are not a topic for debate.\n One should hear them out, and reciprocate in the same coin." - Olga Tokarczuk')
print("I stumble on this phrase of George Sand's: "Poor workers or sick people,\nyou must always struggle against those who tell you:\n 'Work hard to live badly.'" " - Laurent Binet")

Submit this program as quotations.py.

Problem 3

Problem 3 (20 pts)

A program intends to print the following:

18th
19th
20th
21st
22nd
23rd
24th

Here is the draft program. It contains several bugs. Fix them.

k = 26
j = 18
while j < k:
    print(j)
    print(th)

All printing for the program should be performed inside of the while loop.

Submit this program as ordinals.py.

Problem 4

Problem 4 (10 pts)

Write a program similar to the program in Problem 4, that prints every ordinal number between 1st and 101st, each on a new line:

Your output will start:

1st
2nd
3rd
4th
5th
6th

and end:

97th
98th
99th
100th
101st

The final program should use only one loop.

Submit this program as moredinals.py.

Problem 5

Problem 5 (10 pts)

Write a function fizzbuzz that takes as input an integer and prints:

  • The integer, if the integer is not evenly divisible by 5 or by 3
  • String 'fizz' if the integer is evenly divisible by 3
  • String 'buzz' if the integer is evenly divisible by 5

Here is the draft program. Complete it.

def fizzbuzz(input_int)
    print(input_int)

# these are tests, keep them to check if your code works
fizzbuzz(2)
fizzbuzz(3)
fizzbuzz(4)
fizzbuzz(5)
fizzbuzz(6)
fizzbuzz(7)

The output of the tests included in the starter code should be:

2
fizz
4
buzz
fizz
7

Your code should work for inputs between 1 and 14, inclusive. Submit this program as fizzbuzz_func.py.