Anda di halaman 1dari 20

Assignment (BASIC)

1. Write a Python program to print the following string in a specific format (see the output).
Sample String : "Twinkle, twinkle, little star, How I wonder what you are! Up above the world so
high, Like a diamond in the sky. Twinkle, twinkle, little star, How I wonder what you are"

Output :

Twinkle, twinkle, little star,


How I wonder what you are!
Up above the world so high,
Like a diamond in the sky.
Twinkle, twinkle, little star,
How I wonder what you are

2. Write a Python program which accept the radius of a circle from the user and compute the
area.
3. Write a Python program which accept the user's first and last name and print them in reverse
order with a space between them.
4. Write a Python program that accept an integer (n) and computes the value of n+nn+nnn.
Sample value of n is 5
Expected Result : 615
5. Write a Python program to print the documents (syntax, description etc.) of Python built-in
function(s).
Sample function : abs()
Expected Result :
abs(number) -> number
Return the absolute value of the argument.
6. Write a Python program to print the calendar of a given month and year.
Note : Use 'calendar' module.
7. Write a Python program to calculate number of days between two dates.
Sample dates : (2014, 7, 2), (2014, 7, 11)
Expected output : 9 days
8. Write a Python program to get the volume of a sphere with radius 6.
9. Write a Python program to get the difference between a given number and 17, if the number
is greater than 17 return double the absolute difference.
10. Write a Python program to test whether a number is within 100 of 1000 or 2000
11. Write a Python program to calculate the sum of three given numbers, if the values are equal
then return thrice of their sum.
12. Write a Python program to get a new string from a given string where "Is" has been added to
the front. If the given string already begins with "Is" then return the string unchanged.
13. Write a Python program to get a string which is n (non-negative integer) copies of a given
string.
14. Write a Python program to find whether a given number (accept from the user) is even or
odd, print out an appropriate message to the user.
15. Write a Python program to get the n (non-negative integer) copies of the first 2 characters of
a given string. Return the n copies of the whole string if the length is less than 2.
16. Write a Python program to test whether a passed letter is a vowel or not.

17. Write a Python program that will accept the base and height of a triangle and compute the
area.
18. Write a Python program to compute the greatest common divisor (GCD) of two positive
integers.
19. Write a Python program to get the least common multiple (LCM) of two positive integers.
20. Write a Python program to sum of three given integers. However, if two values are equal
sum will be zero.
21. Write a Python program to sum of two given integers. However if the sum is between 15 to
20 it will return 20.
22. Write a Python program that will return true if the two given integer values are equal or their
sum or difference is 5.
23. Write a Python program to add two objects if both objects are integer type.
24. Write a Python program to solve (x + y) * (x + y).
Test Data : x = 4, y = 3
Expected Output : (4 + 3) ^ 2) = 49
25. Write a Python program to compute the future value of a specified principal amount, rate of
interest, and number of years
Test Data : amt = 10000, int = 3.5, years = 7
Expected Output : 12722.79
26. Write a Python program to compute the distance between the points (x1, y1) and (x2, y2).

27. Write a program using loop for displaying number from 1 to n.


28. Write a program using loop for finding factorial of n.
29. Write a program using loop for displaying number n to m by step 2
30. Write a program using loop for displaying 12 to n2
31. Write a program using loop to find out the summation of 12 + 22 + 33 + … + n2
32. Write a program using loop for display the table of n. Say n=7 then display as follows:
7x1=7
7 x 2 = 14
…..
33. Write a program using loop for Fibonacci series:
0,1,1,2,3,5,8,13
34. Find maximum of n number.
35. Find maximum of n even number.
36. Write a program using loop for Decimal to binary conversion.
37. Write a program to check whether a year is leap year or not.
38. Using while loop print the ‘*’ as follows:
*
**
***
****
*****
39. Using while loop print the ‘*’ as follows:
* * * *
* * *
* *
*
40. Write a program for converting a string in lower case letter from upper lower mixture.
PrOgRamMIng → programming
41. Using while loop print the ‘*’ as follows:
*
**
***
****
*****
42. Write a program that takes number of rows and display as follows:

Enter number of rows: 5

1
23
456
7 8 9 10
11 12 13 14 15

43. Write a Python program to get the Python version you are using.
44. Write a Python program to display the current date and time.
Assignment (String)

1. Generate following report from an input English sentence:


