Assignment 2
Assignment 2
1. A store charges $12 per item if you buy less than 10 items. If you buy between 10 and
99 items, the cost is $10 per item. If you buy 100 or more items, the cost is $7 per
item. Write a program that asks the user how many items they are buying and prints
the total cost.
2. A jar of Halloween candy contains an unknown amount of candy and if you can guess
exactly how much candy is in the bowl, then you win all the candy. You ask the
person in charge the following: If the candy is divided evenly among 5 people, how
many pieces would be left over? The answer is 2 pieces. You then ask about dividing
the candy evenly among 6 people, and the amount left over is 3 pieces. Finally, you
ask about dividing the candy evenly among 7 people, and the amount left over is 2
pieces. By looking at the bowl, you can tell that there are less than 200 pieces. Write
a program to determine how many pieces are in the bowl
3. At a certain school, student email addresses end with @student.college.edu, while pro-
fessor email addresses end with @prof.college.edu. Write a program that first asks the
user how many email addresses the user will be entering, and then have the user enter
those addresses. After all the email addresses are entered, the program should print
out a message indicating either that all the addresses are student addresses or that
there were some professor addresses entered.
5. When playing games where you have to roll two dice, it is nice to know the odds of
each roll. For instance, the odds of rolling a 12 are about 3%, and the odds of rolling
a 7 are about 17%. You can compute these mathematically, but if you don’t know the
math, you can write a program to do it. To do this, your program should simulate
rolling two dice about 10,000 times and compute and print out the percentage of rolls
that come out to be 2, 3, 4, . . . , 12.
6. A simple way to estimate the number of words in a string is to count the number of
spaces in the string. Write a program that asks the user for a string and returns an
estimate of how many words are in the string.
7. Write a program that asks the user to enter a word and prints out whether that word
contains any vowels.
8. Write a program that asks the user to enter a string s and then converts s to lowercase,
removes all the periods and commas from s, and prints the resulting string
1
9. Write a program that asks the user to enter a word that contains the letter a. The
program should then print the following two lines: On the first line should be the part
of the string up to and including the the first a, and on the second line should be the
rest of the string. Sample output is shown below:
10. The goal of this exercise is to see if you can mimic the behavior of the in operator and
the count and index methods using only variables, for loops, and if statements.
(a) Without using the in operator, write a program that asks the user for a string
and a letter and prints out whether or not the letter appears in the string.
(b) Without using the count method, write a program that asks the user for a string
and a letter and counts how many occurrences there are of the letter in the string.
(c) Without using the index method, write a program that asks the user for a string
and a letter and prints out the index of the first occurrence of the letter in the
string. If the letter is not in the string, the program should say so.
11. Write a program that asks the user to enter a list of integers. Do the following:
12. Write a program that takes any two lists L and M of the same size and adds their
elements together to form a new list N whose elements are sums of the corresponding
elements in L and M. For instance, if L=[3,1,4] and M=[1,5,9], then N should equal
[4,6,13].
13. There is a provably unbreakable cipher called a one-time pad. The way it works is you
shift each character of the message by a random amount between 1 and 26 characters,
wrapping around the alphabet if necessary. For instance, if the current character is y
and the shift is 5, then the new character is d. Each character gets its own shift, so
there needs to be as many random shifts as there are characters in the message. As an
example, suppose the user enters secret. The program should generate a random shift
between 1 and 26 for each character. Suppose the randomly generated shifts are 1, 3,
2, 10, 8, and 2. The encrypted message would be thebmv.
2
Submission
Submit only 1-5 questions. Questions 6-13 are for your practice. Write code for each question
in a separate file, for example ’Q1.py’ and zip all files in a folder named with your name and
upload final submission on LMS latest by Friday 26, 2021 at 11:55 PM. You may submit
late with 20% deduction per day.