473,411 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Consecutive Character Sequences

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.
Jul 21 '05 #1
10 5939
Walter Brunswick wrote:
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
Jul 21 '05 #2


Walter Brunswick wrote:
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

"""

Jul 21 '05 #3
"Walter Brunswick" <wa*************@sympatico.ca> wrote:
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
Jul 21 '05 #4
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.
from re import findall
def hasConsequent(aString, minConsequent): for ch in aString:
result = findall(ch*minConsequent, aString)
if len(result) >= 1:
return True
return False
hasConsequent(s, 2) True hasConsequent(s, 3) True hasConsequent(s, 4)

False

Aries Sun

Jul 21 '05 #5
"Aries Sun" <su*******@gmail.com> wrote:
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 for
hasConsequent("taaypiqee88adbbba", 3)

True
George
Jul 21 '05 #6
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?
import itertools as it
def hasConsequent(aString, minConsequent): for _,group in it.groupby(aString):
if len(list(group)) >= minConsequent:
return True
return False

hasConsequent("taaypiqee88adbbba", 3) False


Regards,

Aries

Jul 21 '05 #7
"Aries Sun" <su*******@gmail.com> wrote:
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:
sys.version

'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
Jul 21 '05 #8
Aries Sun <su*******@gmail.com> wrote:
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?
import itertools as it
def hasConsequent(aString, minConsequent):

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


Yes: return False is at the wrong indentation level.

--
\S -- si***@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
Jul 21 '05 #9
Thank you me********@aol.com <me********@aol.com>, George Sakkis <gs*****@rutgers.edu> and Aries Sun <su*******@gmail.com> for your
code contributions. I haven't tested them yet, but they look alright and enough for me to work with.
Thanks to everyone else for your comments.

W. Brunswick.
Jul 21 '05 #10
Hi George,

Here's the result:
[list(group) for _,group in it.groupby("taaypiqee88adbbba")] [['t'], ['a', 'a'], ['y'], ['p'], ['i'], ['q'], ['e', 'e'], ['8', '8'],
['a'], ['d'], ['b', 'b', 'b'], ['a']] [len(list(group)) for _,group in it.groupby("taaypiqee88adbbba")] [1, 2, 1, 1, 1, 1, 2, 2, 1, 1, 3, 1]


According to the above output, your solution is correct.

Regards,
Aries

Jul 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Adam | last post by:
Hi, I am trying to mark consective numbers in a data set and get a count as to how many consecutive numbers exist in the a given line of data. Here is an example line: 3, 5, 7, 9, 10, 13,...
5
by: lkrubner | last post by:
I'm worried about idiot users that write long essays in Microsoft Word, then log into their accounts and bring up an HTML form and copy and paste the essay and hit submit. Or perhaps they do this...
3
by: jr | last post by:
A perplexing one this. I Am trying to design a query or series of queries which will firstly identify a condition. If column A value is less than column B value make column C value =1 , else...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
4
by: Vishal | last post by:
I need a simple method to find whether there are any instances of consecutive commas (more than 1) in a given string without parsing each character of the string. I tried with strtok() with comma...
6
by: Pavils Jurjans | last post by:
Hello, I am experiencing a weird behaviour on my ASP.NET project. The project consists from client-side, which can be whatever environment - web page, EXE application, etc. The client sends HTTP...
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
44
by: Kulgan | last post by:
Hi I am struggling to find definitive information on how IE 5.5, 6 and 7 handle character input (I am happy with the display of text). I have two main questions: 1. Does IE automaticall...
2
by: christopher taylor | last post by:
hello python-list! the other day, i was trying to match unicode character sequences that looked like this: \\uAD0X... my issue, is that the pattern i used was returning:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.