Adam � 2019-04-07 08:53 采纳率: 0%
浏览 717

Python For循环和索引的问题,求助

问题给出了一个‘压缩后’的文字,要求输出一个解压后的文字。
如:输入hel(-1)o,输出hello。
(-1)是指代前一位重复出现过的字母,如图所示图片说明

由于只学过入门的for循环和index的切片等操作。我尝试用i.isdigit()找到字符串里的数字,然后定位它的index,但是如果出现了一样的数字定位就会有问题

图片说明

求大家帮助~谢谢
Text to decompress? hel(-1)o
hello

Text to decompress? a(-1)rdv(-4)(-4)k
aardvark

Text to decompress? The cat(-4)in(-3)(-5)(-11)(-11)(-4)(-3)(-11)(-6)
The cat in the hat

  • 写回答

2条回答 默认 最新

  • 狂颜 2019-04-07 21:25
    关注
    decompress = list(input('Text to decompress?'))
    
    output = ''
    flag = 0
    thechar = ''
    theint = 0
    for i,value in enumerate(decompress):
        if value == '(':
            flag = 1
            continue
    
        if value == '-' and flag == 1:
            flag = 2
            continue
    
        if value == ')' and flag == 2:
            flag = 0
            output += output[ len(output) - theint]
            theint = 0
            continue
    
        if flag == 2:
            theint = theint * 10 + int(value)
            continue
    
        if flag == 0:
            output += value
    
    print(output)
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab