Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import string
  2. from random import randint
  3.  
  4.  
  5. def modify_string(word):
  6. """The main function for manipulating the string"""
  7. uppercase_letters = list(string.ascii_uppercase)
  8. lowercase_letters = list(string.ascii_lowercase)
  9. numbers_0_to_9 = [ str(x) for x in range(0, 10) ]
  10. output = ''
  11. all_lists = [uppercase_letters, lowercase_letters, numbers_0_to_9]
  12.  
  13. for letter in word:
  14. char_exists = False
  15. for every_list in all_lists:
  16. position = binary_search(letter, every_list)
  17. if position is not None:
  18. char_exists = True
  19. output += create_word_till_char(letter, position, every_list)
  20. if not char_exists:
  21. output += letter
  22. return output
  23.  
  24. def binary_search(character, list_of_characters, position=0):
  25. """Searches for character using binary seacrh"""
  26. length_of_list = len(list_of_characters)
  27. if length_of_list <= 1:
  28. if length_of_list == 0:
  29. return None
  30. if character != list_of_characters[0]:
  31. return None
  32. return position
  33.  
  34. random_int = randint(0, length_of_list - 1)
  35. if character < list_of_characters[random_int]:
  36. new_list = list_of_characters[:(random_int)]
  37. position += 0
  38. else:
  39. new_list = list_of_characters[random_int:]
  40. position += random_int
  41. # return this so that it recursively comes back to the surface
  42. return binary_search(character, new_list, position)
  43.  
  44.  
  45. def create_word_till_char(character, position, list_of_characters):
  46. """Create word from a list till a given position"""
  47. if not list_of_characters or position is None:
  48. return ''
  49. sliced_list = list_of_characters[: position + 1]
  50. return ''.join(sliced_list)
  51.  
  52.  
  53. if __name__ == '__main__':
  54. print(modify_string(input()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement