Created
October 9, 2020 11:48
-
-
Save XanderVi/8124b8d67a320240c82b6905f8da4554 to your computer and use it in GitHub Desktop.
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
import random | |
NUMBER_OF_TESTCASES = 100 | |
LETTERS = [chr(i) for i in range(97, 97+26)] # all letters from 'a' to 'z' | |
def random_string_generator(): | |
string_blocks = random.randint(1, 100) | |
string = '' | |
for i in range(string_blocks): | |
string += random.choice(LETTERS) * random.randint(1, 10) | |
return string | |
# input string should consist of 1 char at least | |
def longest_duplicates_counter(string): | |
max_number_of_duplicates = 1 | |
best_candidate = string[0] | |
current_number_of_duplicates = 1 | |
current_candidate = string[0] | |
for i in range(1, len(string)): | |
if string[i] == current_candidate: | |
current_number_of_duplicates += 1 | |
# if we need first best candidate - use >, if we need last - use >= | |
if current_number_of_duplicates > max_number_of_duplicates: | |
max_number_of_duplicates += 1 | |
best_candidate = current_candidate | |
else: | |
current_number_of_duplicates = 1 | |
current_candidate = string[i] | |
print(f'String is: {string}') | |
print(f'Character: {best_candidate}') | |
print(f'Length of the sequence:: {max_number_of_duplicates}') | |
for case in range(NUMBER_OF_TESTCASES): | |
longest_duplicates_counter(random_string_generator()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment