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)的更多相关文章

  1. Python递归_打印节点信息

    Python递归_打印节点信息 递归特性:1.必须由一个明确的结束条件2.每次进入更深一层递归时,问题规模相比上一次递归都应该有所减少3.递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用时 ...

  2. python递归、collections系列以及文件操作进阶

    global log 127.0.0.1 local2 daemon maxconn log 127.0.0.1 local2 info defaults log global mode http t ...

  3. python入门练习题1

    常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...

  4. Python/ MySQL练习题(一)

    Python/ MySQL练习题(一) 查询“生物”课程比“物理”课程成绩高的所有学生的学号 SELECT * FROM ( SELECT * FROM course LEFT JOIN score ...

  5. python/MySQL练习题(二)

    python/MySQL练习题(二) 查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T ...

  6. 用Python递归解决阿拉伯数字转为中文财务数字格式的问题(2)--打开思路的一种方法

    几天前自己写了个将阿拉伯数字转为中文财务数字的程序.用的递归,不幸的是它是树形递归. 虽然实际过程中不太可能出现金额数字大到让Python递归栈溢出,但是始终是一块心病,这玩意终究在理论上是受限制的. ...

  7. python字典练习题

    python字典练习题 写代码:有如下字典按照要求实现每一个功能dict = {"k1":"v1","k2":"v2", ...

  8. Python递归实现汉诺塔

    Python递归实现汉诺塔: def f3(n,x,y,z): if(n==1): print(x,'--->',z) else: f3(n-1,x,z,y) print(x,'--->' ...

  9. python 递归深度优先搜索与广度优先搜索算法模拟实现

    一.递归原理小案例分析 (1)# 概述 递归:即一个函数调用了自身,即实现了递归 凡是循环能做到的事,递归一般都能做到! (2)# 写递归的过程 1.写出临界条件2.找出这一次和上一次关系3.假设当前 ...

随机推荐

  1. LEB128相关知识

    LEB128相关知识 介绍 LEB128(little endian base 128)是一种变长的整数压缩编码形式,它是出自于DWARF debug file format.在Android的Dal ...

  2. docker 创建docker用户组,应用用户加入用户组

    在Linux系统下使用docker,为了避免每次输入命令都需要sudo,可以把用户加入docker用户组 创建docker用户组 sudo groupadd docker 普通用户加入docker用户 ...

  3. GitHub Pages:静态站点托管服务(待补充)

    不管是 react 还是 vue 项目路由都必须使用 hash 方式,否则页面打不开,切记!!! 如果使用的是 vue-cli 3 创建项目,那么你要在根目录创建 vue.config.js 文件,并 ...

  4. win10自动更新后SQLServer无法启动的问题排查

    今天中午windows提示更新系统补丁并重启后发现,本地的SQL Server服务器没有正常启动,手工启动sqlserver也失败了,报错:找不到ERRORLOG文件及相应目录. 很是奇怪.强制创建该 ...

  5. Ignite(一): 概述

    1.关于Apache Ignite Apache Ignite是一个以内存为中心的分布式数据库.缓存和处理平台,支持事务.分析以及流式负载,可以在PB级数据上享有内存级的性能.比传统的基于磁盘或闪存的 ...

  6. SSHD启动失败,错误码255

    查看/etc/ssh/sshd_config 发现,Listen Address并不是我想要的ip,将其注释掉 sshd restart,结果返回 Permission denied (publick ...

  7. mysql主从原理及配置

    一.mysql集群架构: 1.一主一从 2.双主 3.一主多从(扩展mysql的读性能) 4.多主一从(5.7开始支持) 5.联机复制 关系图: 二.配置主从用途及条件 2.1用途 1.保障可用性,故 ...

  8. UIScrollView的AutoLayout约束

    首先UIScrollview包含自身的frame和contentSize二个部分.frame决定其展示给用户的可见区域,contentSize决定其整个内容的大小.如果frame的宽高小于conten ...

  9. python基础知识5---数据类型、字符编码、文件处理

    阅读目录 一 引子 二 数字 三 字符串 四 列表 五 元组 六 字典 七 集合 八 数据类型总结 九 运算符 十 字符编码 十一 文件处理 十二 作业   一 引子 1 什么是数据? x=10,10 ...

  10. 树莓派 nfs server安装

    安装服务 sudo  apt-get install  portmap sudo  apt-get install  nfs-kernel-server 配置: sudo nano /etc/expo ...