(a) Number of Upper case characters
(b) Number of Lower case characters
(c) Number of punctuations
(d) Number of white space
(e) Number of digits
2. Frequency count of characters from an input sentence
From an input English sentence display sorted unique words in ascending and descending
order.
3. From an input English sentence display sorted unique words according to the word length.
4. From two given English sentence find out common words.
5. Find out an input substring from an input English sentence and display all possible matches.
For example:
Input sen: Ram is a student of Rammohon University.
Input substring: Ram
Output: Ram, Rammohon
6. Find out an input substring from an input English sentence and display all possible matches
in bracketed format in the sentence. For example:
Input sen: Ram is a student of Rammohon University.
Input substring: Ram
Output: [Ram] is a student of [Rammohon] University.
7.Encode and Decode an input word in a specified pattern:
(a) Replace each character by next character: cat <-> dbu
(b) Transpose the position of nth character with nth + 1 character:
Cat <--> act <--> atc
8. Write a program to convert digit to in words:
123 → One hundred twenty three
9. Create an adjective list and a noun list and find out all possible combination.

ADJECTIVE NOUN

good boy

bad girl

1) good boy
2) good girl
3) bad boy
4) bad girl

10. Write a Python program to get a string made of the first 2 and the last 2 chars from a given a
string. If the string length is less than 2, return instead the empty string.
Sample String : 'w3resource'
Expected Result : 'w3ce'
Sample String : 'w3'
Expected Result : 'w3w3'
Sample String : ' w'
Expected Result : Empty String

11. Write a Python program to get a string from a given string where all occurrences of its first
char have been changed to '$', except the first char itself.
Sample String : 'restart'
Expected Result : 'resta$t'
12. Write a Python program to get a single string from two given strings, separated by a space
and swap the first two characters of each string.
Sample String : 'abc', 'xyz'
Expected Result : 'xyc abz'
13. Write a Python program to add 'ing' at the end of a given string (length should be at least 3).
If the given string is already ends with 'ing' then add 'ly' instead. If the string length of the given
string is less than 3, leave it unchanged.
Sample String : 'abc'
Expected Result : 'abcing'
Sample String : 'string'
Expected Result : 'stringly'
14. Write a Python program to find the first appearance of the substring 'not' and 'poor' from a
given string, if 'bad' follows the 'poor', replace the whole 'not'...'poor' substring with 'good'.
Return the resulting string.
Sample String : 'The lyrics is not that poor!'
Expected Result : 'The lyrics is good!'
15. Write a Python program to remove the nth index character from a non empty string.
16. Write a Python program to change a given string to a new string where the first and last
chars have been exchanged.
17. Write a Python program to remove the characters which have odd index values of a given
string.
18. Write a Python script that takes input from the user and displays that input back in upper
and lower cases.
19. Write a Python program that accepts a comma separated sequence of words as input and
prints the unique words in sorted form (alphanumerically).
Sample Words : red, white, black, red, green, black
Expected Result : black, green, red, white
20. Write a program that accepts a sentence and calculate the number of upper case letters and
lower case letters.
Suppose the following input is supplied to the program:
Hello world!
Then, the output should be:
UPPER CASE 1
LOWER CASE 9

21. Using while loop print an input string in the following pattern (say input string is Kolkata):
K
Ko
Kol
Kolk
Kolka
Kolkat
Kolkata
K
Ko
Kol
Kolk
Kolka
Kolkat
Kolkata
22. Write a program using loop to count the length of an input string
23. Write a program using loop to count number of vowel and consonant of an input string.
24. Write a program using loop for counting number of words in a sentence.
25. Write a program using loop for converting full name to initial:
For example: Atal Bihary Bajpai → A B Bajpai
26. Write a program using loop to check whether a string is palindrome or not. For example
“madam” is a palindrome.
27. Write a program using loop for showing a string in reverse order.
Assignment (List)

1. Write a Python program to sum all the items in a list.


