当前位置:网站首页>Lesson 023 and 024: recursion: these little bunnies, Hanoi Tower after class test questions and answers
Lesson 023 and 024: recursion: these little bunnies, Hanoi Tower after class test questions and answers
2022-06-22 21:35:00 【ChaseTimLee】
use one's hands :
0. Use recursion to write a function that converts decimal to binary ( Required “ take 2 Remainder ” The way , Results and calls bin() Return the string form as well ).
def Dec2Bin(dec):
result = ''
if dec:
result = Dec2Bin(dec//2)
return result + str(dec%2)
else:
return result
print(Dec2Bin(62))
1. Write a function get_digits(n), The parameter n Break down the number of each bit and put it in the list in order . give an example :get_digits(12345) ==> [1, 2, 3, 4, 5]
result = []
def get_digits(n):
if n > 0:
result.insert(0, n%10)
get_digits(n//10)
get_digits(12345)
print(result)
2. Remember the problem of finding palindrome strings ? Now let's use recursion to solve , Can I be proud to say I can ?
def is_palindrome(n, start, end):
if start > end:
return 1
else:
return is_palindrome(n, start+1, end-1) if n[start] == n[end] else 0
string = input(' Please enter a string :')
length = len(string)-1
if is_palindrome(string, 0, length):
print('"%s" It's palindrome string !' % string)
else:
print('"%s" It's not a palindrome string !' % string)
3. Use recursive programming to solve the following problems :
Yes 5 A person sits together , Ask the fifth person how old ? He said bidi 4 Personal big 2 year . Ask No 4 Personal age , He said bidi 3 Personal big 2 year . Ask the third person , And he said bidi 2 People are two years old . Ask No 2 personal , Say two years older than the first one .
Finally, ask the first person , He says it is 10 year . How old is the fifth person ?
def age(n):
if n == 1:
return 10
else:
return age(n-1) + 2
print(' ha-ha , I got it! , The age of the fifth person is %d year , Bobo crisp !' % age(5))
边栏推荐
- ≥server2012R2系统,禁用系统自带的部分计划任务
- 第028讲:文件:因为懂你,所以永恒 | 课后测试题及答案【无标题】
- 94-SQL优化案例一则(用到的写法经常是被嫌弃的)
- 第020讲:函数:内嵌函数和闭包 | 课后测试题及答案
- Baijia forum in the 13th year of Yongzheng (lower part)
- (duc/ddc) digital up mixing / quadrature down mixing principle and MATLAB simulation
- Redis的使用场景分享(项目实战)
- Jericho uses DP and DM for IO key detection [chapter]
- Can the characteristics of different network structures be compared? Ant & meituan & NTU & Ali proposed a cross architecture self supervised video representation learning method CaCl, performance SOTA
- 基于AI驱动大分子药物发现,「华深智药」获近5亿元A轮融资
猜你喜欢

引入稀疏激活机制!Uni-Perceiver-MoE显著提升通才模型的性能

Redis usage scenario sharing (project practice)

NFT,只可远观不可亵玩焉

las 点云创建网格
![[redis]redis的持久化操作](/img/83/9af9272bd485028062067ee2d7a158.png)
[redis]redis的持久化操作

杰理之动态切换 EQ 文件【篇】

Redis learning notes
![List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]](/img/6c/333bc95fe390234d3d06043e4bded1.png)
List of outstanding talents: no crystal vibration, one drag, eight burn and upgrade [chapter]
![[redis] publish and subscribe](/img/50/0c2fbbb8f56fccdd3222b77efdd723.png)
[redis] publish and subscribe

万字长文 | 使用 RBAC 限制对 Kubernetes 资源的访问
随机推荐
[160. cross linked list]
NFT,只可远观不可亵玩焉
redis学习笔记
密码学系列之:PKI的证书格式表示X.509
PHP image making
HarmonyOS应用开发培训第二次
Jerry's plug-in 4m flash to view the processing method with a size of only 1m on the PC [chapter]
软考必备资料大放送,全科目软考资料都给你备好了!
94-SQL优化案例一则(用到的写法经常是被嫌弃的)
The second harmonyos application development training
RealNetworks vs. 微软:早期流媒体行业之争
第025讲:字典:当索引不好用时 | 课后测试题及答案
ACM. HJ24 合唱队 ●●
75- when left join encounters subquery
基于AI驱动大分子药物发现,「华深智药」获近5亿元A轮融资
第029讲:文件:一个任务 | 课后测试题及答案
2022年起重机械指挥考试模拟100题及模拟考试
Redis learning notes
[redis]redis6 master-slave replication
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemmStridedBatched( ha