Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. def long_repeat(line):
  2.  
  3. substrings = []
  4. current_substring = ""
  5. for i, letter in enumerate(line):
  6. if i == 0:
  7. current_substring = letter
  8. else:
  9. if letter == current_substring[len(current_substring) - 1]:
  10. current_substring += letter
  11. if(i == len(line)- 1):
  12. substrings.append(current_substring)
  13. else:
  14. substrings.append(current_substring)
  15. current_substring = letter
  16.  
  17. lengths = []
  18. for substring in substrings:
  19. lengths.append(len(substring))
  20.  
  21. if len(lengths) > 0:
  22. return max(lengths)
  23. else:
  24. return 0
  25.  
  26. def get_length_of_longest_repeated_substring(text):
  27. """Outputs the length of the longest substring with the same letters.
  28.  
  29. Args:
  30. text: Text in which to look for longest repeated substring.
  31.  
  32. Returns:
  33. Length of the longest repeated character.
  34. """
  35. repeated_substrings = []
  36. current_substring = ''
  37. previous_character = ''
  38.  
  39. for i, current_character in enumerate(text):
  40. if not current_substring:
  41. current_substring = current_character
  42. previous_character = current_character
  43. continue
  44.  
  45. if current_character == previous_character:
  46. current_substring += letter
  47. else:
  48. repeated_substrings.append(current_substring)
  49. current_substring = letter
  50.  
  51. if current_substring and len(current_substring) > 1:
  52. repeated_substrings.append(current_substring)
  53.  
  54. max_length = 0
  55. for substring in repeated_substrings:
  56. if len(substring) > max_length:
  57. max_length = len(substring)
  58.  
  59. return max_length
  60.  
  61. def get_length_of_longest_repeated_substring(string, n, i):
  62. i += 1
  63. while (i < n):
  64. current = string[i]
  65. previous = string[i - 1]
  66. if (current == previous):
  67. i += 1
  68. else:
  69. break
  70. return i - 1
  71.  
  72. def largestSubstring(string, n):
  73. length = 0
  74. i = 0
  75. while (i < n):
  76. end = get_length_of_longest_repeated_substring(string, n, i)
  77. length = max(end - i + 1, length)
  78. i = end + 1
  79. return length
  80.  
  81. #string = "aaaaa"
  82.  
  83. #n = len(string)
  84.  
  85. #print(largestSubstring(string, n))
  86.  
  87. >>> 5
  88.  
  89. #string = "sdsffffse"
  90.  
  91. #n = len(string)
  92.  
  93. #print(largestSubstring(string, n))
  94.  
  95. >>> 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement