Skip to content

Instantly share code, notes, and snippets.

@ajschumacher
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajschumacher/19964fafcfd3a26c42af to your computer and use it in GitHub Desktop.
Save ajschumacher/19964fafcfd3a26c42af to your computer and use it in GitHub Desktop.
easy way
def longest_ordered(original):
results = list()
for item in original:
for stem in tuple(results):
if stem[-1] <= item:
results.append(stem + [item])
results.append([item])
max_length = max(map(len, results))
return [x for x in results if len(x) == max_length]
# usage: longest_ordered([1,2,5,4,6]) etc.
longest.ordered <- function(original) {
results <- list()
for (item in original) {
static_results <- results
for (stem in static_results) {
if (stem[length(stem)] <= item) {
results[[length(results)+1]] <- c(stem, item)
}
}
results[[length(results)+1]] <- item
}
return(results[which.max(sapply(results, length))])
}
# usage: longest.ordered(c(1,2,5,4,6)) etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment