#Given a vector of integers, find the longest consecutive sub-sequence of increasing numbers. #If two sub-sequences have the same length, use the one that occurs first. #An increasing sub-sequence must have a length of 2 or greater to qualify. # #Example input: #[1 0 1 2 3 0 4 5] #Result: #[0 1 2 3] def longest_consecutive_increasing(intvect): max_so_far = 0 curr_count = 1 max_pos = -1 #iterate through all integers in the vector for counter1, integer in enumerate(intvect): #case where we just started. No comparison needed yet. if counter1 == 0: pass #case where we have an increase elif intvect[counter1] > intvect[counter1-1]: curr_count += 1 #case where we dont have an increase else: if curr_count > max_so_far: max_pos = counter1-curr_count max_so_far = curr_count curr_count = 1 #final case after exiting loop if curr_count > max_so_far: max_pos = len(intvect)-curr_count max_so_far = curr_count return intvect[max_pos:max_pos+max_so_far] #**** TESTS **** intvect = [1, 0, 1, 2, 3, 0, 4, 5] test_result = longest_consecutive_increasing(intvect) if test_result == [0, 1, 2, 3]: print "Test Passed - Input: " + str(intvect) + " Output: " + str(test_result) else: print "Test Failed - Input: " + str(intvect) + " Output: " + str(test_result) intvect = [1] test_result = longest_consecutive_increasing(intvect) if test_result == [1]: print "Test Passed - Input: " + str(intvect) + " Output: " + str(test_result) else: print "Test Failed - Input: " + str(intvect) + " Output: " + str(test_result) intvect = [5,4,3,2,1] test_result = longest_consecutive_increasing(intvect) if test_result == [5]: print "Test Passed - Input: " + str(intvect) + " Output: " + str(test_result) else: print "Test Failed - Input: " + str(intvect) + " Output: " + str(test_result)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question