Consecutive Character Sequences

W

Walter Brunswick

Is there any way to [efficiently] iterate through a sequence of characters to find N [or more] consecutive equivalent characters?

So, for example, the string "taaypiqee88adbbba" would return 1 if the number (of consequtive characters) supplied in the parameters
of the function call was 2 or 3, because "a", "e", 8, and "b" is repeated 2 or 3 times.

Thanks for any assistance.
W. Brunswick.
 
P

Peter Hansen

Walter said:
Is there any way to [efficiently] iterate through a sequence of characters to find N [or more] consecutive equivalent characters?

So, for example, the string "taaypiqee88adbbba" would return 1 if the number (of consequtive characters) supplied in the parameters
of the function call was 2 or 3, because "a", "e", 8, and "b" is repeated 2 or 3 times.

Why would it return 1? Is that instead of True, or does it represent a
count of items, or the position of something in the list, or what?

-Peter
 
M

mensanator

Walter said:
Is there any way to [efficiently] iterate through a sequence of characters to find N [or more] consecutive equivalent characters?

So, for example, the string "taaypiqee88adbbba" would return 1 if the number (of consequtive characters) supplied in the parameters
of the function call was 2 or 3, because "a", "e", 8, and "b" is repeated 2 or 3 times.

Thanks for any assistance.
W. Brunswick.

def rep(n):
# current repetition count
the_rep = 1
# previous character inspected
last_c = ''
# history of repetions
max_rep = {}
for c in s:
# duplicate character found?
if c==last_c:
# count how many consecutive dups
the_rep += 1
# repetition (if any) ended, save previous rep count
else:
# has this count occured before?
if max_rep.has_key(the_rep):
# if so, track how many times it has
max_rep[the_rep] += 1
# otherwise, add this rep count to history
else:
max_rep[the_rep] = 1
# reset rep count to look for next block
the_rep = 1
# save current character to compare to next character
last_c = c
# check that last character in string wasn't part of a block
if max_rep.has_key(the_rep):
max_rep[the_rep] += 1
else:
max_rep[the_rep] = 1
# finally, did the block size we asked for ever occur?
if max_rep.has_key(n):
return 1
else:
return 0

s = 'taaypiqee88adbbba'

for i in range(9):
print rep(i),


"""

0 1 1 1 0 0 0 0 0

"""
 
G

George Sakkis

Walter Brunswick said:
Is there any way to [efficiently] iterate through a sequence of characters to find N [or more]
consecutive equivalent characters?
So, for example, the string "taaypiqee88adbbba" would return 1 if the number (of consequtive
characters) supplied in the parameters
of the function call was 2 or 3, because "a", "e", 8, and "b" is repeated 2 or 3 times.

Thanks for any assistance.
W. Brunswick.

If you're in 2.4, use itertools.groupby:

import itertools as it

def hasConsequent(aString, minConsequent):
for _,group in it.groupby(aString):
if len(list(group)) >= minConsequent:
return True
return False


George
 
A

Aries Sun

I have tested George's solutions, it seems not complete. When pass (s,
3) to the function hasConsequent(), it returns the wrong result.

The following is my approach. The performence may be not so good. There
must be better ones.
for ch in aString:
result = findall(ch*minConsequent, aString)
if len(result) >= 1:
return True
return False
False

Aries Sun
 
G

George Sakkis

Aries Sun said:
I have tested George's solutions, it seems not complete. When pass (s,
3) to the function hasConsequent(), it returns the wrong result.

What are you talking about ? I get the correct answer forTrue


George
 
A

Aries Sun

Hi George,
I used Python 2.4.1, the following are the command lines.
But the reslut was still False. Is there anything wrong with below
codes?
for _,group in it.groupby(aString):
if len(list(group)) >= minConsequent:
return True
return False


Regards,

Aries
 
G

George Sakkis

Aries Sun said:
Hi George,
I used Python 2.4.1, the following are the command lines.
But the reslut was still False. Is there anything wrong with below
codes?hasConsequent("taaypiqee88adbbba", 3)

All indentation was lost in your message, so I'm not quite sure; here it is again, just in case:

import itertools as it

def hasConsequent(aString, minConsequent):
for _,group in it.groupby(aString):
if len(list(group)) >= minConsequent:
return True
return False

Run it from a file instead of the command line and see if you get the same result.

I'm using 2.4:'2.4 (#1, Mar 29 2005, 15:15:45) \n[GCC 3.3.3 (cygwin special)]'

I would be very surprised if something so crucial changed between 2.4.0 and 2.4.1. What does
[list(group) for _,group in it.groupby("taaypiqee88adbbba")]
return to you ?


George
 
S

Sion Arrowsmith

Aries Sun said:
I used Python 2.4.1, the following are the command lines.
But the reslut was still False. Is there anything wrong with below
codes?
for _,group in it.groupby(aString):
if len(list(group)) >= minConsequent:
return True
return False

Yes: return False is at the wrong indentation level.
 
A

Aries Sun

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,261
Messages
2,571,308
Members
47,976
Latest member
AlanaKeech

Latest Threads

Top