Skip to content

Instantly share code, notes, and snippets.

@proudlygeek
Created March 29, 2011 16:48
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 proudlygeek/892726 to your computer and use it in GitHub Desktop.
Save proudlygeek/892726 to your computer and use it in GitHub Desktop.
7
20
40
9
11
8
4
2
31
# -*- coding: utf-8 -*-
def sublists(lista, func):
tmp_list = []
for index, item in enumerate(lista):
try:
tmp_list.append(item)
if not func(item, lista[index + 1]):
yield tmp_list
tmp_list = []
except IndexError:
yield tmp_list
def main():
lista = [int(num) for num in open("input.txt")]
results_asc = sublists(lista, lambda x, y: x <= y)
results_desc = sublists(lista, lambda x, y: x > y)
all_results = []
print "~ Sottoliste di ordine crescente ~"
for item in results_asc:
print item
all_results.append(item)
print "~ Sottoliste di ordine descrescente ~"
for item in results_desc:
print item
all_results.append(item)
longest_seq = max([(len(substring), substring) for substring in all_results])
print "~ Risultato ~"
print "La sequenza ordinata più lunga è {0} => lunghezza {1} ~" \
.format(longest_seq[1], longest_seq[0])
if __name__ == '__main__':
main()
~ Sottoliste di ordine crescente ~
[7, 20, 40]
[9, 11]
[8]
[4]
[2, 31]
~ Sottoliste di ordine descrescente ~
[7]
[20]
[40, 9]
[11, 8, 4, 2]
[31]
~ Risultato ~
La sequenza ordinata più lunga è [11, 8, 4, 2] => lunghezza 4 ~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment