1. Write a function that draws a grid like the following: FA + Hint: to print...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
Computation Theory ExplainShow that the function giving the next state of a register machine in terms of the current state is primitive recursive. (You may assume the existence of primitive recursive...
-
Table P-23 contains Southwest Airlines' quarterly income before extraordinary items ($MM) for the years 1988-1999. a. Plot the income data as a time series and describe any patterns that exist. b. Is...
-
At the beginning of the current golf season, on April 1, 2012, the general ledger of Thousand Oaks Golf Shop showed Cash $3,500; Merchandise Inventory $1,500; Common Shares $3,000; and Retained...
-
Studies have shown that young adults experience faster weight gain and consume more unhealthy food than any other age group. In a 2017 study published in PLOS One, Buchanan et. al examined the...
-
An engineer is to design a channel lined with planed wood to carry water at a flowrate of \(2 \mathrm{~m}^{3} / \mathrm{s}\) on a slope of \(10 \mathrm{~m} / 800 \mathrm{~m}\). The channel cross...
-
The following events occurred for Johnson Company: a. Received investment of $40,000 cash by organizers and distributed 1,000 shares of $1 par value common stock to them. b. Purchased $15,000 of...
-
What does the Performance Management (PM) process consist of? On what motivational theory is the PM process based on?
-
David, the owner of a local poster shop, comes to you for help. "We've only been breaking even the past two years, and I'm getting very frustrated! I don't know what to do because I feel like I've...
-
Consider a two-stage compression refrigeration system shown below. The working fluid is refrigerant- 134a. The refrigerant leaves the condenser as a saturated liquid and is throttled to a flash...
-
Select a population of interest and a health issue that affects this population for your Final Project. The purpose of this project is for you to explore how a specific health theory or model is used...
-
On December 1 , 2 0 1 5 , your company pays $ 2 4 , 0 0 0 for rent on your store that covers the next 6 months. When you paid the cash, you debited Prepaid Rent.. The adjusting journal entry on...
-
Precision Manufacturing Incorporated (PMI) makes two types of industrial component parts-the EX300 and the TX500. It annually produces 63,000 units of EX300 and 12,800 units of TX500. The company's...
-
A 9 Loan amount Annual interest rate Number of years until maturity Annual payment amount B $300,000.00 Date Graded Worksheet 7% 4 On January 1, 2024, a company borrows cash from a local bank by...
-
Please assume that the provided pessimistic and optimistic values in the problem are based on 99 percentile level. Please provide a table or tables that show the mean, std. dev., and variance for...
-
Gwen MacDonals works as an accountant for Sky High Condos, which is owned by Sam Huang. The business currently owns two buildings with 100 units in each. Sky High is in the process of expanding by...
-
What is a make-or-buy decision?
-
Using the linspace command, create a row vector (assign it to a variable named Nines) with nine elements that are all 9.
-
Solve the following system of five linear equations: 2.5a b + 3c + 1.5d - 2e = 57. 1 3a + 4b - 2c + 2.5d - e = 27.6 -4a + 3b + c - 6d + 2e = -81.2 2a + 3b + c - 2.5d + 4e = -22.2 a + 2b + 5c - 3d +...
-
Molecules of a gas in a container are moving around at different speeds. Maxwell's speed distribution law gives the probability distribution P(v) as a function of temperature and speed: where M is...
-
solve the given problem using PSO method \[f(X)=1200\left(2 x_{1} x_{3}+2 x_{2} x_{3} ight)+2500 x_{1} x_{2}+500\left(\frac{1000}{x_{1} x_{2} x_{3}} ight)+100\left(\frac{1000}{10 x_{1} x_{2} x_{3}}...
-
Solve the optimization problem using SA \[\min f(X)=100\left(x_{1}^{2}+x_{2} ight)^{2}+\left(1-x_{1} ight)^{2}\] subject to $X \in[0,7]$
-
Discuss the perturbation mechanism of simulated annealing.
Study smarter with the SolutionInn App