简体   繁体   中英

Find a repeated character in a string and determining how many times in a row it is repeated in python

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

You got the right idea for finding out which position the 5 is in.

So how do you find out how long a row of 5's is? Think about:

  1. You need to know if you've found a 5, if its part of a series. Keep track of the previous number. If that's also a 5, then you're continuing a series.
  2. If you're continuing a series, then have another counter to keep track how long it is.
  3. You need to reset the counter if you reach a number that is not a 5. But before resetting, you need to store that value.
  4. For the next part of the problem (finding out how many series of 5's there are), try using additional "meta" variables that keeps track of the longest series you have so far and how many times you've seen that.

Good luck! and keep on asking questions

I often find with tasks like this I ask myself, how would I do this without a computer if the problem were big enough I couldn't remember everything. So here, I would go till I found a 5. Then I would look at the next number, and if it was a 5, keep going till there were no more 5's in a row. So in your example, I would have found 3 5's in a row. I would make a note that the longest I have found was 3 5's. I would then move on to the next 5.

I would then again count how many 5's in a row there were. In this case I would see that there was only 1. So I would not bother doing anything because I would see that it is less than 3. Then I would move on to the next 5.

I would see that there were 3 in a row, I would go back to my paper to see how long the longest I have found was, and I would see that it was 3. So then I would then make a note that I have seen 2 sets of 3 in a row.

If I ever found 4 or more I would forget all that info I had about sets of 3 and start over with sets of 4 or whatever.

So try implementing this sort of idea in your loop.

Here is a fairly straightforward way to figure this out:

>>> import re
>>> numString = '1234555325146555'
>>> fives = re.findall(r'5+', numString)
>>> len(max(fives))          # most repetitions
3
>>> fives.count(max(fives))  # number of times most repetitions occurs
2

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 count method of strings -- Something like this (pseudo-code follows -- Note this is not syntactically valid python. That's up to you since this is homework.)

str5='5'
while str5 in your_string
    concatenate '5' with str5

#your string is too long by 1 element
max_string=str5 minus the last '5'
yourstring.count(max_string)
from collections import defaultdict, Counter
from itertools import groupby

num_str = '112233445556784756222346587'

res = defaultdict(Counter)
for dig,seq in groupby(num_str):
    res[dig][len(list(seq))] += 1

print res['5'].most_common()

returns

[(1, 2), (3, 1)]

(meaning that '5' was seen twice and '555' was seen once)

#  First step: Find at most how many times 5 comes in a row.
# For this I have a counter which increases by 1 as long 
# as I am dealing with '5'. Once I find a character other 
# than '5' I stop counting, see if my counter value is greater
# than what I have found so far and start counting from zero again.

numbers = open("numbers.txt",'rU')
count = -1
numString = numbers.readline()
maximum = -1;

for num in numString:
    count +=1
    if num== '5':
        counter += 1
    else:
        maximum=max(maximum, counter)
        counter = 0;

#  Second step: Find how many times this repeats.
# Once I know how much times it comes in a row, I find consequent fives
# with the same method and see if the length of them is equal to my maximum

count=-1
amount = 0
for num in numString:
    count +=1
    if num== '5':
        counter += 1
    else:
        if maximum == counter:
            amount += 1
        counter = 0;

Hope, it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM