Created
April 11, 2013 05:38
-
-
Save sampottinger/5361003 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Mitchell Wolfe | |
| # mitchell.wolfe@colorado.edu | |
| ########################################################### | |
| # Find longest subsequence | |
| def subsequence(numbers): | |
| # create a list with the first number of inputed sequence | |
| # Go through entire list | |
| subseq_found = [] | |
| for j in range(1, len(numbers)): | |
| subseq = list() | |
| subseq.append(numbers[j]) | |
| for i in range(j, len(numbers)): | |
| # If the list has a single item | |
| if(len(subseq) < 2): | |
| # If the next number in number is larger than | |
| # the current value in subseq, add it to subseq | |
| if(numbers[i] > subseq[len(subseq) - 1]): | |
| subseq.append(numbers[i]) | |
| # otherwise, replace the current value with | |
| # the next value in numbers | |
| else: | |
| subseq[len(subseq) - 1] = numbers[i] | |
| # If the list has 2 or more items | |
| else: | |
| # if the next number in numbers is larger than the | |
| # last value in subseq, add it to the list | |
| if numbers[i] > subseq[len(subseq) - 1]: | |
| subseq.append(numbers[i]) | |
| # If the next number in number is small then the | |
| # last value in subseq and bigger than the second | |
| # to last value in subseq replace the last value with | |
| # the next number in numbers | |
| elif numbers[i] < subseq[len(subseq) - 1] and numbers[i] > subseq[len(subseq) - 2]: | |
| subseq[len(subseq) - 1] = numbers[i] | |
| # return the subsequence | |
| subseq_found.append(subseq) | |
| return max(subseq_found, key=lambda x: len(x)) | |
| ########################################################### | |
| # Get list of numbers | |
| print "Input Sequence: " | |
| numbers = raw_input().split() | |
| # change the list into Integers | |
| for i in range(0, len(numbers)): | |
| numbers[i] = int(numbers[i]) | |
| # Get the subsequence | |
| sub = subsequence(numbers) | |
| # Print length and subsequence | |
| print "Lenght: " + str(len(sub)) | |
| print "Longest: Increasing Subsequence: " + str(sub) | |
| #End of code | |
| ########################################################### | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment