Schule als Staat Projekt Web, Dokumente, etc.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
1.6KB

  1. #!/usr/bin/python
  2. # coding: iso-8859-1
  3. import re
  4. longString = "orem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
  5. def firstSpace(source): #function returns first space after 15 characters in source
  6. spaces = [m.start() for m in re.finditer(" ", source)]
  7. return min(x for x in spaces if x > 20)
  8. def returnSearchResult(substring, source): #returns a few characters before the search and a few characters after the search
  9. occurences = [m.start() for m in re.finditer(substring, source)]
  10. result = []
  11. for occurence in occurences:
  12. left = (source[:occurence])[::-1] # reversed string until occurence
  13. right = source[occurence:] # string after occurence
  14. first_spaces = [firstSpace(left), firstSpace(right)]
  15. left_text, right_text = left[:first_spaces[0]], right[:(first_spaces[0]-1)]
  16. left_text = left_text[::-1] #reverses string back to normal
  17. result.append("{0} {1}".format(left_text, right_text)) #adds this to list of search results
  18. return result
  19. if __name__ == "__main__":
  20. substring = str(input("Input substring: "))
  21. result = returnSearchResult(substring, longString)
  22. print(result)
  23. #not done: proper error handling for firstSpace (no min)
  24. #space recognition still has bugs