I have a text file with a single line in it. The line of text is a whole bunch of random numbers. I need to determine the most amount of times a 5 is repeated and print how many times it’s repeated. For example: numList: 1234555325146555. The most amount of times 5 is repeated in a row is 3 and that happens 2 times. Here is the code I have so far, it shows me at what positions 5 occurs. I think this is the first step but can’t figure out how to move on.
numbers = open("numbers.txt",'rU')
count = -1
numString = numbers.readline()
for num in numString:
count += 1
if num == '5':
print count
counter += 1
I would continually check to see if a particular string of 5’s was in the given string until it wasn’t anymore (adding a ‘5’ each time). Then I’d back up 1 and use the
countmethod of strings — Something like this (pseudo-code follows — Note this is not syntactically valid python. That’s up to you since this is homework.)