Assignment 1
Assignment 1
Total marks: 30
Instructions:
Please do your own work; plagiarized code will result in zero marks.
Do all these ask tasks in one python file named as your roll number followed by
your section e.g. 20-13456-c.py
Please add comments in your codes
Submit your file on Moodle before deadline.
Late assignments will not be accepted.
Do not forget to write DOC STRINGS.
Task#1:
The following pseudocode describes how a bookstore computes the price of an order from the total price and the
number of the books that were ordered.
Read the total book price and the number of books.
Compute the tax (7.5 percent of the total book price).
Compute the shipping charge ($2 per book).
The price of the order is the sum of the total book price, the tax, and the shipping charge.
Print the price of the order.
Translate this pseudocode into a Python Pogram.
Task#2:
The following pseudocode describes how to turn a string containing a ten-digit phone number (such as
"4155551212") into a more readable string with parentheses and dashes, like this: "(415) 555-1212".
Take the string consisting of the first three characters and surround it with "(" and ") ". This is the
area code.
Concatenate the area code, the string consisting of the next three characters, a hyphen, and the string
consisting of the last four characters. This is the formatted number.
Translate this pseudocode into a Python program that reads a telephone number into a string variable, computes
the formatted number, and prints it.
Task#3:
Calculating the tip when you go to a restaurant is not difficult, but your restaurant wants to suggest a tip according
to the service diners receive. Write a program that calculates a tip according to the diner’s satisfaction as follows:
Ask for the diners’ satisfaction level using these ratings: 1 = Totally satisfied, 2 = Satisfied, 3 =
Dissatisfied.
If the diner is totally satisfied, calculate a 20 percent tip.
If the diner is satisfied, calculate a 15 percent tip.
If the diner is dissatisfied, calculate a 10 percent tip.
Report the satisfaction level and tip in dollars and cents.
Task#4:
Write a program that prompts for the day and month of the user’s birthday and then prints a horoscope. Make up
fortunes for programmers, like this:
Please enter your birthday.
month: 6
day: 16
Gemini are experts at figuring out the behavior of complicated programs. You feel where bugs are coming from and then stay one step ahead. Tonight,
your style wins approval from a tough critic.
Each fortune should contain the name of the astrological sign. (You will find the sign names and date ranges at a
distressingly large number of sites on the Internet.)
Task#5:
Write an application to pre-sell a limited number of cinema tickets. Each buyer can buy as many as 4 tickets. No
more than 100 tickets can be sold. Implement a program that prompts the user for the desired number of tickets
and then displays the number of remaining tickets. Repeat until all tickets have been sold, and then display the
total number of buyers.
Task#6:
Write a program that reads an integer and displays, using asterisks, a filled diamond of the given side length. For
example, if the side length is 4, the program should display
Task#7:
The game of Nim. This is a well-known game with a number of variants. The following variant has an interesting
winning strategy. Two players alternately take marbles from a pile. In each move, a player chooses how many
marbles to take. The player must take at least one but at most half of the marbles. Then the other player
takes a turn. The player who takes the last marble loses.
Write a program in which the computer plays against a human opponent. Generate a random integer between 10
and 100 to denote the initial size of the pile. Generate a random integer between 0 and 1 to decide whether the
computer or the human takes the first turn. Generate a random integer between 0 and 1 to decide whether the
computer plays smart or stupid. In stupid mode the computer simply takes a random legal value (between 1 and n /
2) from the pile whenever it has a turn. In smart mode the computer takes off enough marbles to make the size of
the pile a power of two minus 1—that is, 3, 7, 15, 31, or 63. That is always a legal move, except when the size
of the pile is currently one less than a power of two. In that case, the computer makes a random legal move.
You will note that the computer cannot be beaten in smart mode when it has the first move, unless the pile size
happens to be 15, 31, or 63. Of course, a human player who has the first turn and knows the winning strategy can
win against the computer.