Run-length decompression

前端 未结 6 1544
天涯浪人
天涯浪人 2021-01-26 17:25

CS student here. I want to write a program that will decompress a string that has been encoded according to a modified form of run-length encoding (which I\'ve already written c

6条回答
  •  旧巷少年郎
    2021-01-26 18:28

    Here is a working implementation in python. This also works fine for 2 or 3 or multiple digit numbers

    inputString="a1b3s22d4a2b22"
    inputString=inputString+"\0"                 //just appending a null char
    charcount=""
    previouschar=""
    outputString=""
    
    for char in inputString:
      if char.isnumeric():
        charcount=charcount+char  
      else:
        outputString=outputString
        if previouschar:
          outputString=outputString+(previouschar*int(charcount))   
        charcount=""
        previouschar=char
        
    print(outputString) // outputString= abbbssssssssssssssssssssssffffddaabbbbbbbbbbbbbbbbbbbbbb
    

提交回复
热议问题