前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python基础练习100题 ( 61

Python基础练习100题 ( 61

作者头像
py3study
发布2020-01-03 18:02:12
4930
发布2020-01-03 18:02:12
举报
文章被收录于专栏:python3python3

刷题继续

昨天和大家分享了51-60题,今天继续来刷61~70题

Question 61:

The Fibonacci Sequence is computed based on the following formula:

代码语言:javascript
复制
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:*

代码语言:javascript
复制
7

Then, the output of the program should be:

代码语言:javascript
复制
13
解法一
代码语言:javascript
复制
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:

代码语言:javascript
复制
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:*

代码语言:javascript
复制
7

Then, the output of the program should be:

代码语言:javascript
复制
0,1,1,2,3,5,8,13
解法一
代码语言:javascript
复制
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:*

代码语言:javascript
复制
10

Then, the output of the program should be:

代码语言:javascript
复制
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.


解法一
代码语言:javascript
复制
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:*

代码语言:javascript
复制
100

Then, the output of the program should be:

代码语言:javascript
复制
0,35,70
解法一
代码语言:javascript
复制
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.


解法一
代码语言:javascript
复制
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:*

代码语言:javascript
复制
35 + 3

Then, the output of the program should be:

代码语言:javascript
复制
38
解法一
代码语言:javascript
复制
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.


解法一
代码语言:javascript
复制
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.


解法一
代码语言:javascript
复制
import random
rand_num = random.uniform(10,100)
print(rand_num)
解法二
代码语言:javascript
复制
import random
print(random.random()*100)

Question 69:

Please generate a random float where the value is between 5 and 95 using Python module.


解法一
代码语言:javascript
复制
import  random 
print(random.random()*100-5)
解法二
代码语言:javascript
复制
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.


解法一
代码语言:javascript
复制
import random
resp = [i for i in range(2,11,2)]
print(random.choice(resp))
解法二
代码语言:javascript
复制
import random
even_numbers = [x for x in range(0,11) if x%2==0]
print(random.choice(even_numbers))

源代码下载

这十道题的代码在我的github上,如果大家想看一下每道题的输出结果,可以点击以下链接下载:

我的运行环境Python 3.6+,如果你用的是Python 2.7版本,绝大多数不同就体现在以下3点:

  • raw_input()在Python3中是input()
  • print需要加括号
  • fstring可以换成.format(),或者%s,%d

谢谢大家,我们下期见!希望各位朋友不要吝啬,把每道题的更高效的解法写在评论里,我们一起进步!!!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 刷题继续
    • Question 61:
      • Question 62:
        • Question 63:
          • Question 64:
            • Question 65:
              • Question 66:
                • Question 67:
                  • Question 68:
                    • Question 69:
                      • Question 70:
                      • 源代码下载
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档