---61---
Question:
>The Fibonacci Sequence is computed based on the following formula:
f(n)=0 if n=0
f(n)=1 if n=1
f(n)=f(n-1)+f(n-2) if n>1
>Please write a program to compute the value of f(n) with a given n input by console.
Example:
>If the following n is given as input to the program: 7
>Then, the output of the program should be: 13
Hints:
>We can define recursive function in Python.
Solution:
---62---
Question:
>The Fibonacci Sequence is computed based on the following formula:
f(n)=0 if n=0
f(n)=1 if n=1
f(n)=f(n-1)+f(n-2) if n>1
>Please write a program to compute the value of f(n) with a given n input by console.
Example:
>If the following n is given as input to the program: 7
>Then, the output of the program should be: 0,1,1,2,3,5,8,13
Hints:
>We can define recursive function in Python.
>Use list comprehension to generate a list from an existing list.
>Use string.join() to join a list of strings.
Solution:
解法一
解法二
---63---
Question:
>Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console.
Example:
>If the following n is given as input to the program: 10
>Then, the output of the program should be: 0,2,4,6,8,10
Hints:
>Use yield to produce the next value in generator.
Solution:
解法一
解法二
---64---
Question:
Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console.
Example:
>If the following n is given as input to the program: 100
>Then, the output of the program should be: 0,35,70
Hints:
>Use yield to produce the next value in generator.
Solution:
---65---
Question:
>Please write assert statements to verify that every number in the list [2,4,6,8] is even.
网友评论