During this activity, students should be able to:
for
and while
loops, as well as string operations.
This activity helps students develop the following skills, values and attitudes: proficiency in English, ability to analyze and synthesize, capacity to identify and solve problems, and efficient use of computer systems.
Individually or in pairs, solve the following set of problems using Python 3.4. Run and test each of your programs to make sure they work as expected.
Each source file must include at the top the authors’ personal information (student ID and name) within comments. For example:
# Authors: # A01166611 Pepper Pots # A01160611 Anthony Stark # # Description of problem being solved. # # March 11, 2015. . . (The rest of the program goes here) .
Write a program called numbers.py
. This program should request the user to input an arbitrary amount of integer numbers. The program should stop requesting more input when the user types a zero. The program should then print how many numbers were typed, and the sum and average of all those numbers (without considering the zero). You do not need to define any functions other than main()
.
Example (user input is in blue):
Input an integer number (0 to quit): 16 Input an integer number (0 to quit): 4 Input an integer number (0 to quit): 42 Input an integer number (0 to quit): 8 Input an integer number (0 to quit): 23 Input an integer number (0 to quit): 15 Input an integer number (0 to quit): 0 6 numbers were typed. The sum of all those numbers is: 108 The average of all those number is: 18.0
Write a program called guess.py
. The program should pick a random integer number n from 1 to 100. It should then allow the user to guess this number. If the user’s guess is greater or less than n the program should display a message saying so, and then request the user for a new guess; but if the user’s guess is equal to n, the program should end displaying how many tries it took the user to guess correctly. You do not need to define any functions other than main()
.
Example (user input is in blue):
I'm thinking of a number between 1 and 100. Try to guess what number it is. What's your guess? 50 Your guess is too low. What's your guess? 75 Your guess is too low. What's your guess? 90 Your guess is too high. What's your guess? 85 Your guess is too low. What's your guess? 87 Your guess is too low. What's your guess? 88 Correct! It only took you 6 tries!
Write a program called invert.py
. Define in this program a function called invert(n)
that returns an integer comprised of the same digits contained in n
but in reversed order. For example, invert(2015)
should return the number 5102
. Do not use any string operations to solve this problem.
Test your program with the following main()
function:
def main(): print(invert(2015)) print(invert(123456789)) print(invert(1000)) print(invert(9999)) print(invert(1234) + 1)
The expected program output should be:
5102 987654321 1 9999 4322
Note that invert(1000)
is 1
, because any leading zeros in 0001
do not count and therefore are not printed.
According to Wikipedia: “a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself”. For example, 28 is a perfect number because the proper divisors of 28 are: 1, 2, 4, 7, and 14; and 1 + 2 + 4 + 7 + 14 = 28.
Write a program called perfect.py
. Define in this program a function called perfect(n)
that returns True
if n
is a perfect number, otherwise returns False
.
Test your program with the following main()
function:
def main(): for i in range(1, 10000): if perfect(i): print(i)
The expected program output should be:
6 28 496 8128
Write a program called reduce.py
. Define in this program a function called reduce(n)
that reduces the value of n
. The reduction consists in adding up all individual digits of the given number and repeating this process until you get a one digit number. For example, reduce(897)
would first add 8 + 9 + 7 = 24; because 24 is not a one digit number the process has to be repeated: 2 + 4 = 6; given that 6 is a one digit number, that’s the result that should be returned by the reduce()
function.
Test your program with the following main()
function:
def main(): print(reduce(897)) print(reduce(67521)) print(reduce(10000)) print(reduce(9999999999999)) print(reduce(123456789123456789123456789123456789))
The expected program output should be:
6 3 1 9 9
Write a program called periods.py
. Define in this program a funtion called periods(starting, target, interest)
that returns the number of investment periods required for the starting
balance to have grown equal or larger than the target
balance given an interest
rate (5% should be specified as 0.05). Although this can be computed directly mathematically, you should use a while
loop to solve this problem.
For example, periods(200, 250, 0.05)
returns 5, because that’s the number of periods it will take to get to 250 (or more) starting with 200 with an 5% interest rate, as can be observed in the following table:
Period # | Balance |
---|---|
0 | 200 |
1 | 210 |
2 | 220.5 |
3 | 231.525 |
4 | 243.10125 |
5 | 255.2563125 |
We stop at period 5 because 255.2563125 is greater or equal than the 250 target.
Test your program with the following main()
function:
def main(): print(periods(200, 250, 0.05)) print(periods(15000, 14000, 0.07)) print(periods(15000, 30000, 0.05)) print(periods(15000, 30000, 0.07)) print(periods(15000, 30000, 0.10)) print(periods(15000, 30000, 1.00))
The expected program output should be:
5 0 15 11 8 1
The food chain company Juan In A Million has certain rules to generate their employees’ user names required for their internal information systems. Assuming that every employee has a first, middle and last name (for example: Scarlett Ingrid Johansson), these rules are as follows:
So, the user name for employee Scarlett Ingrid Johansson should be: snjohans
.
Write a program called username.py
. Define in this program a function called username(first, middle, last)
that takes the first
name, middle
name, and last
name of a person and generates her user name given the above rules. You may assume that every first name has at least one letter and every middle name has at least one consonant. Use string.lower()
to convert string
into lower case letters.
Test your program with the following main()
function:
def main(): print(username('Scarlett', 'Ingrid', 'Johansson')) print(username('Donald', 'Ervin', 'Knuth')) print(username('Alan', 'Mathison', 'Turing')) print(username('Martin', 'Luther', 'King')) print(username('Stephen', 'William', 'Hawking')) print(username('Alejandro', 'Gonzalez', 'Inarritu'))
The expected program output should be:
snjohans drknuth amturing mlking swhawkin aginarri
Write a program called duplicate.py
. Define in this program a function called duplicate(s)
that returns a new string in which every character of s
is duplicated. For example, duplicate("Hello")
should return "HHeelllloo"
.
Test your program with the following main()
function:
def main(): print(duplicate("Hello")) print(duplicate("")) print(duplicate("Programming is fun!")) print(duplicate("THE END"))
The expected program output should be:
HHeelllloo PPrrooggrraammmmiinngg iiss ffuunn!! TTHHEE EENNDD
A palindrome is a word or sequence that reads the same backward as forward. For example: ana, civic, or madam.
Write a program called palindrome.py
. Define in this program a function called palindrome(s)
that returns True
if s
is a palindrome, otherwise returns False
.
def main(): print(palindrome("ana")) print(palindrome("civic") and palindrome("madam")) print(palindrome("sexes madam sexes")) print(palindrome("what is this?")) print(palindrome("not a palindrome")) print(palindrome("annas") or palindrome("caiaphas"))
The expected program output should be:
True True True False False False
(This is problem 13.6 from [JOHNSON]) Write a program called encode.py
. Define in this program a function called encode(msg)
that returns a string containing
the ASCII codes of each character in msg
separated by spaces. For example, encode("ABC")
should return the string "65 66 67"
.
Test your program with the following main()
function:
def main(): print(encode("ABC")) print(encode("")) print(encode("Hello World!")) print(encode("Programming is fun!")) print(encode("THE END"))
The expected program output should be:
65 66 67 72 101 108 108 111 32 87 111 114 108 100 33 80 114 111 103 114 97 109 109 105 110 103 32 105 115 32 102 117 110 33 84 72 69 32 69 78 68
Create a ZIP file called loop_strings.zip
containing only the ten programs you wrote (numbers.py
, guess.py
, invert.py
, perfect.py
, reduce.py
, periods.py
, username.py
, duplicate.py
, palindrome.py
and encode.py
).
To deliver the loop_strings.zip
file, please provide the following information:
If this activity was developed by a team of two people, only one person is required to deliver it. No activity will be accepted through e-mail or any other means.
Due date is Wednesday, March 11, all day up to midnight.
This activity will be evaluated using the following criteria:
-10 | One or more programs don’t contain within comments the authors’ personal information. |
---|---|
DA | One or more programs were plagiarized. |
10-100 | Depending on the amount of problems that were solved correctly. |