Created
June 6, 2017 19:23
-
-
Save prkstaff/a391f7269b169370e081bb4463e6003c to your computer and use it in GitHub Desktop.
longest subsequence printing a sequence given a tuple of number
This file contains 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
class Solution: | |
def get_longest_subsequence(self, tupl): | |
sequence_counter = [] | |
longest_sequence = 1; | |
for i,ivalue in enumerate(tupl): | |
sequence_counter.insert(i,1) | |
for j in reversed(range(i)): | |
print "============" | |
print "Turn: I {0} and J {1}".format(tupl[i],tupl[j]) | |
print "I has index {0}".format(sequence_counter[i]) | |
print "J has index {0}".format(sequence_counter[j]) | |
if(tupl[j] < tupl[i] and sequence_counter[j] >= sequence_counter[i]): | |
print "I neeeeeeeeeds to increeeeease " | |
sequence_counter[i] = sequence_counter[j]+1 | |
print "Now I is {0}".format(sequence_counter[i]) | |
if(sequence_counter[i] > longest_sequence): | |
longest_sequence = sequence_counter[i] | |
print " \n\n ==== \n The longest sequence is {0} \n ==== ".format(longest_sequence) | |
# print a sequence: | |
sequence = [] | |
prev_number = max(tupl) + 1 | |
prev_number_index = len(tupl) + 1 | |
print sequence_counter | |
for k in reversed(range(longest_sequence)): | |
lis = [ tupl[x] for x,y in enumerate(sequence_counter) | |
if y == k+1 and tupl[x] < prev_number | |
and x < prev_number_index] | |
prev_number = max(lis) | |
prev_number_index = tupl.index(prev_number) | |
sequence.insert(0, max(lis)) | |
print sequence | |
myc = Solution() | |
myc.get_longest_subsequence((0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment