Starting from:
$35

$29

Week 10 Solution


    1. Write a function which can compute the factorial of a given numbers. The results should be printed in a comma-separated sequence on a single line.

Suppose the following input is supplied to the program:

    8 

Then, the output should be: 40320

    2. Write a function that accepts a sentence and calculate the number of letters and digits. Suppose the following input is supplied to the program:

hello world! 123

Then, the output should be: LETTERS 10

DIGITS 3

    3. Write a function to determine whether any real number a, b, c form a triangle or not.

    4. Write a function to calculate the area of a triangle with three edges equal to a, b, c, respectively.

    5. Write a function to find gcd(a, b): the greatest common divisor of two positive integers a, b; e.g., gcd(10,3) = 1, gcd(9,6) = 3.

    6. A year is called a leap year if it is exactly divisible by 4, or if it is divisible by both

    100 and 400.

    7. Write a function to find the three angles of a given triangle with edge lengths equal to a, b, c, respectively.

    8. Please write a function which accepts a string from console and print it in reverse order.

Example:

If the following string is given as input to the program:

rise to vote sir

Then, the output of the program should be:

ris etov ot esir

Hints:

Use list[::-1] to iterate a list in a reverse order.

    9. With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print this list after removing all duplicate values with original order reserved.

    10. Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print all strings line by line.

    11. Define a function that can accept an integer number as input and print the "It is an even number" if the number is even, otherwise print "It is an odd number".

    12. Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (n) (both included) and the values are square of keys. The function should just print the values only.

Hints:

Use dict[key]=value pattern to put entry into a dictionary.

Use ** operator to get power of a number.

Use range() for loops.

Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.

    13. Define a function which can generate and print a list where the values are square of numbers between 1 and n=20 (both included).

Hints:

Use ** operator to get power of a number.

Use range() for loops.

Use list.append() to add values into a list.

    14. Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.

Hints:

Use ** operator to get power of a number.

Use range() for loops.

Use list.append() to add values into a list.

Use [n1:n2] to slice a list

    15. Define a function which can generate a list where the values are square of numbers between 1 and n=20 (both included). Then the function needs to print the last
    5 elements in the list.

Hints:

Use ** operator to get power of a number.

Use range() for loops.

Use list.append() to add values into a list.

Use [n1:n2] to slice a list

Solution

    16. Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print all values except the first 5 elements in the list.

Hints:

Use ** operator to get power of a number.

Use range() for loops.

Use list.append() to add values into a list.

Use [n1:n2] to slice a list



    17. It's a generally accepted belief, to assume that one year in the life a dog corresponds to seven years in the life of a human being. But apparently there are other subtle methods to calculate this haunting problem, haunting at least for some dog owners. Another subtler method works like this:

•  A one-year-old dog roughly corresponds to a fourteen-year-old child

•  A dog who is two years old corresponds to a 22-year-old human

•  Every further dog year corresponds to five human years

Write a program to implement the dog-human-age rule.


18. sieve of Eratosthenes (筛法)

https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

    a) Write a function to determine whether a given integer is prime or not.

    b) Write a function to find all the primes less than 1,000,000

More products