Advertisement
1sairandhri

decompress strings

Jun 18th, 2024
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. def decompress_string(compressed_str):
  2.     decompressed_str = ""
  3.     i = 0
  4.     while i < len(compressed_str):
  5.         char = compressed_str[i]
  6.         i += 1
  7.         count = 0
  8.         while i < len(compressed_str) and compressed_str[i].isdigit():
  9.             count = count * 10 + int(compressed_str[i])
  10.             i += 1
  11.         decompressed_str += char * (count if count > 0 else 1)
  12.     return decompressed_str
  13.  
  14. # Example usage
  15. compressed_str = "a3b2c"
  16. decompressed_str = decompress_string(compressed_str)
  17. print(decompressed_str)  # Output: aaabbc
  18.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement