find_longer_consecutive_streak

Run Settings
LanguagePython
Language Version
Run Command
# find the longer consecutive streak in an array # example1 : [6,8,12,7,22,2] # longer streak is 3 (6,7,8) # example2: [9,7,6,2,5,1,3,8,4,11,12,13] # longer streak is 9 (1 until 9) def find_longer_consecutive_streak(array): hashed_array = {} highest_streak = 0 count = 1 for element in array: hashed_array[element] = True for element in array: next_number = element+1 while next_number in hashed_array: count += 1 next_number += 1 if count > highest_streak: highest_streak = count # print(f"highest streak is {highest_streak}, from {element} to {next_number-1}") count = 1 return highest_streak print("Hello World!") sample1 = [6,8,12,7,22,2] result1 = find_longer_consecutive_streak(sample1) print(result1) assert(result1 == 3) sample2 = [11,9,7,2,5,1,3,8,4,12,13,6] result2 = find_longer_consecutive_streak(sample2) print(result2) assert(result2 == 9) print("Goodbye World")
Editor Settings
Theme
Key bindings
Full width
Lines