SimeonTs

SUPyF2 Text-Pr.-Ex. - 07. String Explosion

Oct 27th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. """
  2. Text Processing - Exercise
  3. Check your code: https://judge.softuni.bg/Contests/Practice/Index/1740#6
  4.  
  5. SUPyF2 Text-Pr.-Ex. - 07. String Explosion
  6.  
  7. Problem:
  8. Explosions are marked with '>'. Immediately after the mark, there will be an integer,
  9. which signifies the strength of the explosion.
  10. You should remove x characters (where x is the strength of the explosion), starting after the punch character ('>').
  11. If you find another explosion mark ('>') while you’re deleting characters,
  12. you should add the strength to your previous explosion.
  13. When all characters are processed, print the string without the deleted characters.
  14. You should not delete the explosion character – '>', but you should delete the integers, which represent the strength.
  15.  
  16. Input
  17. You will receive single line with the string.
  18. Output
  19. Print what is left from the string after explosions.
  20. Constraints
  21. • You will always receive a strength for the punches
  22. • The path will consist only of letters from the Latin alphabet, integers and the char '>'
  23. • The strength of the punches will be in the interval [0…9]
  24. Examples
  25. Input:                              Output:
  26. abv>1>1>2>2asdasd                   abv>>>>dasd
  27.  
  28. Input:                              Output:
  29. pesho>2sis>1a>2akarate>4hexmaster   pesho>is>a>karate>master
  30.  
  31. Comments:
  32. 1st explosion is at index 3 and it is with strength of 1. We delete only the digit after the explosion character.
  33. The string will look like this: abv>>1>2>2asdasd
  34. 2nd explosion is with strength one and the string transforms to this: abv>>>2>2asdasd
  35. 3rd explosion is now with strength of 2. We delete the digit and we find another explosion.
  36. At this point the string looks like this: abv>>>>2asdasd.
  37. 4th explosion is with strength 2. We have 1 strength left from the previous explosion,
  38. we add the strength of the current explosion to what is left and that adds up to a total strength of 3.
  39. We delete the next three characters and we receive the string abv>>>>dasd
  40. We do not have any more explosions and we print the result: abv>>>>dasd
  41. """
  42. text = [item for item in input()]
  43. bomb = 0
  44. i = 0
  45. while True:
  46.     try:
  47.         text[i]
  48.     except Exception:
  49.         break
  50.     if bomb > 0 and text[i] != ">":
  51.         text.pop(i)
  52.         bomb -= 1
  53.         i -= 1
  54.     elif text[i] == ">":
  55.         bomb += int(text[i + 1])
  56.     i += 1
  57. print(''.join(text))
Add Comment
Please, Sign In to add comment