昨天和你們分享了51-60題,今天繼續來刷61~70題git
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:*github
7
Then, the output of the program should be:
13
def f(n): if n < 2: return n return f(n-1) + f(n-2) n = int(input()) print(f(n))
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:*express
7
Then, the output of the program should be:
0,1,1,2,3,5,8,13
def f(n): if n < 2: fibo[n] = n return fibo[n] fibo[n] = f(n-1) + f(n-2) return fibo[n] n = int(input()) fibo = [0]*(n+1) # initialize a list of size (n+1) f(n) fibo = [str(i) for i in fibo] ans = ",".join(fibo) print(ans)
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:*app
10
Then, the output of the program should be:
0,2,4,6,8,10
In case of input data being supplied to the question, it should be assumed to be a console input.
def get_evennumbers(x): for x in range(0,x+1): if x%2==0: yield x input_number=int(input()) values = [] for i in get_evennumbers(input_number): values.append(str(i)) print(",".join(values))
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:*dom
100
Then, the output of the program should be:
0,35,70
def devision_seven_five(x): for x in range(0,x+1): if x%35==0: yield x input_number=int(input()) values = [] resp = [str(i) for i in devision_seven_five(input_number)] print(",".join(resp))
Please write assert statements to verify that every number in the list [2,4,6,8] is even.
data = [2,4,5,6] for i in data: assert i%2 == 0, "{} is not an even number".format(i)
Please write a program which accepts basic mathematic expression from console and print the evaluation result.*Example:
If the following n is given as input to the program:*lua
35 + 3
Then, the output of the program should be:
38
expression = input() ans = eval(expression) print(ans)
Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list.
from bisect import bisect_right def BinarySearch(a, x): i = bisect_right(a, x) if i != len(a)+1 and a[i-1] == x: return (i-1) else: return -1 lst = [1,2,4,5,6,7,8] x = int(input()) res = BinarySearch(lst, x) if res == -1: print(x, "is absent") else: print("Last occurrence of", x, "is present at", res)
Please generate a random float where the value is between 10 and 100 using Python module.
import random rand_num = random.uniform(10,100) print(rand_num)
import random print(random.random()*100)
Please generate a random float where the value is between 5 and 95 using Python module.
import random print(random.random()*100-5)
import random rand_num = random.uniform(5,95) print(rand_num)
Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension.
import random resp = [i for i in range(2,11,2)] print(random.choice(resp))
import random even_numbers = [x for x in range(0,11) if x%2==0] print(random.choice(even_numbers))
這十道題的代碼在個人github上,若是你們想看一下每道題的輸出結果,能夠點擊如下連接下載:code
個人運行環境Python 3.6+,若是你用的是Python 2.7版本,絕大多數不一樣就體如今如下3點:orm
謝謝你們,咱們下期見!但願各位朋友不要吝嗇,把每道題的更高效的解法寫在評論裏,咱們一塊兒進步!!!ip