Practice Question Omar Khan / Atif Alvi October 10, 2016 1
Chapter 1 Loops 1.1 While Loop Iterating through a string 1. Print the first 10 integers 1 i = 0 2 n = 10 3 while i<n: 4 print (i) 5 i = i + 1 6 print () 2. Print the first 10 integers in a row. : 1 i = 0 2 while i<n: 3 print (i, end =",") 4 i = i + 1 5 print () 3
3. Write code that prints the following pattern on the screen: * * * * * : 1 i = 1 2 while i<n: 3 j = 0 4 while j < i: 5 print (".", end =" ") 6 j = j + 1 7 print () 8 i = i + 1 9 print () 4. Write code that prints the following pattern on the screen: * : 1 i = 1 2 while i <=n: 3 j = 0 4 while j < i: 5 if j ==0 or j==i -1 or i==n: 4
6 print ("*", end =" ") 7 else : 8 print (" ", end =" ") 9 j = j + 1 10 print () 11 i = i + 1 12 print () 5. Write code to print the following pattern. You have to use nested while loops. * * * * : 1 i = 1 2 while i<n: 3 j = 0 4 while j < i %5: 5 print ("*", end =" ") 6 j = j + 1 7 print () 8 i = i + 1 9 print () 5
Chapter 2 Functions 1. Write a function isodd that return a True value if the input to it is an odd number and False value if the input number is even 1 def isodd (n): 2 return n %2==1 2. Write a function oddsinrange that takes two number l and u as input (you can assume that l u) and returns the number of odd number in the range l - u (l and u inclusive). Use the isodd function defined above. 1 def oddsinrange (l, u): 2 count = 0 3 while l < u: 4 if isodd (l): 5 count = count + 1 6 l = l + 1 7 return count 3. Write a function max4 that takes 4 numbers as input and returns the value of the largest. 1 def max4 (a, b, c, d): 2 maxval = a 3 if b > maxval : 4 maxval = b 5 if c > maxval : 6 maxval = c 7 if d > maxval : 8 maxval = d 9 return maxval 7
4. Write a function isprime that takes a positive integer as input and determines if the given number is a prime or not 1 def isprime (n): 2 if n <= 0 : 3 print (" Please use positive integers as input only ") 4 if n == 1 or n == 2: 5 return True 6 range = int (n /2) 7 i = 2 8 while i <= range : 9 if n %2==0: 10 return False 11 i = i + 2 12 return True 5. Write a function called dicen that takes a number n as input. The function when called should simulate the roll of a dice n times and return the ration of the times that the roll came up with the number 6 1 from random import randint 2 def dicen (n): 3 i = 0 4 count = 0 5 while i<n: 6 dice = randint (1,6) 7 if dice1 == 6: 8 count = count + 1 9 i = i + 1 10 return count / n 6. Write a function called dicesumratio that takes two positive integers n and total as input, simulates the roll of a pair of dice n times and returns the following ratio: 1 from random import randint 2 def dicesumratio (n, total ): 3 i = 0 4 count = 0 5 while i<n: 6 dice1 = randint (1,6) times that the sum of a roll is equal to total n 8
7 dice2 = randint (1,6) 8 if ( dice1 + dice2 ) == total : 9 count = count + 1 10 i = i + 1 11 return count / n Which sum do you expect to come up most often in a roll of a pair of dice? Can you write code that experimentally supports your answer? 7. Given the function below, show what values are printed for f(0), f(1), f(10). Describe the execution steps. 1 def f(a): 2 if a < 0: 3 return -1 4 n = a 5 while n > 0: 6 if n%2 == 0: 7 n = n //2 8 print (n) 9 elif n == 1: 10 print (n) 11 return 1 12 else : 13 n = 3 * n + 1 14 print (n) 15 return 0 a) For f(0) neither the condition for the if statement nor the condition for the while statement are true so the function just returns 0. b) For f(1) we enter the while loop and skip the condition with the if part and go into the body of the elif part. A value of 1 is printed and the function returns a value of 1 also. c) For f(10) the following evaluations take palce: i. n is even so n=n//2 is evaluated n becomes 5 and 5 is printed ii. In the next iteration of the while loop n is odd so n = 3 * n + 1 is evaluated n becomes 16. 16 gets printed iii. n is even so n=n//2 is evaluated n becomes 8 and 8 is printed iv. n is even so n=n//2 is evaluated n becomes 4 and 4 is printed v. n is now going to remain even until it becomes 1 at which point 1 gets printed and the function returns a value of 1. 9
The following thus gets printed: 5 16 8 4 2 1 10
Chapter 3 Strings 3.1 Iterative Questions Iterating through a string 1. Write a function called printstr that takes a string as an input parameter and prints out each element of the list on a separate line. 3 ways to do it are : 1 def printstr ( str ): 2 for c in str : 3 print (c) 1 def printstr ( str ): 2 for i in range ( len ( str )): 3 print ( str [i]) 1 def printstr ( str ): 2 i=0 3 while i<len ( str ): 4 print ( str [i]) 5 i = i + 1 Generating new strings from old 1. Write a function called punctuated that takes string s and a character c as input and prints a string that is the same as the input string s but with the character c between each adjacent letter 1 def punctuated (str, c): 2 for i in range ( len ( str ) -1): 11
3 print ( str [i], end =c) 4 print ( str [ -1]) Sample run 1 >>> punctuated ( Python, - ) 2 P-y-t-h-o-n 2. Write a function called rtnpunctuated that takes string str and a character c as input and returns a string that is the same as the input string str but with the character c between each adjacent letter 1 def punctuated (str, c): 2 rtnstr = 3 for i in range ( len ( str ) -1): 4 rtnstr = rtnstr + str [ i] + c 5 rtnstr = rtnstr + str [ i] 6 return rtnstr Sample run 1 >>> punctuated ( Python, - ) 2 P-y-t-h-o-n 3. Write a function called rtnsubstr that takes a string s and two integers starti and stopi as an input parameters and returns the substring represented by indices starti and stopi. 1 def rtnsubstr (s, starti, stopi ): 2 rtnstr = 3 for i in range ( starti, stopi +1) : 4 rtnstr = rtnstr + s[i] 5 return rtnstr Sample run 1 >>> rtnsubstr ( abracadabra, 3, 7) 2 acada 4. Write a function called prntfirstn that takes an integer n and a string as input and prints the first n characters of the string. If the string has fewer than n characters then print the message input list does not have enough elements. 12
1 def prntfirstn (n, str ): 2 if len ( str )<n: 3 print (" input string does not have enough elements ") 4 else : 5 for i in range (n): 6 print ( str [i]) 5. Write a function called prntlastn that takes an integer n and a str as input and prints the Last n members of the string. If the string has fewer than n elements then print the message input string does not have enough elements. 2 ways: 1 def prntlastn (n, str ): 2 if len ( str )<n: 3 print (" the input string does not have enough elements ") 4 else : 5 first = len ( str )-n 6 for i in range (first, len ( str )): 7 print ( str [i]) 1 def prntlastn (n, str ): 2 if len ( str )<n: 3 print (" the input string does not have enough elements ") 4 else : 5 first = len ( str )-n 6 for e in str [ first :]: 7 print (e) 6. Write a function prtrstr that prints the given string in reverse order. 1 def prtrlst ( str ): 2 i = len ( str ) -1 3 while i >=0: 4 print ( str [i], end = ) 5 i = i - 1 7. Write a function removechar that takes a string and a character as input and returns a string with all the instances of the character removed. 13
1 def removechar (istr, ic): 2 rstr = 3 for c in istr : 4 if c!= ic: 5 rstr = rstr + c 6 return rstr 8. Write a function replacechar that takes a string and two characters as input and returns a string with all the instances of the first character replaced by the second character. 1 def replacechar (istr, c1, c2): 2 rstr = 3 for c in istr : 4 if c== c1: 5 rstr = rstr + c2 6 else : 7 rstr = rstr + c 8 return rstr String Analysis 1. Write a function noofwords that takes a string and returns the number of words in that string. Words may be separated by multiple spaces! Do not use split. 1 def noofwords ( istr ): 2 readingaword = False 3 totalwords = 0 4 for c in istr : 5 if c == : 6 if readingaword : 7 readingaword = False 8 else : 9 if not readingaword : 10 readingaword = True 11 totalwords = totalwords + 1 12 return totalwords 14
Chapter 4 Lists 4.1 Iterative Questions Iterating through a list 1. Iterate through the members of a list. 3 ways: 1 lst = [1, a, " astring ", [1,2,3]] 2 for m in lst : 3 print (m) 1 lst = [1, a, " astring ", [1,2,3]] 2 for i in range ( len ( lst )): 3 print ( lst [i]) 1 lst = [1, a, " astring ", [1,2,3]] 2 i=0 3 while i<len ( lst ): 4 print ( lst [i]) 5 i=i+1 2. Write a function minmax That prints both the minimum and the max values contained in a list of number. 1 def minmax ( lst ): 2 min = lst [0] 3 max = lst [0] 4 for n in lst : 5 if n<min : 6 min = n 15
7 if n>max : 8 max = n 9 print (" Min : ", min ) 10 print (" Max : ", max ) 3. Write a function that takes a list of numbers and a number n as input and generates a new list consisting of all the number in the given list that are greater than n. 1 def greatern (lst, n): 2 rlst = [] 3 for i in lst : 4 if i>n: 5 rlst. append (i) 6 return rlst Mutating lists 1. Write a function squarelist that takes a list of numbers as input and applies the square function to each member of the list : 1 def squarelist ( lst ): 2 for i in range ( len ( lst )): 3 lst [i] = lst [i ]**2 Forming new lists 1. Write a function firstnsqs that takes a number n as input and return a list of the squares of the the first n integers. : 1 def firstnsqs (n): 2 rlst = [] 3 for i in range (1, n +1) : 4 rlst. append (i **2) 5 return rlst 2. Write a function strtolist that takes a string as input and returns a list of all the characters in the string. : 1 def strtolist ( str ): 2 rlst = [] 3 for s in str : 4 rlst. append (s) 5 return rlst 16