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

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)
    
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常