Created
March 28, 2014 16:55
-
-
Save bulv1ne/9837531 to your computer and use it in GitHub Desktop.
How to use python expressive power for solving a level-5 challenge http://impythonist.wordpress.com/2014/03/28/how-to-use-python-expressive-power-for-solving-a-level-5-challenge/
This file contains hidden or 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
numbers = map(int, open("num.txt").readlines()) | |
filtered_list = [] | |
for i in numbers: | |
# Check against last element of last list | |
if filtered_list and filtered_list[-1][-1] < i: | |
# Append element to last list | |
filtered_list[-1].append(i) | |
else: | |
# Create a new list | |
filtered_list.append([i]) | |
# Only get the longuest list from filtered_list | |
longest_list = max(filtered_list, key=lambda l: len(l)) | |
length = len(longest_list) | |
total = abs(sum(longest_list)) | |
print "The longest sequence no in File is: %d and sum is %d"% (length, total) |
This file contains hidden or 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
fil=open("num.txt","r").readlines() | |
mylo=[] | |
for l in fil: | |
mylo.append(int(l)) | |
long_seq,i,temp,max_seq_index=0,0,1,0 | |
while i<len(mylo): | |
if long_seq>temp: temp,max_seq_index=long_seq,i-1 | |
long_seq,j=1,i | |
while j!=len(mylo)-1 and mylo[j]<mylo[j+1] : | |
long_seq+=1 | |
j+=1 | |
i+=1 | |
total=abs(sum(mylo[max_seq_index:max_seq_index+temp])) | |
print "The longest sequence no in File is: %d and sum is %d"% (temp,total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment