Inputting To A List And Finding Longest Streak Of The Same Input Python
Solution 1:
Assuming you have a list such as [7, 7, 7, 6, 6, 6, 6, 5, 4, 6, 7]
you can use the groupby()
function to count the number of repetitions and then print the max number at the end.
from itertools import groupby
a = [7, 7, 7, 6, 6, 6, 6, 5, 4, 6, 7]
lst = []
for n,c in groupby(a):
num,count = n,sum(1for i in c)
lst.append((num,count))
maxx = max([y for x,y in lst])
print'Your longest run was {}'.format(maxx)
In this case it returns 4 since the number six was repeated 4 consecutive times
Solution 2:
>>>from itertools import groupby>>>input_iter = iter(lambda: input('enter a number: '), 'end')>>>max(sum(1for x in v) for k,v in groupby(input_iter))
enter a number: 7
enter a number: 7
enter a number: 7
enter a number: 6
enter a number: 6
enter a number: 4
enter a number: end
3
Solution 3:
This is a drawn-out version of how what you describe can be done. Half-way through I realized I was running it with Python 16, so it's backwards compatible!
a = None # stores the last number seen
b = 0# stores the count of the last number
result = [0, 0] # number, count result arrayfor c in "7,7,7,6,6,6,6,5,4".split(','): # split string into array of# just our numbers
c = int(c) # change the character into a bast ten intif c != a: # check current number against last
a = c # if different than last, remember current number as last
b = 1# start over counting at oneelse: # if current number is same as last
b = b + 1# increment counterif b > result[1]: result = a, b # if counter higher than highest# previous count, store the# current number and countprint(("value: %i, count: %i" % result)) # print resulting number, count
Output:
value: 6, count 4
If you have any questions, feel free to comment them.
Solution 4:
This will track the value of streak. If another streak is longer it will replace the last streak with the new value and length.
I used exception handling for input. If you enter a non-number it will ignore it and ask for a number again. If you enter nothing it will end the input loop.
Note that I'm using raw_input instead of input.
whileTrue:
number = raw_input('enter a number: ')
if number == '':
breaktry:
number = int(number)
except ValueError:
print ("'" + number + "' is not a number.")
continue
mylist.append(number)
iflen(mylist) > 0:
#print (mylist)# Chain is the current run we're tracking. Longest is the longest chain we've tracked so far.
chain = longest = 1# Current is the value of the chain we're tracking now. Value is the value of the longest chain we've tracked so far. We use the first value of the list.
current = value = mylist[0]
# Starting with the second number in the list and iterating to the end we compare values.for number in mylist[1:]:
# Did we find another in our current chain?if number == current:
chain += 1else:
chain = 1
current = number
# This will require chains to exceed the previous longest chain to be chosen as the longest. Change this to >= to track the last chain (in the case of a tie).if chain > longest:
longest = chain
value = current
print ('Your longest run was', longest)
Solution 5:
Try this
mylist = []
while True:
mylist.append(int(raw_input("enter number:")))
streak =0
cur, last=0, Nonefor num in mylist:
if num ==last:
curr +=1else:
streak =max(streak, cur)
last= num
cur =0
print("longest run was ",streak)
Post a Comment for "Inputting To A List And Finding Longest Streak Of The Same Input Python"