当前位置:网站首页>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))
边栏推荐
- How swiftui simulates the animation effect of view illumination increase
- 杰理之开启四声道通话近端卡顿问题【篇】
- 不同网络结构的特征也能进行对比学习?蚂蚁&美团&南大&阿里提出跨架构自监督视频表示学习方法CACL,性能SOTA!
- View Apple product warranty status
- ACM. HJ24 合唱队 ●●
- 2022化工自动化控制仪表考试练习题及在线模拟考试
- php 镜像制作
- 第028讲:文件:因为懂你,所以永恒 | 课后测试题及答案【无标题】
- [redis]三种新数据类型
- DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]
猜你喜欢
![[876. intermediate node of linked list]](/img/c8/463d150bc6c88cfb57e94795957b0e.png)
[876. intermediate node of linked list]

300. 最长递增子序列 ●●
![Jerry's music mode obtains the directory of playing files [chapter]](/img/2f/efb8a077e3e398cb3b14cfd98a8422.png)
Jerry's music mode obtains the directory of playing files [chapter]
How to operate redis on the IntelliJ idea database console
![[206. reverse linked list]](/img/c8/d83655395379a512c8b67a82dc4cae.png)
[206. reverse linked list]

RealNetworks vs. 微软:早期流媒体行业之争

数据库总结:mysql在开发过程中常见的问题及优化

HarmonyOS应用开发培训第二次
![[redis]redis6 transaction operations](/img/50/639867a2fcb92082ea262a8a19bb68.png)
[redis]redis6 transaction operations

Install MySQL in ECS (version 2022)
随机推荐
Baijia forum Daqin rise (lower part)
84- I am on Internet & lt; 52 SQL statement performance optimization strategies & gt; Some views of
优化求解器 | Gurobi的MVar类:矩阵建模利器、求解对偶问题的备选方案 (附详细案例+代码)
[160. cross linked list]
杰理之外挂 4M 的 flash 在 PC 上查看大小只有 1M 的处理方法【篇】
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemmStridedBatched( ha
Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model
Learning websites that programmers must see
嵌入式开发基础之任务管理(线程管理)
71- analysis of an Oracle DBA interview with Alibaba in 2010
79- do not create desc descending index when you see order by XXX desc - there is book donation benefit at the end of the article
ACM. HJ24 合唱队 ●●
How to operate redis on the IntelliJ idea database console
2022化工自动化控制仪表考试练习题及在线模拟考试
Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
[cm11 linked list splitting]
第027讲:集合:在我的世界里,你就是唯一 | 课后测试题及答案
Differences between watch, computed and methods
[the penultimate node in the linked list]
≥server2012R2系统,禁用系统自带的部分计划任务