I can compute the length of the longest ascending subsequence formed by consecutive numbers in a list, as well as the largest sum of any subsequence.
Code:
def longest_largest_seq(list):
list = [5,6,3,8,3,4,9,8,10,12,11,99,98]
largest = 0
sum = 0
new_list = set(list) #using set to get unique value from the list
max_count = 0
count = 0
for num in list:
if num - 1 not in new_list:
sum = 0
count = 0
while num in new_list:
sum += num
count += 1
num += 1
if sum > largest:
largest = sum
if count > max_count:
max_count = count
return largest, max_count
print(f'Largest consecutive sum and longest consecutive subsequence', longest_largest_seq(list))
However, I'm having trouble printing out the longest subsequence with consecutive digits e.g: 8,9,10,11,12). How am I going to do that? Should I build an empty list and overwrite the values in the new list every time the num value is checked?
Thank you in advance