Advertisement
gustbs

Untitled

Mar 19th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. # https://www.codewars.com/kata/51c8e37cee245da6b40000bd/train/python
  2.  
  3. """
  4. Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out.
  5.  
  6. Example:
  7.  
  8. Given an input string of:
  9.  
  10. apples, pears # and bananas
  11. grapes
  12. bananas !apples
  13. The output expected would be:
  14.  
  15. apples, pears
  16. grapes
  17. bananas
  18.  
  19. The code would be called like so:
  20.  
  21. result = solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"])
  22. # result should == "apples, pears\ngrapes\nbananas"
  23.  
  24. """
  25.  
  26. def solution(string, markers):
  27.     index_line_break = [index for index, char in enumerate(string) if char == '\n']
  28.     for char_special in markers:
  29.         for i in index_line_break:
  30.             if i > string.index(char_special):
  31.                 new_string = string.replace(string[string.index(char_special):i], "")
  32.             elif i < string.index(char_special):
  33.                 new_string = string.replace(string[string.index(char_special)::], "")
  34.     return new_string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement