Problem Set 2

姓名: 学号:

请注意以下几点:

  1. Write a program to operate a sentence below.

    • count the number of letter e in this sentence.

    • show the host of the email address mail.shufe.edu.cn by using find()

    • show the time in a list ['1st', 'Sept.', '2017', 'Friday'] by using find() and split()

  2. Write a program to create a dictionary that contains the following information.

    NameFavorite LanguageAge
    MikeJava20
    TracyC++21
    JackPython19

    Use for to print the information such that the names are in alphabetical order. The following shows an execution of the program.

  3. Write a program to show the complete multiplication table (九九乘法表). The following shows an execution of the program. (Hint: please use nested for .)

  4. Write a program that creates a function fib(n) to show the Fibonacci sequence. The length of the sequence is n. Fibonacci sequence is a sequence such that every number after the first two is the sum of the two preceding ones. For example, 1,1,2,3,5,8,13,21,34,55,89,... . Note that the third element 2 is the sum of the first two elements, 1+1=2, and the fifth element 5 is the sum of 2 and 3. (8 = 3+5,13=5+8, and so on.) The following shows an execution of the program.

    (Hint: you can start with the following incomplete program. )

  1. Design a function noDuplicates(x) to receive a string and return a new string in which there is no duplicates. The following shows the execution of the function.

  2. Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. The following shows an execution of the function. (Hint: create two functions: one is for finding all the triplets giving the sum of zero, and the other is for deleting duplicates.)