Problem Set 3

姓名: 学号:

请注意以下几点:

  1. Write a program that creates a function fff() to show a 33 table with # randomly distributed in those 9 blocks. The following shows an execution of the program.

    Since you will use the module random, please add import random at the beginning of your program so that you can use random.random() > 0.8 . The statement random.random() randomly generates a number in (0,1). Note that any number in (0,1) can be a threshold instead of 0.8 .

  2. Write a program that creates a closure function such that it returns a geometric sequence (等比数列), arn, with 5 items, where r0 is the common ratio defined by the outer function named input_ratio(r), and a is a scale factor used by the inner function named scale_factor(a). The following shows an execution of the program.

  3. Write a program that creates a decorator. The function to be decorated is given as follows.

    The following shows an execution of your program. Note that the number 4 in the last line means there are 4 numbers in the tuple. In other words, it calculates the number of arguments. Please do not simply add a sentence.

  4. Write a program that creates a class named Food with two attributes which are name and price. Then, create two methods: one is to show the name and price, and the other is to update the price. Then, create a subclass Fruit which is a child class of the parent class Food. The class Fruit inherits the two attributes from its parent class and has its own attribute about its producing area. Then create a method of the child class to show its producing area. The following shows an execution of the program.

  1. Write a program that creates a class named Num to receive a sorted array of numbers without duplicates as an attribute. Create a method sip (search insert position) to receive a target value, the method return the index if the target is found in the array. If not, return the index where it would be if it were inserted in order. The following shows the execution of the function. (Do not use any build-in function or method.)

  2. Write a program that creates a class named Words to receive an array of strings as an attribute. Then create a method to find the longest common prefix string amongst the array of strings. The following shows an execution of the method. (Hint: find the shortest word at first.)