Python Mini Projects 1-7
Python Mini Projects 1-7
Mini Projects
Project #1:
Here are some basic facts about tennis scoring: A tennis match is made up of sets. A set is
made up of games.
To win a set, a player has to win 6 games with a difference of 2 games. At 6-6, there
is often a special tie-breaker. In some cases, players go on playing till one of them wins the
set with a difference of two games.
Tennis matches can be either 3 sets or 5 sets. The player who wins a majority of sets
wins the match (i.e., 2 out 3 sets or 3 out of 5 sets) The score of a match lists out the games in
each set, with the overall winner's score reported first for each set. Thus, if the score is 6-3, 5-
7, 7-6 it means that the first player won the first set by 6 games to 3, lost the second one 5
games to 7 and won the third one 7 games to 6 (and hence won the overall match as well by 2
sets to 1).
You will read input from the keyboard (standard input) containing the results of
several tennis matches. Each match's score is recorded on a separate line with the following
format:
Winner:Loser:Set-1-score,...,Set-k-score, where 2 ≤ k ≤ 5
Halep:Wozniacki:3-6,6-3,6-3
indicates that Halep beat Wozniacki 3-6, 6-3, 6-3 in a best of 3 set match.
You have to write a Python program that reads information about all the matches and compile
the following statistics for each player:
You should print out to the screen (standard output) a summary in decreasing order of
ranking, where the ranking is according to the criteria 1-6 in that order (compare item 1, if
equal compare item 2, if equal compare item 3 etc, noting that for items 5 and 6 the
comparison is reversed).
Federer:Nadal:2-6,6-7,7-6,6-3,6-1
Nadal:Federer:6-3,4-6,6-4,6-3
Federer:Nadal:6-0,7-6,6-7,6-3
Nadal:Federer:6-4,6-4
Federer:Nadal:2-6,6-2,6-0
Nadal:Federer:6-3,4-6,6-3,6-4
Federer:Nadal:7-6,4-6,7-6,2-6,6-2
Nadal:Federer:7-5,7-5
Halep:Wozniacki:3-6,6-3,6-3
Halep 0 1 2 15 1 12
Wozniacki 0 0 1 12 2 15
You can assume that there are no spaces around the punctuation marks ":", "-" and ",". Each
player's name will be spelled consistently and no two players have the same name.
Project #2:
Write a Python function histogram(l) that takes as input a list of integers with repetitions and
returns a list of pairs as follows:
for each number n that appears in l, there should be exactly one pair (n,r) in the list
returned by the function, where r is is the number of repetitions of n in l.
the final list should be sorted in ascending order by r, the number of repetitions. For
numbers that occur with the same number of repetitions, arrange the pairs in ascending
order of the value of the number.
For instance:
>>> histogram([13,12,11,13,14,13,7,7,13,14,12])
[(11, 1), (7, 2), (12, 2), (14, 2), (13, 4)]
>>> histogram([7,12,11,13,7,11,13,14,12])
[(14, 1), (7, 2), (11, 2), (12, 2), (13, 2)]
>>> histogram([13,7,12,7,11,13,14,13,7,11,13,14,12,14,14,7])
[(11, 2), (12, 2), (7, 4), (13, 4), (14, 4)]
Project #3:
The academic office at the Hogwarts School of Witchcraft and Wizardry has compiled data
about students' grades. The data is provided as text from standard input in three parts:
information about courses, information about students and information about grades. Each
part has a specific line format, described below..
The possible grades are A, AB, B, BC, C, CD, D with corresponding grade points 10, 9, 8, 7,
6, 5 and 4. The grade point average of a student is the sum of his/her grade points divided by
the number of courses. For instance, if a student has taken two courses with grades A and C,
the grade point average is 8 = (10+6)÷2. If a student has not completed any courses, the grade
point average is defined to be 0.
You may assume that the data is internally consistent. For every grade, there is a
corresponding course code and roll number in the input data.
Each section of the input starts with a line containing a single keyword. The first section
begins with a line containing Courses. The second section begins with a line
containing Students. The third section begins with a line containing Grades. The end of the
input is marked by a line containing EndOfInput.
Write a Python program to read the data as described above and print out a line listing the
grade point average for each student in the following format:
Your output should be sorted by Roll Number. The grade point average should be rounded
off to 2 digits after the decimal point. Use the built-in function round().
Courses
POT~Potions~1~2011-2012~Severus Snape
Students
RAV4309~Angelina Johnson
HUF7201~Gwenog Jones
GRF9110~Parvati Patil
RAV4308~Olive Hornby
Grades
POT~1~2011-2012~RAV4308~C
POT~1~2011-2012~RAV4309~B
POT~1~2011-2012~GRF9110~A
EndOfInputCHAR~1~2011-2012~SLY2306~C
CHAR~1~2011-2012~SLY2307~B
CHAR~1~2011-2012~SLY2308~AB
EndOfInput
Sample Output:
GRF9110~Parvati Patil~10.0
HUF7201~Gwenog Jones~0
RAV4308~Olive Hornby~6.0
RAV4309~Angelina Johnson~8.0
Project #4:
As we all know, a palindrome is a word that equals its reverse. Here are some
examples of palindromes: malayalam, gag, appa, amma.
In this task you will given a word and you must find the longest subword of this word
that is also a palindrome.
For example if the given word is abbba then the answer is abbba. If the given word
is abcbcabbacba then the answer isbcabbacb.
Solution hint:
Input format:
The first line of the input contains a single integer N indicating the length of the word.
The following line contains a single word of length N, made up of the letters a,b,…, z.
Output format:
The first line of the output must contain a single integer indicating the length of the
longest subword of the given word that is a palindrome. The second line must contain a
subword that is a palindrome and which of maximum length. If there is more than one
subword palindrome of maximum length, print the one that is lexicographically smallest (i.e.,
smallest in dictionary order).
Test Data:
You may assume that 1 ≤ N ≤ 5000. You may further assume that in 30% of the
inputs 1 ≤ N ≤ 300.
Example:
We illustrate the input and output format using the above examples:
Sample Input 1:
abbba
Sample Output 1:
abbba
Sample Input 2:
12
abcbcabbacba
Sample Output 2:
bcabbacb
Project #5:
A mail is treated as spam when the percentage of spam sentences in the content of mail is
more than 30%. A list of spam sentences are:
Be amazed
Write a python program that accepts this input text file, processes the content and determines
whether this text file is treated as spam or not.
Project #6:
Sample Input:
bfajihgedc
Sample Output:
bfcadeghij
Project #7:
Let us consider polynomials in a single variable x with integer coefficients. For instance:
3x4 - 17x2 - 3x + 5
Each term of the polynomial can be represented as a pair of integers (coefficient,exponent).
The polynomial itself is then a list of such pairs.
We have the following constraints to guarantee that each polynomial has a unique
representation:
[(3,4),(-17,2),(-3,1),(5,0)]
The zero polynomial, 0, is represented as the empty list [], since it has no terms with nonzero
coefficients.
Write Python functions for the following operations:
addpoly(p1,p2)
multpoly(p1,p2)
>>> addpoly([(4,3),(3,0)],[(-4,3),(2,1)])
[(2, 1),(3, 0)]
Explanation: 2x + (-2x) = 0
>>> multpoly([(1,1),(-1,0)],[(1,2),(1,1),(1,0)])
[(1, 3),(-1, 0)]