ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

小练手3

2019-12-13 18:03:50  阅读:212  来源: 互联网

标签:练手 return num print program fun input


1.
"""
# Write a program to compute:
#
# f(n)=f(n-1)+100 when n>0
# and f(0)=1
#
# with a given n input by console (n>0).
#
# Example:
# If the following n is given as input to the program:
#
# 5
#
# Then, the output of the program should be:
#
# 500
# In case of input data being supplied to the question, it should be assumed to be a console input.
"""def fun(num):
    if num == 0:
return 0
elif num == 1:
return 1
else:
return fun(num-1)+100
print(fun(5))

2.
"""

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
In case of input data being supplied to the question, it should be assumed to be a console input.

"""
def fun(num):
if num == 0:
return 0
elif num == 1:
return 1
elif num > 1:
return fun(num-1)+fun(num-2)
print(fun(7))

3.
"""
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 using list comprehension to print the Fibonacci Sequence in comma separated form 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 == 0: return 0
elif n == 1: return 1
else: return f(n-1)+f(n-2)

n=int(input())
values = [str(f(x)) for x in range(0, n+1)]
print(",".join(values))

4.
"""
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.

"""

import math
def bin_search(li, element):
bottom = 0
top = len(li)-1
index = -1
while top>=bottom and index==-1:
mid = int(math.floor((top+bottom)/2.0))
if li[mid]==element:
index = mid
elif li[mid]>element:
top = mid-1
else:
bottom = mid+1

return index

li=[2,5,7,9,11,17,222]
print(bin_search(li,11))
print(bin_search(li,12))

5.
"""
Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!".

"""
import zlib
s = 'hello world!hello world!hello world!hello world!'
t = zlib.compress(s.encode())
print(t)
print(zlib.decompress(t))

6.
"""
Please write a program to generate all sentences where subject is in ["I", "You"] and verb is in ["Play", "Love"] and the object is in ["Hockey","Football"].

"""
def fun():
for i in ["I", "You"]:
for j in ["Play", "Love"]:
for k in ["Hockey","Football"]:
print(i+' '+j+' '+k)

fun()

7.
"""
By using list comprehension, please write a program generate a 3*5*8 3D array whose each element is 0.

"""

array = [[[0 for col in range(8)] for col in range(5)] for row in range(3)]
print (array)

8.
"""
Write a program to solve a classic ancient Chinese puzzle: 
We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have?
"""
def fun(Thead,Tleg):
for i in range(Thead+1):
j = Thead - i
if i*2+j*4 == Tleg:
return i,j

print(fun(35,94))
 


标签:练手,return,num,print,program,fun,input
来源: https://www.cnblogs.com/miaoweiye/p/12036545.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有