Saturday, February 22, 2020

Chapter-13 (def function)   Solution of Practice Question



Ques-1: Write a function called rectangle that takes two integers m and n as arguments and prints out an m×n box consisting of asterisks. Shown below is the output of rectangle(2,4) 

**** 
**** 

Solution:


Ques-2:  (a) Write a function called add_excitement that takes a list of strings and adds an exclamation point (!) to the end of each string in the list. The program should modify the original list and not return anything. 

(b) Write the same function except that it should not modify the original list and should instead return a new list

Solution:


Ques-3: Write a function called sum_digits that is given an integer num and returns the sum of the digits of num. 


Solution:



Ques-4:The digital root of a number n is obtained as follows: Add up the digits n to get a new number. Add up the digits of that to get another new number. Keep doing this until you get a number that has only one digit. That number is the digital root.

For example, if n=45893, we add up the digits to get 4+5+8+9+3=29. We then add up the digits of 29 to get 2+9 = 11. We then add up the digits of 11 to get 1+1 = 2. Since 2 has only one digit, 2 is our digital root. Write a function that returns the digital root of an integer n. [Note: there is a shortcut, where the digital root is equal to n mod 9, but do not use that here.

Solution:


Ques-5:Write a function called first_diff that is given two strings and returns the first location in which the strings differ. If the strings are identical, it should return -1.


Solution:


Ques-6:  Write a function called binom that takes two integers n and k and returns the binomial coefficient (n/ k). The definition is (n/k)=n!/k!(n-k)!


Solution:


Ques-7:Write a function that takes an integer n and returns a random integer with exactly n digits. For instance, if n is 3, then 125 and 593 would be valid return values, but 093 would not because that is really 93, which is a two-digit number. 


Solution:


Ques-8: Write a function called number_of_factors that takes an integer and returns how many factors the number has. 


Solution:


Ques-9:  Write a function called factors that takes an integer and returns a list of its factors.


Solution: Same as above only some modification do yourself


Ques-10: Write a function called closest that takes a list of numbers L and a number n and returns the largest element in L that is not larger than n. For instance, if L=[1,6,3,9,11] and n=8, then the function should return 6,because 6 is the closest thing in L to 8 that is not larger than 8. Don’t worry about if all of the things in L are smaller than n.


Solution:


Ques-11:  Write a function called matches that takes two strings as arguments and returns how many matches there are between the strings. A match is where the two strings have the same character at the same index. For instance, 'python' and 'path' match in the first, third, and fourth characters, so the function should return 3


Solution:


Ques-12: Recall that if s is a string, then s.find('a') will find the location of the first a in s. The problem is that it does not find the location of every a. Write a function called findall that given a string and a single character, returns a list containing all of the locations of that character in the string. It should return an empty list if there are no occurrences of the character in the string


Solution: 


Ques-13: Write a function called change_case that given a string, returns a string with each upper case letter replaced by a lower case letter and vice-versa. 


Solution:


Ques-14:  Write a function called is_sorted that is given a list and returns True if the list is sorted and False otherwise



Solution:


Ques-15: Write a function called root that is given a number x and an integer n and returns x1/n. In the function definition, set the default value of n to 2.


Solution:


Ques-16:  Write a function called one_away that takes two strings and returns True if the strings are of the same length and differ in exactly one letter, like bike/hike or water/wafer. 

Solution:


Ques-17:  (a) Write a function called primes that is given a number n and returns a list of the first n primes. Let the default value of n be 100. 

(b) Modifythefunctionabovesothatthereisanoptionalargumentcalledstartthatallows the list to start at a value other than 2. The function should return the first n primes that are greater than or equal to start. The default value of start should be 2. 

Solution:



Ques-18: Our number system is called base 10 because we have ten digits: 0, 1, ..., 9. Some cultures, including the Mayans and Celts, used a base 20 system. In one version of this system, the 20 digits are represented by the letters A through T. Here is a table showing a few conversions:

0 A 1 B 2 C 4 D 5 E 6 F 7 G 8 H 9 I 10 J 11 K 12 ........... (see in practice question)

Write a function called base20 that converts a base 10 number to base 20. It should return the result as a string of base 20 digits. One way to convert is to find the remainder when the number is divided by 20, then divide the number by 20, and repeat the process until the number is 0. The remainders are the base 20 digits in reverse order, though you have to convert them into their letter equivalents.

Solution:


Ques-19: Write a function called verbose that, given an integer less than 1015, returns the name of the integer in English. As an example, verbose(123456) should return one hundred twenty-three thousand, four hundred fifty-six.


Solution:


Ques-20: Write a function called merge that takes two already sorted lists of possibly different lengths, and merges them into a single sorted list.

(a) Do this using the sort method. 
(b) Do this without using the sort method


Solution:



Next 3 question are better understood in graphical pattern so we study in next chapter.


No comments:

Post a Comment

If you have any doubt, let me know.

How and Why you should learn python? About Python Programming Language Python is the programming language. By the help of Python you can ...