The stupid question is the question not asked PerlMonks

### Re: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

 on Sep 11, 2007 at 23:25 UTC ( #638457=note: print w/replies, xml ) Need Help??

That would be:
```#!/usr/bin/env python

str = "ABBBCCDDZ"

parts = []
for i in range(len(str)):
if len(parts)>0:
if parts[len(parts)-1][len(parts[len(parts)-1])-1]==str[i]:
parts[len(parts)-1] += str[i]
else:
parts.append(str[i])
else:
parts.append(str[i])

print parts
- Antonio Ognio, Lima-Peru
• Comment on Re: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

Replies are listed 'Best First'.
Re^2: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
by Anonymous Monk on Sep 11, 2007 at 23:40 UTC

Another solution, much more verbose but certainly more readable:

```#!/usr/bin/env python

str = "ABBBCCDDZ"

parts = []
last_char = ''
current_chunk = ''
for i in range(len(str)):
current_char = str[i]
if (current_chunk == '') or (last_char == current_char):
current_chunk = current_chunk + current_char
else:
parts.append(current_chunk)
current_chunk = current_char
last_char = current_char
if len(current_chunk)>0:
parts.append(current_chunk)
print parts

Also available here:

http://pastebin.com/f1213db97

- Antonio Ognio, Lima-Peru

I find them equivalently unreadable ;-)
Gnah. No No and No.

The itertools version is quite lovely, but if you must do an iterative version please at least make it idiomatic. If you really needed an index, you could at least use enumerate() ;-P

```mystr = "ABBBCCDDZ"

def get_iterparts(mystr):
buf = ""
for char in mystr:
if not buf or buf.endswith(char):
buf += char
else:
yield buf
buf = char
if buf:
yield buf
print list(get_iterparts(mystr))

def get_parts(mystr):
parts = []
buf = ""
for char in mystr:
if not buf or buf.endswith(char):
buf += char
else:
parts.append(buf)
buf = char
if buf:
parts.append(buf)
return parts
print get_parts(mystr)

Create A New User
Node Status?
node history
Node Type: note [id://638457]
help
Chatterbox?
 [marto]: Discipulus Let's discuss Podmaster's Signature and the response from tye

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-06-23 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (539 votes). Check out past polls.