Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
56 views

Programming Exercises-Review

The document contains 14 programming problems involving writing functions and programs to: 1) Determine if a year is a leap year 2) Simulate a lottery game 3) Create a guessing game to match a random number 4) Display the number of days in a given month and year 5) Calculate the day of the week for a given date 6) Generate an ISBN number from 9 digits 7) Analyze input integers and calculate totals and averages 8) Find the student with the highest and second highest scores 9) Find the largest number and its occurrences in a list of integers 10) Convert milliseconds to hours, minutes and seconds 11) Display distinct numbers from a list of integers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Programming Exercises-Review

The document contains 14 programming problems involving writing functions and programs to: 1) Determine if a year is a leap year 2) Simulate a lottery game 3) Create a guessing game to match a random number 4) Display the number of days in a given month and year 5) Calculate the day of the week for a given date 6) Generate an ISBN number from 9 digits 7) Analyze input integers and calculate totals and averages 8) Find the student with the highest and second highest scores 9) Find the largest number and its occurrences in a list of integers 10) Convert milliseconds to hours, minutes and seconds 11) Display distinct numbers from a list of integers
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1. Write a program that lets the user enter a year and then determines whether it is a leap year.

All leap
years are divisible by four, but multiples of 100 are not leap years unless they are also a multiple of
400.
For example:
2016 was a leap year, but 1900 was not
2000 was a leap year, as it is a multiple of 400, but 2100 will not be a leap year.

2. Develop a program to play a lottery. The program randomly generates a two-digit number, prompts
the user to enter a two-digit number, and determines whether the user wins according to the following
rules:
a) If the user’s input matches the lottery in the exact order, the award is $10,000.
b) If all the digits in the user’s input match all the digits in the lottery number, the award is $3,000.
c) If one digit in the user’s input matches a digit in the lottery number, the award is $1,000.
3. Create a simple guessing game. Program generates a random integer number between 1 and 50. The
program prompts the user to enter numbers continuously until it matches the randomly generated
number. For each user input, the program reports whether it is too low or too high, so the user can
choose the next input intelligently.
4. Write a program that prompts the user to enter the month and year and displays
the number of days in the month. For example, if the user entered month 2 and year
2000, the program should display that February 2000 has 29 days. If the user
entered month 3 and year 2005, the program should display that March 2005 has 31
days.

Sample Run 1
Enter a month in the year (e.g., 1 for Jan): 4
Enter a year: 2005
April 2005 has 30 days
Sample Run 2
Enter a month in the year (e.g., 1 for Jan): 2
Enter a year: 2006
February 2006 has 28 days
Sample Run 3
Enter a month in the year (e.g., 1 for Jan): 2
Enter a year: 2000
February 2000 has 29 days
5. Zeller’s congruence is an algorithm developed by Christian Zeller to calculate the day of the week. The
formula is
h = (q + 26(m+1)//10 + k + k//4 +j//4 +5j) % 7
where
- h is the day of the week (0: Saturday, 1: Sunday, 2: Monday, 3: Tuesday, 4: Wednesday, 5: Thursday, 6:
Friday).
- q is the day of the month.
- m is the month (3: March, 4: April, ..., 12: December). January and February are counted as months 13
and 14 of the previous year.
- j is year//100.
- k is the year of the century (i.e., year % 100).
Write a program that prompts the user to enter a year, month, and day of the month, and then it displays the
name of the day of the week.
Sample Run 1
Enter year: (e.g., 2008): 2013
Enter month: 1-12: 1
Enter the day of the month: 1-31: 25
Day of the week is Friday
Sample Run 2
Enter year: (e.g., 2008): 2012
Enter month: 1-12: 5
Enter the day of the month: 1-31: 12
Day of the week is Saturday
6. An ISBN-10 (International Standard Book Number) consists of 10 digits:
d1d2d3d4d5d6d7d8d9d10. The last digit, d10, is a checksum, which is
calculated from the other nine digits using the following formula:
(d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) %
11
If the checksum is 10, the last digit is denoted as X according to the ISBN-10
convention. Write a program that prompts the user to enter the first 9 digits and
displays the 10-digit ISBN (including leading zeros).
Sample Run 1
Enter the first 9 digits of an ISBN as a string: 3601267
Incorrect input. It must have exact 9 digits
Sample Run 2
Enter the first 9 digits of an ISBN as a string: 013601267
The ISBN-10 number is 0136012671
Sample Run 3
Enter the first 9 digits of an ISBN as a string: 013031997
The ISBN-10 number is 013031997X
7. Write a program that reads an unspecified number of integers, determines how
many positive and negative values have been read, and computes the total and
average of the input values (not counting zeros). Your program ends with the input
0. Display the average as a floating-point number.
Sample Run 1
Enter an integer, the input ends if it is 0: 1
Enter an integer, the input ends if it is 0: 2
Enter an integer, the input ends if it is 0: -1
Enter an integer, the input ends if it is 0: 3
Enter an integer, the input ends if it is 0: 0
The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25
Sample Run 2
Enter an integer, the input ends if it is 0: 0
No numbers are entered except 0
8. Write a program that prompts the user to enter the number of students and each
student’s name and score, and finally displays the student with the highest score and
the student with the second-highest score. Assume that the number of students is at
least 2.
Sample Run
Enter the number of students: 5
Enter a student name: Smith
Enter a student score: 60
Enter a student name: Jones
Enter a student score: 96
Enter a student name: Peterson
Enter a student score: 85
Enter a student name: Greenlaw
Enter a student score: 98
Enter a student name: Zhang
Enter a student score: 95
Top two students:
Greenlaw's score is 98.0
Jones's score is 96.0
9. Write a program that reads integers, finds the largest of them, and counts its occurrences.
Assume that the input ends with number 0. Suppose that you entered 3 5 2 5 5 5 0. The program
finds that the largest is 5 and the occurrence count for 5 is 4.
Hint: Maintain two variables max and count. max stores the current max number and count
stores its occurrences. Initially, assign the first number to max and 1 to count. Compare each
subsequent number with max. If the number is greater than max, assign it to max and reset count
to 1. If the number is equal to max, increment count by 1.
Sample Run 1
Enter an integer (0: for end of input): 3
Enter an integer (0: for end of input): 5
Enter an integer (0: for end of input): 2
Enter an integer (0: for end of input): 5
Enter an integer (0: for end of input): 5
Enter an integer (0: for end of input): 5
Enter an integer (0: for end of input): 0
The largest number is 5
The occurrence count of the largest number is 4
Sample Run 2
Enter an integer (0: for end of input): 0
No numbers are entered except 0
10. Write a function that converts milliseconds to hours,
minutes, and seconds using the following header:
def convertMillis(millis):
The function returns a string as hours:minutes:seconds.
For example, convertMillis(5500) returns the string
0:0:5,
convertMillis(100000) returns the string 0:1:40,
and convertMillis(555550000) returns the string
154:19:10.
Write a test program that prompts the user to enter a value for
milliseconds and displays a string in the format
of hours:minutes:seconds.
Sample Run
Enter time in milliseconds: 555550000
154:19:10
11. Write a program that reads in integers separated by a space in one line and displays distinct
numbers in their input order and separated by exactly one space (i.e., if a number appears multiple
times, it is displayed only once).
Hint: Read all the numbers and store them in list1. Create a new list list2. Add a number
in list1 to list2. If the number is already in the list, ignore it.
Sample Run
Enter numbers: 1 2 3 2 1 6 3 4 5 2
The distinct numbers are: 1 2 3 6 4 5

12. Write the following function that returns True if the list is already sorted in increasing order:
def isSorted(lst):
Write a test program that prompts the user to enter a list of numbers separated by a space in one line
and displays whether the list is sorted or not. Don’t use the Python list sort method in this program.
Here is a sample run:
Sample Run 1
Enter list: 1 1 3 4 4 5 7 9 10 30 11
The list is not sorted
Sample Run 2
Enter list: 1 1 3 4 4 5 7 9 10 30
The list is already sorted
13. Write a function that checks whether two words are anagrams. Two words are anagrams if they contain the same
letters. For example, silent and listen are anagrams. The header of the function is:
def isAnagram(s1, s2):
(Hint: Obtain two lists for the two strings. Sort the lists and check if two lists are identical.)
Write a test program that prompts the user to enter two strings and checks whether they are anagrams.
Sample Run 1
Enter a string s1: silent
Enter a string s2: listen
silent and listen are anagrams

14. Write a function that returns the sum of all the elements in a specified column in a matrix using the following
header:
def sumColumn(m, columnIndex):
Write a test program that reads a 3 × 4 matrix and displays the sum of each column. Note that the matrix is entered by
rows and the numbers in each row are separated by a space in one line.
Sample Run
Enter a 3-by-4 matrix row 0: 1.5 2 3 4
Enter a 3-by-4 matrix row 1: 5.5 6 7 8
Enter a 3-by-4 matrix row 2: 9.5 1 3 1
Sum of the elements for column 0 is 16.5
Sum of the elements for column 1 is 9.0
Sum of the elements for column 2 is 13.0
Sum of the elements for column 3 is 13.0

You might also like