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 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?