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. Java技术学习之影响MySQL性能的配置参数

    本文将介绍MySQL参数的五大类设置,平时我们一般都很少碰它们,在进行MySQL性能调优和故障诊断时这些参数还是非常有用的. (一)连接连接通常来自Web服务器,下面列出了一些与连接有关的参数,以及该 ...

  2. 转载一篇阿里云Terraform 开发指南

    连接:https://www.jianshu.com/p/0aebea117cae 是一个比较详细的开发指南          

  3. saltstack总结-2018-0620

    以下结论 结论1由于minion配置文件里能配置的只有master的IP和master的ret_port,而无法指定master的publish_port因此minion获取的master的publi ...

  4. 纪念使用FTPClient工具所遇到的

    我所使用的是org.apache.commons.net.ftp.FTPClient.  查了资料还有其余几个FTPClient,其余的先不展开. 1.ftpClient.changeWorkingD ...

  5. 【java】java三种技术架构

    JAVAEE : 企业环境下开发应用程序,servlet,jsp,针对web应用程序的开发. JAVASE:普通简单的桌面应用程序,商务应用程序.(java版扫雷) JAVAME手机应用软件,电子消费 ...

  6. SQL数据库日志清理

    USE [master] GO ALTER DATABASE HCPM_01_181230 SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE HCP ...

  7. Elasticsearch-6.7.0系列(一)9200端口 .tar.gz版本centos7环境--下载安装运行

    https://www.elastic.co/guide/index.html(推荐)        ES官方英文原版文档,一般会更新到最新版本 https://www.elastic.co/cn/d ...

  8. Ubuntu 16.04将左侧面板置于底部

    ctrl+alt+t打开终端,输入: gsettings set com.canonical.Unity.Launcher launcher-position Bottom 有Bottom和Left两 ...

  9. [UE4]虚幻引擎的C++环境安装

    一.一般使用VS2017开发 二.需要勾选“使用C++的游戏开发”

  10. PTA币值转化

    include<stdio.h> include<math.h> int n,s=0; int main(void){ int i,j,k,p=1,flag=1; int f( ...