当前位置:网站首页>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))
边栏推荐
- Cannot re-register id: PommeFFACompetition-v0问题解决
- 第014-15讲:字符串 (见小甲鱼新版27讲-32讲)| 课后测试题及答案
- 第031讲:永久存储:腌制一缸美味的泡菜 | 课后测试题及答案
- 2022 question bank and simulated examination for work license of main principals of hazardous chemical business units
- 513. 找树左下角的值 / 剑指 Offer II 091. 粉刷房子
- csv新增一列
- 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
- 引入稀疏激活机制!Uni-Perceiver-MoE显著提升通才模型的性能
- [redis]集群与常见错误
- 2022危险化学品经营单位主要负责人上岗证题库及模拟考试
猜你喜欢

Redis usage scenario sharing (project practice)

鸿蒙第三次培训

不同网络结构的特征也能进行对比学习?蚂蚁&美团&南大&阿里提出跨架构自监督视频表示学习方法CACL,性能SOTA!

ICML2022 | 利用虚拟节点促进图结构学习
![[138. copy linked list with random pointer]](/img/87/b2f1d224cfc627b4311208ccb9e274.png)
[138. copy linked list with random pointer]
![[palindrome structure of or36 linked list]](/img/67/33730a30c715db573e1d4f7e5556da.png)
[palindrome structure of or36 linked list]

第019讲:函数:我的地盘听我的 | 课后测试题及答案

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

Operation of simulation test platform for 2022 Shandong safety officer C certificate test

大势智慧创建倾斜模型和切割单体化
随机推荐
2022 a special equipment related management (elevator) examination questions and simulation examination
第029讲:文件:一个任务 | 课后测试题及答案
数据库总结:mysql在开发过程中常见的问题及优化
PHP image making
Apple Objective-C source code
[redis]redis persistence
Adblock屏蔽百度热搜
DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]
72 results and development suggestions of the latest on-site production system optimization
ACM. HJ45 名字的漂亮度 ●●
Watch,computed和methods的区别
85- have you learned any of these SQL tuning tips?
[redis]Redis6的事务操作
≥server2012R2系统,禁用系统自带的部分计划任务
[redis]三种新数据类型
[book delivery at the end of the article] AI has spread all over the Internet to color old photos. Here is a detailed tutorial!
杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】
5分钟快速上线Web应用和API(Vercel)
70 root cause analysis Oracle database sudden performance problems, who will take the blame
2022化工自动化控制仪表考试练习题及在线模拟考试