【396】python 递归练习题(COMP9021)
Merging two strings into a third one
Say that two strings s1 and s2 can be merged into a third string s3 if s3 is obtained from s1 by inserting arbitrarily in s1 the characters in s2, respecting their order. For instance, the two strings ab and cd can be merged into abcd, or cabd, or cdab, or acbd, or acdb, ..., but not into adbc nor into cbda. Write a program merging_strings.py that prompts the user for 3 strings and displays the output as follows:
If no string can be obtained from the other two by merging, then the program outputs that there is no solution.
Otherwise, the program outputs which of the strings can be obtained from the other two by merging.
def one_char(a, b_list):
for i in range(len(b_list)+1):
tmp = b_list.copy()
tmp.insert(i, a)
yield tmp def whole_char(a_list, b_list):
if len(a_list) == 1:
yield from one_char(a_list[0], b_list)
else:
for li in whole_char(a_list[1:], b_list):
yield from one_char(a_list[0], li) def is_merging(str_1, str_2, str_3):
for tmp in [''.join(li) for li in whole_char(list(str_1), list(str_2))]:
if tmp == str_3:
return True
return False str1 = input("Please input the first string: ")
str2 = input("Please input the second string: ")
str3 = input("Please input the third string: ") if is_merging(str1, str2, str3):
print("The third string can be obtained by merging the other two.")
elif is_merging(str1, str3, str2):
print("The second string can be obtained by merging the other two.")
elif is_merging(str2, str3, str1):
print("The first string can be obtained by merging the other two.")
else:
print("No solution")
测试数据:
str1: abcd
str2: cd
str3: ab
output:
The first string can be obtained by merging the other two.
【396】python 递归练习题(COMP9021)的更多相关文章
- Python递归_打印节点信息
Python递归_打印节点信息 递归特性:1.必须由一个明确的结束条件2.每次进入更深一层递归时,问题规模相比上一次递归都应该有所减少3.递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用时 ...
- python递归、collections系列以及文件操作进阶
global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaults log global mode http t ...
- python入门练习题1
常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...
- Python/ MySQL练习题(一)
Python/ MySQL练习题(一) 查询“生物”课程比“物理”课程成绩高的所有学生的学号 SELECT * FROM ( SELECT * FROM course LEFT JOIN score ...
- python/MySQL练习题(二)
python/MySQL练习题(二) 查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T ...
- 用Python递归解决阿拉伯数字转为中文财务数字格式的问题(2)--打开思路的一种方法
几天前自己写了个将阿拉伯数字转为中文财务数字的程序.用的递归,不幸的是它是树形递归. 虽然实际过程中不太可能出现金额数字大到让Python递归栈溢出,但是始终是一块心病,这玩意终究在理论上是受限制的. ...
- python字典练习题
python字典练习题 写代码:有如下字典按照要求实现每一个功能dict = {"k1":"v1","k2":"v2", ...
- Python递归实现汉诺塔
Python递归实现汉诺塔: def f3(n,x,y,z): if(n==1): print(x,'--->',z) else: f3(n-1,x,z,y) print(x,'--->' ...
- python 递归深度优先搜索与广度优先搜索算法模拟实现
一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件2.找出这一次和上一次关系3.假设当前 ...
随机推荐
- windows进程查看
查看目前使用的端口 netstat -nao 查看目前运行程序的具体路径 命令行输入wmic接着输入process
- Linux 连接mysql
连接MYSQL: 格式: mysql -h主机地址 -u用户名 -p用户密码 1.例1:连接到本机上的MYSQL 找到mysql的安装目录,一般可以直接键入命令mysql -uroot -p,回车后提 ...
- PHP获取新插入的主键id
近期在做订单系统开发的时候遇到了此类情景,A表内插入后返回新插入的主键ID,然后用于B表插入数据并携带此id. 目前有几个方法总结 No1.每次插入数据之后返回A表内的最大值,但是对于多用户以及高并发 ...
- locate中使用variant
利用locate构造多字段查询,采用variant很方便,简介如下, //构造查询多字段,例如'编号;姓名'形式 aLookField := FieldByName ('关键字1').AsStrin ...
- C# 6.0:Auto-Property initializer
在之前的开发中,属性只能在构造函数中进行初始化,如果它有定义一个后台字段的话,那这个字段就就可以在定义的地方初始化.C# 6.0 引进了一个Auto-Property initializer机制使属性 ...
- # 20175311 2018-2019-2 《Java程序设计》第2周学习总结
## 教材学习内容总结 第二周我对如何运行java程序已经比较熟悉了,第二周更多的是注重程序内部的原理了. ## 教材学习中的问题和解决过程 - 问题1:看书时看到的一个例子,不是很懂它是怎么得出结果 ...
- 并发之痛 Thread,Goroutine,Actor
转自:http://jolestar.com/parallel-programming-model-thread-goroutine-actor/ 先梳理下两个概念,几乎所有讲并发的文章都要先讲这两个 ...
- Unity暂停游戏功能
关于暂停游戏功能的做法,网上的教程以及Unity官方发布的Demo都是通过把Time.timeScale设成0来实现的,然而这会导致一些蛋疼的问题,因为Time.timeScale是全局变量,改成0后 ...
- 【原创】Mac book pro入手后,需要做哪些才能开始开展自动化测试工作
2018国庆节,脑袋一热,入手了一台Mac book pro,从此掉坑到了这个异构的操作系统中,因为之前工作中接触了Windows.Linux.Unix等操作系统的诸多版本,基本的操作倒是不成问题,但 ...
- C#开发微信支付之企业向用户付款
1.企业付款的介绍 所谓企业付款指的是,在功能开放后诸如保险行业的客户理赔.退保.商品退款.发放征集活动奖金.抽奖互动等操作都可以通过企业付款完成.而此前,微信支付只能提供客户向企业单向付款. 商户如 ...