2. Write a Python program to multiply all the items in a list.
3. Write a Python program to get the largest number from a list.
4. Write a Python program to get the smallest number from a list.
5. Write a Python program to count the number of strings where the string length is 2 or more
and the first and last character are same from a given list of strings.
Sample List : ['abc', 'xyz', 'aba', '1221']
Expected Result : 2
6. Write a Python program to get a list, sorted in increasing order by the last element in each
tuple from a given list of non-empty tuples.
Sample List : [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result : [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]
7. Write a Python program to remove duplicates from a list.
8. Write a Python program to check a list is empty or not.
9. Write a Python function that takes two lists and returns True if they have at least one common
member.
10. Write a Python program to print a specified list after removing the 0th, 2nd, 4th and 5th
elements.
Sample List : ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
Expected Output : ['Green', 'White', 'Black']
11. Write a Python program to print the numbers of a specified list after removing even numbers
from it.
12. Write a Python program to generate all permutations of a list in Python.
13. Write a Python program to get the difference between the two lists.
14. Write a Python program to convert a list of characters into a string.
15. Write a Python program to find second smallest number in a list.
16. Write a Python program to get the frequency of the elements in a list.
17. Write a function that accepts two lists A and B and returns a new list which contains all the
elements of list A followed by elements of list B. Notice that the behaviour of this function is
different from list.extend() method because the list.extend() method extends the list in place, but
here you are asked to create a new list and return it. Your function should not return the original
lists. For example if the input lists are:
A = ['p', 'q', 6, 'k']
B = [8, 10]
Then your function should return a list such as:
['p', 'q', 6, 'k', 8, 10]
18. Write a function that accepts a list that contains positive integers and returns a new list
which contains all the elements from original list but adds 1 to those elements which are odd.
For example if :
input_list = [12, 3, 4, 5, 6]
Then your function should return a list such as:
[12, 4, 4, 6, 6]
19. Write a function that accepts a list and returns a new list such that the new list contains all
the items of the old list in reverse order. Note that this is NOT a sorting problem. Do NOT use
the built in reverse() method for this problem. For example if:
input_list = ['apples', 'eat', "don't", 'I', 'but', 'Grapes', 'Love', 'I']
then your function should return a list such as:
['I', 'Love', 'Grapes', 'but', 'I', "don't", 'eat', 'apples']
20. Write a Python program to display the first and last colors from the following list.
color_list = ["Red","Green","White" ,"Black"]
21. Write a Python program to print all even numbers from a given numbers list in the same
order and stop the printing if any numbers that come after 237 in the sequence.
numbers = [
386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345,
399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217,
815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717,
958,743, 527
]
22. Write a Python program to print out a set containing all the colors from color_list_1 which
are not present in color_list_2.
Test Data :
color_list_1 = set(["White", "Black", "Red"])
color_list_2 = set(["Red", "Green"])
Expected Output :
{'Black', 'White'}
23. Write a Python program which accepts a sequence of comma-separated numbers from user
and generate a list and a tuple with those numbers.
24. Write a Python program to accept a filename from the user print the extension of that.
25. Write a Python program to display the examination schedule. (extract the date from
exam_st_date).
exam_st_date = (11, 12, 2014)
Sample Output : The examination will start from : 11 / 12 / 2014
26. Write a Python program to check whether a specified value is contained in a group of
values.
Test Data :
3 -> [1, 5, 8, 3] : True
-1 -> [1, 5, 8, 3] : False
27. Write a Python program to concatenate all elements in a list into a string and return it.
28. Write a Python program to count the number 4 in a given list.
Assignment (Dictionaries)

1. Write a Python script to sort (ascending and descending) a dictionary by value.


2. Write a Python script to add key to a dictionary.
Sample Dictionary : {0: 10, 1: 20}
Expected Result : {0: 10, 1: 20, 2: 30}
3. Write a Python script to concatenate following dictionaries to create a new one.
Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
4. Write a Python script to check if a given key already exists in a dictionary.
5. Write a Python program to iterate over dictionaries using for loops.
6. Write a Python script to generate and print a dictionary that contains number (between 1 and
n) in the form (x, x*x).
Sample Dictionary ( n = 5) :
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
7. Write a Python program to sum all the items in a dictionary.
8. Write a Python program to remove a key from a dictionary.
9. Write a Python program to get the maximum and minimum value in a dictionary.
10. Write a function that takes an integer as input argument and returns the integer using words.
For example if the input is 4721 then the function should return the string "four seven two one".
Note that there should be only one space between the words and they should be all lowercased
in the string that you return.
11. Write a function that accepts a string which contains a particular date from the Gregorian
calendar. Your function should return what day of the week it was. Here are a few examples of
what the input string (Month Day Year) will look like. However, you should not 'hardcode' your
program to work only for these input!
"June 12 2012"
"September 3 1955"
"August 4 1843"
Note that each item (Month Day Year) is separated by one space. For example if the input string
is:
"May 5 1992"
Then your function should return the day of the week (string) such as:
"Tuesday"
Algorithm with sample example:
# Assume that input was "May 5 1992"
day (d) = 5 # It is the 5th day
month (m) = 3 # (*** Count starts at March i.e March = 1, April = 2, ... January = 11, February
= 12)
century (c) = 19 # the first two characters of the century
year (y) = 92 # Year is 1992 (*** if month is January or february decrease one year)
# Formula and calculation
day of the week (w) = (d + floor(2.6m - 0.2) - 2c + y + floor(y/4) + floor(c/4)) modulo 7
after calculation we get, (w) = 2
Count for the day of the week starts at Sunday, i.e Sunday = 0, Monday = 1, Tuesday = 2, ...
Saturday = 6
Since we got 2, May 5 1992 was a Tuesday
12. Write a function that takes a 4 digit integer (from 1000 to 9999 both inclusive) as input
argument and returns the integer using words as shown below:
Sample Examples:
if the input integer is 7000 then the function should return the string "seven thousand"
if the input integer is 1008 then the function should return the string "one thousand eight"
if the input integer is 4010 then the function should return the string "four thousand ten"
if the input integer is 1012 then the function should return the string "one thousand twelve"
if the input integer is 4506 then the function should return the string "four thousand five hundred
six"
if the input integer is 9900 then the function should return the string "nine thousand nine
hundred"
if the input integer is 8880 then the function should return the string "eight thousand eight
hundred eighty"
if the input integer is 5432 then the function should return the string "five thousand four hundred
thirty two"

Notice that the words in the output strings should all be lower cased and separated by only one
space and make sure your words match the following spellings:
one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen,
sixteen, seventeen, eighteen, nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty,
ninety, hundred, thousand
Assignment (Function)

1. Write function Binary2Dec(Binary) that take input a binary number as string and return
the decimal equivalent of it.
2. Write a function EuclideanDist(v1,v2) that take two lists (int/float) as input and return the
Euclidean distance between two list.
3. Write a function MinimumDist(lst, lstLst) that take input a list and a list of list of same
order e.g. lst=[1,2,3] and lstLst=[[1,1,2], [2,4,5], [7,1,3], [1,2,3]] and return the list element
from lstLst which has minimum EuclideanDist when compared to lst.
4. Write a function named find_word_horizontal that accepts a 2-dimensional list of
characters (like a crossword puzzle) and a string (word) as input arguments. This
function searches the rows of the 2d list to find a match for the word. If a match is found,
this functions returns a list containing row index and column index of the start of the
match, otherwise it returns the value None (no quotations).
For example if the function is called as shown below:
crosswords=[['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]
word='cat'
find_word_horizontal(crosswords,word)
then your function should return
[2,1]
Notice that the 2d input list represents a 2d crossword and the starting index of the horizontal
word 'cat' is [2,1]
s d o g

c u c m

a c a t

t e t k
Note: In case of multiple matches only return the match with lower row index. If you find two
matches in the same row then return the match with lower column index.

2. Write a function named find_word_vertical that accepts a 2-dimensional list of characters


(like a crossword puzzle) and a string (word) as input arguments. This function searches the
columns of the 2d list to find a match for the word. If a match is found, this functions returns a
list containing row index and column index of the start of the match, otherwise it returns the
value None (no quotations).
For example if the function is called as shown below:
crosswords=[['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]
word='cat'
find_word_vertical(crosswords,word)
then your function should return:
[1,0]
Notice that the 2d input list represents a 2d crosswords and the starting index of the vertical
word 'cat' is [1,0]
s d o g

c u c m

a c a t

t e t k
Note: In case of multiple matches only return the match with lower column index. If you find two
matches in the same column then return the match with lower row index.

3. Write a function named capitalize_word_in_crossword that accepts a 2-dimensional list of


characters (like a crossword puzzle) and a string (word) as input arguments. This function
searches the rows and columns of the 2d list to find a match for the word. If a match is found,
this functions capitalizes the matched characters in 2-dimensional list and returns the list. If no
match is found, this function simply returns the origianl 2-dimensional list with no modification.
Example 1: If the function is called as shown below:
crosswords=[['s','d','o','g'],['c','u','c','m'],['a','x','a','t'],['t','e','t','k']]
word='cat'
capitalize_word_in_crossword(crosswords,word)
then your function should return:
[['s','d','o','g'],['C','u','c','m'],['A','x','a','t'],['T','e','t','k']]
Notice that the above list is a representation for a 2-dimensional crossword puzzle as shown
below.
s d o g

C u c m

A x a t

T e t k
Example 2: if the function is called as shown below:
crosswords=[['s','d','o','g'],['c','u','c','m'],['a','c','a','t'],['t','e','t','k']]
word='cat'
capitalize_word_in_crossword(crosswords,word)
then your function should return:
[['s','d','o','g'],['c','u','c','m'],['a','C','A','T'],['t','e','t','k']]
Notice that the above list is a representation for a 2-dimensional crossword puzzle as shown
below.
s d o g

c u c m

a C A T

t e t k
Note: If both a horizontal and a vertical match is found then only select the horizontal match. For
example, in the above case there is a horizontal match starting at [2,1] and there is also a
veritcal match starting at [1,0]. Notice that only the characters in the horizontal match should be
capitalized in the returned list.
Hint: You should use the functions that you developed in part 1 and part 2 as helper functions
for part 3.

Assignment (Recursion)
1. Write a function named calculate_fibonacci that receives a positive integer 'n' as
parameter and calculates and returns the nth fibonacci number using recursion.

Notes
● Fibonacci numbers are the numbers in the sequence: 0, 1, 1, 2, 3, 5, 8, 13, ...
● The 0th Fibonacci number is 0, the 1st Fibonacci number is 1.
● All other numbers are sum of the previous two numbers.
For example, when your function is called as:
calculate_fibonacci(10)

Then, your function should return the 10th fibonacci number:


55

2. Write a function named calculate_factorial that receives a positive integer 'n' as parameter
and calculates and returns the factorial of n using recursion.

Notes

Factorial is the product of an integer with all the integers below it. For example, the factorial of 5
is 5*4*3*2*1 = 120. Note that the factorial of both 0 and 1 is 1. For example, when your function
is called as:
calculate_factorial(10)
Then, your function should return the number:
3628800
3. Write a function named calculate_exponent that receives two positive integers a and b as
parameter and calculates and returns a to the power of b using recursion. For example, when
your function is called as:
calculate_exponent(5, 3)
Then, your function should return: 125

4. Write a function named nested_list_sum that receives a nested list of integers as parameter
and calculates and returns the total sum of the integers in the list using recursion. Keep in mind
that the inner elements may be integers or other nested lists themselves.

For example, when your function is called as:


nested_list_sum([1, -1, [2, -2], [3, -3, [4, -4], 10]])

Then, your function should return the total sum as 10

Assignment (FILE I/0)

1. Write a user defined function in python to read the content from a text file myfile.txt,
count and display the number of blank spaces present init.
2. Write a function in python to count the number of alphabets present in a text file
“alpha.txt”.
3. Write a function in python to count and display the number of words starting with a vowel
present in a given text file “mytext.txt”.
4. Write a function which reads an already existing file “text1.txt” and write digits (0-9) into a
new text file “DIGIT.TXT” and non digit files into NONDIG.TXT.
5. Write a function in python to count the number of lines present in a text file
“STORY.TXT”.
6. Assuming that a text file named TEXT1.TXT already contains some text written into it.
Write a function named vowelwords(), that reads the file TEXT1.TXT and creates a new
file named TEXT2.TXT , which shall contain only those words from the file TEXT1.TXT
which don’t start with an uppercase vowel.
7. Write an interactive python program to read a text file and display the following :
(i) Frequency table of all the alphabetic characters(ii)Number of numeric characters
present in the file
8. Write a function that accepts a filename as input argument and reads the file and saves
each line of the file as an element in a list (without the new line ("\n")character) and
returns the list. Each line of the file has comma separated values: For example if the
content of the file looks like this:
Lucas,Turing,22
Alan,Williams,27
Layla,Trinh,21
Brayden,Huang,22
Oliver,Greek,20

then your function should return a list such as


['Lucas,Turing,22', 'Alan,Williams,27', 'Layla,Trinh,21', 'Brayden,Huang,22',
'Oliver,Greek,20']
9. Write a function that accepts a filename(string) of a CSV file which contains the
information about student's names and their grades for four courses and returns a
dictionary of the information. The keys of the dictionary should be the name of the
students and the values should be a list of floating point numbers of their grades. For
example, if the content of the file looks like this:
Mark,90,93,60,90
Abigail,84,50,72,75
Frank,46,83,53,79
Yohaan,47,77,74,96
then your function should return a dictionary such as:
out_dict = {'Frank': [46.0, 83.0, 53.0, 79.0],
'Mark': [90.0, 93.0, 60.0, 90.0],
'Yohaan': [47.0, 77.0, 74.0, 96.0],
'Abigail': [84.0, 50.0, 72.0, 75.0]}
10. Write a function that takes a file name (string) of a CSV file which contains the
information about student's names and their grades for four courses and returns a
dictionary that contains information about the students whose grades in both Math and
Chemistry is above 70. Note that if the file has any comments (lines starting with #) then
you should ignore such a line. The file will have the following format:
#first_name,math,physics,chemistry,biology
For example if the contents of the file are:
Luke,89,94,81,97
Eva,40,50,65,90
Joseph,55,58,54,99
Oliver,73,74,89,91

then your function should return a dictionary such as:


out_dict = {'Luke': [89.0, 94.0, 81.0, 97.0],
'Oliver': [73.0, 74.0, 89.0, 91.0]}

11. Write a function that accepts a filename as input argument, the file contains the
information about a persons expenses on milk and bread and returns a dictionary that
contains exactly the strings 'milk' and 'bread' as the keys and their floating point values
in a list as values. Each line of the file may start with a 'm' or a 'b' denoting milk or bread
respectively. For example if the contents of the file are:
m000
b251
b354
m100
b531
m010
b245

then your function should return a dictionary such as:


out_dict = {'milk': [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [0.0, 1.0, 0.0]],
'bread': [[2.0, 5.0, 1.0], [3.0, 5.0, 4.0], [5.0, 3.0, 1.0], [2.0, 4.0, 5.0]]}

12. Write a function that accepts a file name as input argument and constructs and returns a
nested dictionary from it. The keys of this dictionary should be last names, and the
values should be dictionaries that contain first names as the keys and integer ages as
the values. Note that the data may contain multiple people with the same last name, but
it will have unique first names. Ignore any lines that start with '#' The file will contain
comma separated values (CSV) For example if the contents of the file are:
#first_name,last_name,age
Matthew,Abbey,65
Chloe,Orion,49
Yohaan,Adams,54
Krishna,Adams,35
Resa,Orion,86
Lucas,Abbey,60
Courtney,Abbey,67
Joseph,Orion,45
Mark,Abbey,60
Eva,Orion,76

then your function should return a dictionary such as:


{'Abbey': {'Matthew': 65, 'Courtney': 67, 'Lucas': 60, 'Mark': 60},
'Orion': {'Chloe': 49, 'Resa': 86, 'Eva': 76, 'Joseph': 45},
'Adams': {'Krishna': 35, 'Yohaan': 54}}

13. Write a function named create_grades_dict that accepts a string as the name of a file.
Assuming that the file is a text file which includes name and grades of students, your
function should read the file and return a dictionary with the exact format as shown
below: The format of the input file is:
Student ID, Last_name, Test_x, grade, Test_x, grade, ..
Student ID, Last_name, Test_x, grade, Test_x, grade, ..
Student ID, Last_name, Test_x, grade, Test_x, grade, ..
....
An example of the input file is shown below. Sample Input Output Assuming that
the input file "student_grades.txt" contains the following text:
1000123456, Rubble, Test_3, 80, Test_4 , 80
1000123459, Chipmunk, Test_4, 96, Test_1, 86 , Quiz_1 , 88
Notes:
● Items are seperated by comma and one or more spaces may exist between the
items.
● The "ID" of each student is unique. Two students may have the same Name (but
IDs will be different)
● The "Name" of each student will only include a last name with no punctuation.
Maximum of 15 characters.
● There will be an integer grade for each test (0-100)
● There are only four valid tests, i.e. Test_1, Test_2, Test_3, Test_4. There may be
other grades in the file and you should ignore those grades.
○ Each student may have missing grade(s) for the tests. A missing grades should
be considered as 0.
○ Grades may not be in order i.e. Test_3 may appear before Test_1.
Your function should read the input file, calculate the average test grade for each
student and return a dictionary with the following format:
{'Student_ID':[Last_name,Test_1_grade,Test_2_grade,Test_3_grade,Test_4_grade,aver
age], ...}
For example in the case of sample input file shown above, your function should return
the following dictionary:
{'1000123456': ['Rubble', 0, 0, 80, 80, 40.0], '1000123459': ['Chipmunk', 86, 0, 0, 96,
45.5]}

14. Write a function in python to print the count of word the as an independent word in a text
file STORY.TXT. for example, if the content of the file STORY.TXT is
There was a monkey in the zoo. The monkey was very naughty.

Then the output of the program should be 2.


15. Write a function in python to count and display the number of lines not starting with
alphabet 'A' present in a text file "STORY.TXT".
Example:
If the file "STORY.TXT" contains the following lines,
The rose is red.
A girl is playing there.
There is a playground.
An aeroplane is in the sky.
Numbers are not allowed in the password.

The function should display the output as 3.


16. Assuming that a text file named FIRST.TXT contains some text written into it, write a
function named copyupper(), that reads the file FIRST.TXT and creates a new file
named SECOND.TXT contains all words from the file FIRST.TXT in uppercase.
17. Read a Unicode Text file and create a dictionary where key is the word and value isthe
frequency of the word. Then dump the dictionary in a file named as “MyPickle.dct”. Then
read the “MyPickle.dct” and another unicode file say “SecondUni.txt” and check whether
the words in “SecondUni.txt” file is already present in “MyPickle.dct” or not. If the word
present in the “MyPickle.dct” then update the frequency value otherwise enter the new
word into it as new key and keep the value of that key as counts of the word in
“SecondUni.txt” .

Assignment (Tuple)

1. Write a function that accepts a tuple of positive integers and returns the mean and
median of the integers as a tuple. For example if the input tuple is:
(3, 3, 0, 1, 12, 13, 15, 16)
then your function should return a tuple that contains the mean and median as:
(7.875, 7.5)
You may use (import) the numpy package in your function. Do not use the "statistics" package.
2. Write a function that accepts a dictionary as input and returns a tuple of tuples. That is your
first tuple should be the tuple of all the keys, and the second tuple should be tuple of all the
values in the dictionary. For example if the input dictionary is:
input_dictionary = {1:"a", 2:"b", 3:"c", 4:"d"}
then you should return a tuple(tuple of keys, tuple of values) such as:
((1, 2, 3, 4), ('a', 'b', 'c', 'd'))

Assignment (Error Handling)

1. You are trying to concatenate two strings and you need to write a function to perform the
task. The function takes two arguments. You do not know the type of the first argument
in advance but the second argument is certainly a string. Write a function that takes an
unknown input and a string as input and tries to handle the error when you try to
concatenate this unknown input to the string using the try..except..else clause. The
unknown input could be either an integer or a string or a float. If the concatenation fails
your function should return the value None (exactly without the quotes), If successful
your function should return the resulting string.
2. You are trying to divide two numbers and you need to write a function to perform the
task. The function takes two arguments. The first argument is a float but you are unsure
about the second argument (there is a chance that the second argument could be a
zero). Write a function that takes a float and an unknown input and tries to handle the
error when you try to divide the float by the unknown input using the try..except..else
clause. The unknown input could be either an integer or a string or a float. If the
operation fails your function should return the value None (exactly without the quotes), If
successful your function should return the result.
3. You are trying to modify the content of a list and you need to write a function to perform
the task. The function takes three arguments. The first argument is the list itself, the
second argument is an index 'n' and the third argument is a string. Your job is to set the
'n'th (index) item of the list as the given string and return the modified list if successful. In
case of a failure your function should return the original list. Write a function that
performs this task using the try...except...else statements.

Assignment (OOP)

1. Write a program using python to simulate a Bank with account creation, deposit
and withdraw facility. Create class BankAccount which has methods like (a)
make_account (b) withdraw(account, amount) and (b) deposit(account, amount)
and a variable “balance: will be initialized to 0 after calling make_account() and it
will be increase and decrease with the calling of functions deposit and withdraw
method simultaneously.
2. Write a program to implement a STACK using python. Create class Stack with
following methods: (a) isEmpty (b) push (c) pop (d) peek i.e. top of the Stack (e)
size

Anda mungkin juga menyukai