Goal:
This project is designed reinforce the techniques around the following: * Usingwhile
loops * Usingprint
statements * Usingif
statements
Description:
The program will print nicely formatted tables of numbers. Those numbers will represent integers raised to integer powers between 2 and a chosen upper limit. For instance, the table for the integers up to 4 raised to the powers 1..3 could look like this:
---------- | 1| 1| 1| | 2| 4| 8| | 3| 9|27| | 4|16|64| ==========
Functional Requirements:
This program will be made up of exactly four user-defined functions:
repeated(char, N)
The functionrepeated
takes two parameters: a string,char
, and an integer,N
.
repeated
must meet the following requirements: * It returnsN
repetitions ofchar
. * It mustnotuse the string repetition operator (*
). * It must not use afor
loop to implementrepeated
.
These assertions, while not required, may help you:
assert repeated('%', 0) == ''
assert repeated('*', 2) == '**'
assert repeated('$', 4) == '$$$$'
padded(x, width)
The functionpadded
takes two arguments: an integer,x
, and another integer,width
.
padded
must meet the following requirements: * It returns a string of exactlywidth
characters. * It must userepeated
for any string repetition needs. * Ifx
is too wide to fit in a string ofwidth
characters, then it should return a string of just asterisks.
These assertions, while not required, may help you:
assert padded(3, 1) == '3'
assert padded(4, 2) == ' 4'
assert padded(12, 4) == ' 12'
assert padded(123, 2) == '**'
power(base, exponent)
The functionpower
takes two arguments: an integer,base
, and another integer,exponent
.
power
must meet the following requirements: * It must return the integer value ofbase
raised to the power ofexponent
. * It must compute this value using a loop. Using the**
operator is not allowed. * It maynotassume thatexponent
is non-negative. If it is negative,power
should return -1.
These assertions, while not required, may help you:
assert power(3, 0) == 1
assert power(4, 2) == 16
assert power(3, 4) == 81
assert power(3, -3) == -1
print_row(n, max_power, column_width)
The functionprint_row
takes three integer arguments and prints a string. It does not return anything.
The arguments:
n
is the number that is going to be raised to powers in this row of output.
max_power
represent the highest power to whichn
will be raised.
column_width
is the number of characters to be used to hold computed values.
print_row
must meet the following requirements: * It must printmax_power
columns of output. * It prints a single row. * All columns arewidth
wide. * All columns are separated with a|
character. * The first and last characters are also the|
character. * The first column isn
raised to the first power, the second column isn
raised to the second power, etc. * You must use thepower
function to compute the appropriate values. You must not use the**
operator.
Here are some samples:
print_row(4,2,3)
will print
| 4| 16|
print_row(5,3,4)
will print
| 5| 25| 125|
print_table(max_value, max_power, column_width)
print_table
has 3 integer arguments: 1.max_value
represents the highest value for which the table is going to print a row. 2.max_power
represents the maximum power to which any value is raised. 3.column_width
represents the width of each column of output (exclusive of the|
borders).
print_table
must meet the following requirements: * It must useprint_row
to print the numeric rows of the table. * It must print an upper and lower border that is the same width as the table. * The upper border is made of-
characters. * The lower border is made of=
characters. * It should userepeated
as appropriate. It shouldnotuse the string repetition operator (*
). * Where it requires looping, it should use awhile
loop. It shouldnotuse afor
loop.
Here are some samples:
print_table(4, 3, 2)
would print
---------- | 1| 1| 1| | 2| 4| 8| | 3| 9|27| | 4|16|64| ==========
print_table(6, 4, 2)
would print
------------- | 1| 1| 1| 1| | 2| 4| 8|16| | 3| 9|27|81| | 4|16|64|**| | 5|25|**|**| | 6|36|**|**| =============
main()
Use thismain
function, and include the final call tomain()
def main():
print_table(4, 3, 2)
print_table(6, 4, 2)
main()
Advice:
Write the functions in the order they were presented above.
Forbidden:
Your program may not use any techniques or Python features forbidden above. Do not use any string-related functionality other than the following: * Concatenation (+
) *len(s)
to determine the length ofs
*str(x)
to convert an integer to a string.