小练手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))
分类:
小练习
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人