Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. String cleaning
  2. ===============
  3.  
  4. Your spy, Beta Rabbit, has managed to infiltrate a lab of mad scientists who are turning rabbits into zombies. He sends a text transmission to you, but it is intercepted by a pirate, who jumbles the message by repeatedly inserting the same word into the text some number of times. At each step, he might have inserted the word anywhere, including at the beginning or end, or even into a copy of the word he inserted in a previous step. By offering the pirate a dubloon, you get him to tell you what that word was. A few bottles of rum later, he also tells you that the original text was the shortest possible string formed by repeated removals of that word, and that the text was actually the lexicographically earliest string from all the possible shortest candidates. Using this information, can you work out what message your spy originally sent?
  5.  
  6. For example, if the final chunk of text was "lolol," and the inserted word was "lol," the shortest possible strings are "ol" (remove "lol" from the beginning) and "lo" (remove "lol" from the end). The original text therefore must have been "lo," the lexicographically earliest string.
  7.  
  8. Write a function called answer(chunk, word) that returns the shortest, lexicographically earliest string that can be formed by removing occurrences of word from chunk. Keep in mind that the occurrences may be nested, and that removing one occurrence might result in another. For example, removing "ab" from "aabb" results in another "ab" that was not originally present. Also keep in mind that your spy's original message might have been an empty string.
  9.  
  10. chunk and word will only consist of lowercase letters [a-z].
  11. chunk will have no more than 20 characters.
  12. word will have at least one character, and no more than the number of characters in chunk.
  13.  
  14. Languages
  15. =========
  16.  
  17. To provide a Python solution, edit solution.py
  18. To provide a Java solution, edit solution.java
  19.  
  20. Test cases
  21. ==========
  22.  
  23. Inputs:
  24. (string) chunk = "lololololo"
  25. (string) word = "lol"
  26. Output:
  27. (string) "looo"
  28.  
  29. Inputs:
  30. (string) chunk = "goodgooogoogfogoood"
  31. (string) word = "goo"
  32. Output:
  33. (string) "dogfood"
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. My CODE:::
  44.  
  45. import re
  46.  
  47. def recurse(chunk,word):
  48.  
  49. rev_string = chunk[::-1]
  50. rev_regex = word[::-1]
  51.  
  52. trimmed = re.sub(rev_regex, '', rev_string)[::-1]
  53. # print final
  54. if trimmed != chunk:
  55. return recurse(trimmed, word)
  56. else:
  57. return trimmed
  58.  
  59.  
  60. def answer(chunk, word):
  61. print recurse(chunk, word)
  62. return recurse(chunk, word)
  63.  
  64.  
  65.  
  66. if __name__ == "__main__":
  67. answer("ololololol", 'lol')
  68. answer("goodgoggooooooggooogoooggoofgogooogggooooooogoogoood", 'goo')
  69. answer('llllllllllallllllll', 'llllll')
  70. answer('aabb', 'a')
  71. answer('banaaaannnnana', 'an')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement