Created
March 29, 2011 16:48
-
-
Save proudlygeek/892726 to your computer and use it in GitHub Desktop.
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
7 | |
20 | |
40 | |
9 | |
11 | |
8 | |
4 | |
2 | |
31 |
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
# -*- 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() | |
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
~ 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