Last active
September 26, 2020 15:04
-
-
Save chaps/0290c3a26dd6c98a1b4303fd24b78215 to your computer and use it in GitHub Desktop.
maximum consecutive repeating character
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Test: | |
Write a program that finds maximum consecutive repeating character in a string. | |
Given string: “aaabbbbbcddeeefff”. | |
Output of the program must return the character and the length of its biggest repeating sequence. | |
Output example: | |
Character: p | |
Length of the sequence: 8 | |
""" | |
def result_output(key, value): | |
""" Outputs the excercise results with the requested format | |
""" | |
result_format = """Character: {} | |
Length of the sequence: {} | |
""" | |
return result_format.format(key, value) | |
def biggest_repeating_sequence(input_string): | |
""" Given a string return the maximum consecutive repeating | |
character in the string. | |
""" | |
max_values = (None, 0) | |
tmp = (None, 0) | |
for char in input_string: | |
# Update tmp (increase the counter) | |
# or set the new char and sets the counter to 1 | |
tmp = (char, 1) if char != tmp[0] else (char, tmp[1]+1) | |
# Update max_values in case | |
# the current value of tmp is greater than the max_value | |
# Greater than will return the first biggest repeating sequence | |
# greater than equals will return the last biggest repeating sequence | |
if tmp[1] > max_values[1]: | |
max_values = char, tmp[1] | |
return max_values | |
def main(): | |
samples = [ | |
"aaabbbbbcddeeefff", | |
"a", | |
"", | |
"aaaabbbb", | |
"aaabb", | |
"aabbb", | |
"abbcccdddd", | |
"ddddcccbba" | |
"ddddcccbbaeeeeeffggg" | |
] | |
for sample in samples: | |
print("Input: ", sample) | |
res = biggest_repeating_sequence(sample) | |
print("Result: ", res) | |
print(result_output(*res)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment