Python Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Q.

1 COUNT MANIA (topic: strings) Write a program that reads in a string on the command line and returns a table of the letters of the alphabet in alphabetical order which occur in the string together with the number of times -Do this for the letters -Do the same for words.(Hint use split) Case should be ignored.(i.e. This = this ) A sample run of the program would look this this: $python q1.py ThiS is String with uPper and Lower CASE Letters I Hate this Upper Lower case Letters a4 c2 d1 e9 g1 h4 i6 l3 n2 o2 p4 r6 s7 t7 u2 w3 Words this 2 upper 2 lower 2 case 2

Q.2 RECURSIVE COUNT (topic:recursion) Write a function Recursive_count(no,nestedlist) that returns the number of occurances of no in nestedlist.

A sample run of the program would look this this: $recursive_count( 2, [2, 9, [2, 1, 13, 2], 8, [2, 6]] ) 4 $recursive_count( 7, [[9, [7, 1, 13, 2], 8], [7, 6]] ) 2 $recursive_count( 15, [[9, [7, 1, 13, 2], 8], [2, 6]] ) 0 $recursive_count( 5, [[5, [5, [1, 5], 5], 5], [5, 6]] ) 6

Q.3 MATRIX MULTIPLICATION (topic: lists) Write a function matrix_multiplication(a,b) which takes two 2-d lists as input and finds the product of the matrix. -sufficient checks for possibility of multiplication should be done A sample run of the program would look this this: $ matrix_multiplication( [ [1,0,-2 ],[0,3,-1 ] ] , [ [0,3],[-2,-1],[0,4] ] ) [ [0,5],[-6 ,-7] ] (i.e. 1 0 -2 0 -3 -1 0 3 -2 -1 0 4 0 5 -6 -7

Q 4.ROT13 (topic special modules) ROT13 is a very funny encryption scheme that scrambles a word in a Caesar-cypher style. Each letter of a word will be "rotated" by 13, that is: a -> n b -> o ... m -> z n -> a o -> b So create a function rot13 which creates a rot13 transformed string

A sample run of the program would look this this: $ rot13("foobar") 's b b o n e' $ rot13("alphatransparency") 'n y c u n g e n a f c n e r a p l'

Q5.ANAGRAM CHECK(topic:strings) An anagram is a word formed by reordering the letters of another word. Write a function isAnagram(w1, w2) that takes in two words as arguments and return True if one word is an anagram of the other word. A sample run of the program would look this this: $ isAnagram('google', 'gogole') True $ isAnagram('google', 'gogoll') False $ isAnagram('google', 'gogogo') False $ isAnagram('Google', 'google') True

Q6.PERMUTATION(topic:lists) Write a program which takes in a pair of positive integers, n and m,(0 < m , n < 20) in the command line and outputs all the possible permutations of choosing m integers from the set {1,2,..n}. O/P: final line containing the number of permutations printed. The permutations should be printed in ascending order lexicographically (e.g., 1 2 precedes 1 3). In other words, the output is unique for a given input. A sample run of the program would look this this:

(p.t.o)

$python q6.py 3 2 12 13 21 23 31 32 6

Q7.GENETIC MUTATIONS (Bio special :P): Let's say there are two methods to find the "distance" of two chromosomes -- that is, how much the two have varied through mutations. Method 1 : is to go letter by letter and count the number of discrepancies. Discrepancy : the no which do not match Method 2 : is to sum the discrepancies of a's (e.g. chromosome x has 5 a's, chromosome y has 7 a's), c's, g's and t's. ( only a , c , g, t) Discrepancy : the diff in no. For eg: If chromosome1= "gtggcaacgtgc" and chromosome 2= "gtagcagcgcgc" Then method 1 : check letter by letter difference.if i does not match with j then discrepancy++ therefore o/p= 3 Then method 2 : Sum of difference in counts of a,c,g,t Therefore o/p = 2 (g:5-5=0 , c:4-3=1 , t:2-1=1 , a:2-2=0) Hence write a program which has three functions method1, method2 , compare(x,y) The compare function should output the distance measure by both the methods. The two chromosome values are stored in the file chromosome.txt Input : chromosome.txt (attached) o/p: 32 52

12
62 44 64

Q8.BRACKET CHECK(topic:lists) Write a program to check the validity of bracket sequence:Create a functions bracket_check(a).A sequence is valid if for every opening bracket a corresponding closing bracket exists. Where a is a string of brackets given as input. -spaces are to be ignored For eg: $python q8.py Enter bracket seq: () ((())) ( (()) () (()) True Enter bracket seq: ()( False Enter bracket seq: ( ((( )) ) () ( ()) True Enter bracket seq: ( (( )) ) ) False Hint: Use concept of stacks

Q.8) DEAL OR NO DEAL(topic: special python modules) There are different monetary amounts (like Re.1, Rs.10, ..., Rs.10 lakh) hidden in different slots which are numbered from 1 to 25. The player has to pick the slot numbers -- which ever he picks, the money in that slot is "lost". The player tries to choose those slots which have less money. The amount that remains in the last slot is the prize money given to the player. You are supposed to output the average amount that the TV show organizers lose for each game. This can be done by running the game automatically and randomly for say 10000 times and output the average prize money.

Write a program which runs randomly for 10000 customers and outputs the avg prize money lost by the ORGANIZERS. Use python random module of python.

Q.9)DICT REVISITED(topic : dict) Write a program to store birthdays of people and has facilities to: (a) Insert people (b) Delete people (c) Modify b'day of a person (d) View b'days of all people (e) View b'day of a given person (f) Get names of people with same b'day So A sample run would be like this : $python q9.py 1.Insert ,2.Delete, 3.Change,4.View Person , 5.View All , 6.Extract people User choice:

(If user choses 1 , ask him to enter a name and bday If user choses 2 ,3,4 ask him to enter name. If user choses 6 ask him to enter Bday)

Q10.BST(topic: classes) Implement a binary search tree data structure with a class for nodes. The class has facilities to insert, delete and traverse the nodes. Use classes to achieve this. For information of a BST visit : http://en.wikipedia.org/wiki/Binary_search_tree

Q11.COMPLEXCRAP Write a program that takes complex numbers until user enters done and computes a list of their magnitudes A sample run will be as follows

You can assume that there are no spaces before or after + $python q11.py 1+3i 4i 5 0+5i -4+3i -4-3i -7i -6 Done o/p [ 3.16 , 4 ,5 ,5 ,5,5,-7,-6 ]

You might also like