小练手3

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))
 


posted on 2019-12-13 17:52  不要挡着我晒太阳  阅读(146)  评论(0编辑  收藏  举报

导航