Assignment 3
Assignment 3
1. A common task while writing any software is to display a menu and ask the user for a choice. One
such example is the menu on your cellphone. It has messaging, contacts, games, settings, media,
and web (and possibly others) as options. Write a function called display menu that displays the
menu to the user and allows the user to make a choice (using input).
Hint: Make a function Display_menu() displaying all the features of cellphone and takes the input
a. Message
b. Contact #It will add or delete the contact information from the dictionary
c. Games
Choose the options
a
Recipient name: Rohan
Text: Hello
#it will display
Rohan:
Hello
2. Suppose you are purchasing something online on the Internet. At the website, you get a 10%
discount if you are a member. Additionally, you are also getting a discount of 5% on the item
because its Father’s Day. Write a function that takes as input the cost of the item that you are
purchasing and a Boolean variable indicating whether you are a member (or not), applies the
discounts appropriately, and returns the final discounted value of the item.
Note: The cost of the item need not be an integer.
3. Given a list of N numbers, write a function to shift the numbers circularly by some integer k
(where k < N). The function should take a list and k as a arguments and return the shifted list.
Hint:
original list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
shifted by 4, to the left: [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
shifted by 4, to the right: [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
4. Write a function for checking the speed of drivers. This function should have one parameter: speed.
(a) If speed is less than 70, it should print “Ok”.
(b) Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point
and print the total number of demerit points. For example, if the speed is 80, it should print:
“Points: 2”.
(c) If the driver gets more than 12 points, the function should print: “License suspended”
5. Write a function that returns the sum of multiples of 3,5 and 7 between 0 and limit (parameter). For
example, if limit is 20, it should return the sum of 3, 5, 6,7, 9, 10, 12,14, 15, 18, 20.
6. 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('allen')
Allen
7. Write a function to check, if a given integer num is a Curzon number. If 1 plus 2 elevated to num is
exactly divisible by 1 plus 2 multiplied by num, then num is a Curzon number.
Given a non-negative integer num, implement a function that returns True if num is a Curzon
number, or False otherwise.
Hint:
curzon(5) ➞ True
# 2 ** 5 + 1 = 33
# 2 * 5 + 1 = 11
# 33 is a multiple of 11
8. Imagine a circle and two squares: a smaller and a bigger one. For the smaller one, the circle is a
circumcircle and for the bigger one, an incircle. Create a function, that takes an integer (radius of
the circle) and returns the difference of the areas of the two squares.
9. In cricket, an over consists of six deliveries a bowler bowls from one end. Create a function that
takes the number of balls balls bowled by a bowler and calculates the number
of overs and balls bowled by him/her. Return the value as a float, in the format overs balls.
10. A financial institution provides professional services to banks and claims charges from the
customers based on the number of man-days provided. Internally, it has set a scheme to motivate
and reward staff to meet and exceed targeted billable utilization and revenues by paying a bonus
for each day claimed from customers in excess of a threshold target. This quarterly scheme is
calculated with a threshold target of 32 days per quarter, and the incentive payment for each billable
day in excess of such threshold target is shown as follows:
Days Bonus
0 to 32 days Zero
11. Take a paragraph from a user: calcuate frequency of vowels, store it in dictionary.
12. Write a program to store following details of each state of India.
a. Capital, Area in Square km, Population, Literacy rate
b. Answer the following questions w.r.t dictionary created above:
Given a state name by user, display its details
Given a state name by user, display its capital
Display total population of country
Display state names of length greater than 10.
Display states in order of their names
13. Take numbers from user until he says “over”. If user enters odd number, add it in dictionary named
“Odd” with number as key value and its square and cube as values. If user enters even number,
information will moved to dictionary named “Even”
14. Given a list of numbers, create a new list of numbers such that the first and last numbers are added
and stored as the first number, the second and second-to-last numbers are stored as the second
number, and so on. Note that you need to check for even and odd length of lists. In case of an odd
number of integers, leave the central integer in the original list as it is.
15. Given a list of N numbers, write a function to shift the numbers circularly by some integer k (where
k < N). The function should take a list and k as a arguments and return the shifted list.
(b) Write a function that takes a third argument that specifies shifting left or right.
16. The letters a, b, d, e, g, o, p, and q all have something in common: a hole. If you imagine that the
letters were made of stretchy material such as rubber, you could transform one letter into another
while preserving the hole. If tearing or gluing are not allowed, no other letter (without a hole) could
be transformed by stretching into one of these letters with holes. Mathemeticans say that these
letters are topologically
(a) Write a function that takes as an argument a lowercase string and finds the counts of letters in
the string with holes and the count of letters without holes.
(b) Write a function that searches a word list and prints all the words that have two or more letters
with holes.
(c) The set of uppercase letters with holes is different than the set of lowercase letters, e.g.,
lowercase E belongs to the set of letters with a hole, but its capital E does not. Refactor your
functions to consider uppercase letters.
17. Sally invited 17 guests to a dance party. She assigned each guest a number from 2 to 18, keeping 1
for herself. The sum of each couple’s numbers was a perfect square. Write a program to find the
number of Sally’s partner.
18. Using the Python int operators +, -, *, /, ** (not %), and the numbers, 2, 3, 4, and 5, find an
expression using all four numbers exactly once and any three of the operators exactly once that
evaluates to 26. Hint: Build strings and then use the Python eval function, which takes a string as
an argument, evaluates the string, and returns its value, e.g., eval('2*3+4') returns the integer 10.
19. 123456789 = 100. Of course, it doesn’t, but:
(a) Using only the addition operator +, can you insert some addition operators between those
successive digits so that it does sum to 99?
(b) Similarly, but more complicated: using the four standard integer arithmetic operators +, -, *,
and / (not %), how many different solutions can you find (there are more than 100)?