LinuxQuestions.org How can I find the longest subsequence in a Python list built by consecutive numbers?
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

 12-15-2022, 04:26 AM #1 Gollum21 LQ Newbie   Registered: Sep 2022 Location: Urbana, Illinios Posts: 5 Rep: How can I find the longest subsequence in a Python list built by consecutive numbers? 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
 12-15-2022, 06:46 AM #2 ntubski Senior Member   Registered: Nov 2005 Distribution: Debian, Arch Posts: 3,779 Rep: There are multiple solutions in the blog post that you linked. Are you having trouble understanding them? Do you have any specific questions?

 Tags python

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Bartonsen Programming 9 04-02-2019 08:36 PM rpittala Linux - Newbie 4 01-30-2012 05:40 PM Mike_V Programming 12 11-24-2010 09:51 AM tcma Linux - General 0 10-19-2004 03:25 PM

LinuxQuestions.org

All times are GMT -5. The time now is 06:35 PM.

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -