Python打卡Day16

Question 60

Question

Write a program to compute:

1f(n)=f(n-1)+100 when n>0
2and f(0)=0

with a given n input by console (n>0).

Example: If the following n is given as input to the program:

15

Then, the output of the program should be:

1500

In case of input data being supplied to the question, it should be assumed to be a console input.


Hints

We can define recursive function in Python.


Main author's Solution: Python 2

1def f(n):
2    if n==0:
3        return 0
4    else:
5        return f(n-1)+100
6
7n=int(raw_input())
8print f(n)

My Solution: Python 3

1def f(n):
2    if n == 0:
3        return 0
4    return f(n-1) + 100
5
6n = int(input())
7print(f(n))

Question 61

Question

The Fibonacci Sequence is computed based on the following formula:

1f(n)=0 if n=0
2f(n)=1 if n=1
3f(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:

17

Then, the output of the program should be:

113

In case of input data being supplied to the question, it should be assumed to be a console input.


Hints

We can define recursive function in Python.


Main author's Solution: Python 2

1def f(n):
2    if n == 0: return 0
3    elif n == 1: return 1
4    else: return f(n-1)+f(n-2)
5
6n=int(raw_input())
7print f(n)

My Solution: Python 3

1def f(n):
2    if n < 2:
3        return n
4    return f(n-1) + f(n-2)
5
6n = int(input())
7print(f(n))

Question 62

Question

The Fibonacci Sequence is computed based on the following formula:

1f(n)=0 if n=0
2f(n)=1 if n=1
3f(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:

17

Then, the output of the program should be:

10,1,1,2,3,5,8,13

In case of input data being supplied to the question, it should be assumed to be a console input.


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.


Main author's Solution: Python 2

1def f(n):
2    if n == 0: return 0
3    elif n == 1: return 1
4    else: return f(n-1)+f(n-2)
5
6n=int(raw_input())
7values = [str(f(x)) for x in range(0, n+1)]
8print ",".join(values)

My Solution: Python 3

 1def f(n):
 2    if n < 2:
 3        fibo[n] = n
 4        return fibo[n]
 5    fibo[n] = f(n-1) + f(n-2)
 6    return fibo[n]
 7
 8n = int(input())
 9fibo = [0]*(n+1)  # initialize a list of size (n+1)
10f(n)              # call once and it will set value to fibo[0-n]
11fibo = [str(i) for i in fibo]   # converting integer data to string type
12ans = ",".join(fibo)    # joining all string element of fibo with ',' character
13print(ans)

 1
 2'''Solution by: popomaticbubble
 3'''
 4def fibo(n):
 5    if n < 2: return n
 6	return fibo(n-1)+fibo(n-2)
 7
 8def print_fiblist(n):
 9    fib_list = [(str(fibo(i))) for i in range(0, n+1)]
10    return print(",".join(fib_list))
11n = int(input())
12print_fiblist(n)

Question 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:

110

Then, the output of the program should be:

10,2,4,6,8,10

In case of input data being supplied to the question, it should be assumed to be a console input.


Hints

Use yield to produce the next value in generator.


Solution:

 1def EvenGenerator(n):
 2    i=0
 3    while i<=n:
 4        if i%2==0:
 5            yield i
 6        i+=1
 7
 8
 9n=int(raw_input())
10values = []
11for i in EvenGenerator(n):
12    values.append(str(i))
13
14print ",".join(values)

OR

1# Solution by: StartZer0
2n = int(input())
3
4for i in range(0, n+1, 2):
5  if i < n - 1:
6    print(i, end = ',' )
7  else:
8    print(i)

Question 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:

1100

Then, the output of the program should be:

10,35,70

In case of input data being supplied to the question, it should be assumed to be a console input.


Hints

Use yield to produce the next value in generator.


Main author's Solution: Python 2

 1def NumGenerator(n):
 2    for i in range(n+1):
 3        if i%5==0 and i%7==0:
 4            yield i
 5
 6n=int(raw_input())
 7values = []
 8for i in NumGenerator(n):
 9    values.append(str(i))
10
11print ",".join(values)

My Solution: Python 3

1def generate(n):
2    for i in range(n+1):
3        if i % 35 == 0:    # 5*7 = 35, if a number is divisible by a & b then it is also divisible by a*b
4            yield i
5
6n = int(input())
7resp = [str(i) for i in generate(n)]
8print(",".join(resp))

go to previous day

go to next day

Discussion