摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法一解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題
刷題繼續(xù)
昨天和大家分享了51-60題,今天繼續(xù)來(lái)刷61~70題
Question 61: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解法一
def f(n): if n < 2: return n return f(n-1) + f(n-2) n = int(input()) print(f(n))Question 62:
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解法一
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)Question 63:
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
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))Question 64:
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解法一
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))Question 65:
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)Question 66:
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:*
35 + 3
Then, the output of the program should be:
38解法一
expression = input() ans = eval(expression) print(ans)Question 67:
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)Question 68:
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)Question 69:
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)Question 70:
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上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載:
Python 61-70題
我的運(yùn)行環(huán)境Python 3.6+,如果你用的是Python 2.7版本,絕大多數(shù)不同就體現(xiàn)在以下3點(diǎn):
raw_input()在Python3中是input()
print需要加括號(hào)
fstring可以換成.format(),或者%s,%d
謝謝大家,我們下期見(jiàn)!希望各位朋友不要吝嗇,把每道題的更高效的解法寫在評(píng)論里,我們一起進(jìn)步!!!
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/43962.html
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法二解法一解法一解法二解法一解法一解法二解法一解法一解法二解法一解法二解法一解法二解法三解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下 刷題繼續(xù) 昨天和大家分享了61-70題,今天繼續(xù)來(lái)刷71~80題 Question 71: Please write a program to out...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法一解法二解法一解法一解法一解法一解法二解法一解法二解法一解法二解法三解法一解法一解法二源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題我的運(yùn) 刷題繼續(xù) 昨天和大家分享了71-80題,今天繼續(xù)來(lái)刷81~90題 Question 81: By using list comprehension, p...
摘要:笨辦法學(xué)第版結(jié)構(gòu)非常簡(jiǎn)單,共包括個(gè)習(xí)題,其中個(gè)覆蓋了輸入輸出變量和函數(shù)三個(gè)主題,另外個(gè)覆蓋了一些比較高級(jí)的話題,如條件判斷循環(huán)類和對(duì)象代碼測(cè)試及項(xiàng)目的實(shí)現(xiàn)等。最后只想說(shuō),學(xué)習(xí)不會(huì)辜負(fù)任何人,笨辦法學(xué) 內(nèi)容簡(jiǎn)介 《笨辦法學(xué)Python(第3版)》是一本Python入門書籍,適合對(duì)計(jì)...
Python 練習(xí)題 --- 梯度下降 題目要求思路講解第一題第二題第一步第二步第三步第四步第五步改進(jìn) 結(jié)尾 題目來(lái)源:在校課程老師布置的作業(yè)偷偷說(shuō)一句:如果對(duì)我的答案和解析滿意的話可不可以給我 點(diǎn)個(gè)贊 , 點(diǎn)個(gè)收藏 之類的Let's do it !!! 題目要求 已知某系統(tǒng)模型可由 ...
摘要:刷題繼續(xù)昨天和大家分享了題,今天繼續(xù)來(lái)刷題解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一解法二解法一解法二解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結(jié)果,可以點(diǎn)擊以下鏈接下載題我的運(yùn)行環(huán)境如果你 刷題繼續(xù) 昨天和大家分享了21-30題,今天繼續(xù)來(lái)刷31~40題 Question 31: Define a function which can pr...
閱讀 1289·2023-04-25 19:33
閱讀 1171·2021-10-21 09:39
閱讀 3644·2021-09-09 09:32
閱讀 2614·2019-08-30 10:58
閱讀 1599·2019-08-29 16:17
閱讀 873·2019-08-29 15:29
閱讀 2885·2019-08-26 11:55
閱讀 2657·2019-08-26 10:33