Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. Write a function that draws a grid like the following: FA + Hint: to print more than one value on a line, you
1. Write a function that draws a grid like the following: FA + Hint: to print more than one value on a line, you can print a comma- separated sequence of values: print('+', '-') By default, print advances to the next line, but you can override that behavior and put a space at the end, like this: print('+', end=' ') print('_') The output of these statements is '+ -' on the same line. The output from the next print statement would begin on the next line. 2. Write a function that draws a similar grid with four rows and four columns. Let's have a look at the function called printString() defined below: def printString(): for index in 'Spam': print(index, end="") 1. Copy the function definition above into a Python program and call this function from the main part of the program to make sure it works. 2. Write a more general version of printString(): a function that takes a string as a parameter. In doing so, we are using the generalisation guideline to "decompose" this problem. What is being generalised? How are we generalising it? Then, following the composition guideline, create another function called printTwice() that prints the string twice by calling our new and more generalised version of printString(). The body of this function printTwice() must only have two statements + the return statement. 1. Define a new function called printFour() that takes a string as a parameter and prints this string four times. The body of this four (+ the return statement). In order to solve this riddle successfully, we must use the composition guideline again. 2. Can we generalise printFour() by creating a function (we will need to rename it) that prints a string x times? ction printFour() must only have two statements (+ the return statement), not Exercise 1 Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display. >>> right_justify( 'monty') monty Hint: Use string concatenation and repetition. Also, Python provides a built-in function called len that returns the length of a string, so the value of len('monty') is 5. Exercise 4 A number, a, is a power of b if it is divisible by b and a/b is a power of b. Write a function called is power that takes parameters a and b and returns True if a is a power of b. Note: you will have to think about the base case. Exercise 5 The greatest common divisor (GCD) of a and b is the largest number that divides both of them with no remainder. One way to find the GCD of two numbers is based on the observation that if r is the remainder when a is divided by b, then gcd(a, b) = gcd(b, r). As a base case, we can use gcd(a,0) = a. Write a function called gcd that takes parameters a and b and returns their greatest common divisor. Credit: This exercise is based on an example from Abelson and Sussman's Structure and Interpretation of Computer Programs.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started