Skip to content

Instantly share code, notes, and snippets.

@XanderVi
Created October 9, 2020 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XanderVi/8124b8d67a320240c82b6905f8da4554 to your computer and use it in GitHub Desktop.
Save XanderVi/8124b8d67a320240c82b6905f8da4554 to your computer and use it in GitHub Desktop.
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