LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-15-2022, 05:26 AM   #1
Gollum21
LQ Newbie
 
Registered: Sep 2022
Location: Urbana, Illinios
Posts: 5

Rep: Reputation: 0
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
 
Old 12-15-2022, 07:46 AM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,682

Rep: Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008Reputation: 2008
There are multiple solutions in the blog post that you linked. Are you having trouble understanding them? Do you have any specific questions?
 
  


Reply

Tags
python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check if a list of numbers are between a range of numbers from file Bartonsen Programming 9 04-02-2019 09:36 PM
how to find the 1 number out of 10 numbers, if I have taken 9 numbers from my brain ? rpittala Linux - Newbie 4 01-30-2012 06:40 PM
[SOLVED] find the total of numbers that are higher than x in a text file with numbers (using awk??) Mike_V Programming 12 11-24-2010 10:51 AM
Add consecutive numbers to a Chinese file tcma Linux - General 0 10-19-2004 04:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration