Compress a string

M

Matt Porter

Hi guys,

I'm trying to compress a string.
E.g:
"AAAABBBC" -> "ABC"

The code I have so far feels like it could be made clearer and more
succinct, but a solution is currently escaping me.


def compress_str(str):
new_str = ""
for i, c in enumerate(str):
try:
if c != str[i+1]:
new_str += c
except IndexError:
new_str += c
return new_str


Cheers
Matt
 
S

Salvatore DI DI0

Try this

t = set("aaaaaaaaaaaabbbbbbbbbbccccccccc")
list(t)

Regards

Salvatore
 
A

Arnaud Delobelle

Matt Porter said:
Hi guys,

I'm trying to compress a string.
E.g:
"AAAABBBC" -> "ABC"

The code I have so far feels like it could be made clearer and more
succinct, but a solution is currently escaping me.


def compress_str(str):
new_str = ""
for i, c in enumerate(str):
try:
if c != str[i+1]:
new_str += c
except IndexError:
new_str += c
return new_str


Cheers
Matt
[/QUOTE]
'spam'

HTH

PS: I keep seeing problems on this list whose solution seems to
involve 'window' iterating over a sequence. E.g.
[('e', 'g'), ('g', 'g'), ('g', 's')]
 
I

inhahe

i see lots of neat one-liner solutions but just for the sake of argument:

def compress_str(str):
new_str = ""
lc = ""
for c in str:
if c != lc: new_str.append(c)
return new_str
 
B

Bjoern Schliessmann

inhahe said:
i see lots of neat one-liner solutions but just for the sake of
argument:

def compress_str(str):
new_str = ""
lc = ""
for c in str:
if c != lc: new_str.append(c)
return new_str

Please test before posting.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in compress_str
AttributeError: 'str' object has no attribute 'append'

Regards,


Björn
 
J

John Salerno

Hi guys,

I'm trying to compress a string.
E.g:
"AAAABBBC" -> "ABC"

Not that you need help anymore, but I decided to give it a try. Not as elegant as the one- and two-liners, but somewhat concise I guess.

def compress(s):
new = [s[:1]]

for c in s[1:]:
if c not in new:
new.append(c)
return ''.join(new)
 
J

John Salerno

Not that you need help anymore, but I decided to give it a try. Not as elegant as the one- and two-liners, but somewhat concise I guess.

Whoops! Could be cleaner! :)

def compress(s):
new = []

for c in s:
if c not in new:
new.append(c)
return ''.join(new)


No, wait! I can do better!

def compress(s):
new = []
[new.append(c) for c in s if c not in new]
return ''.join(new)

Wow, list comprehensions are cool.
 
M

Marc 'BlackJack' Rintsch

def compress(s):
new = []

for c in s:
if c not in new:
new.append(c)
return ''.join(new)


No, wait! I can do better!

def compress(s):
new = []
[new.append(c) for c in s if c not in new] return ''.join(new)

Wow, list comprehensions are cool.

And it's a misuse of list comprehension here IMHO because they are meant to
build lists, not to cram a ``for``/``if`` loop into a one liner. You are
building a list full of `None` objects, just to throw it away.

Ciao,
Marc 'BlackJack' Rintsch
 
B

Bruno Desthuilliers

Salvatore DI DI0 a écrit :
(top-post corrected - Salvatore, please, don't top-post)
> Try this
>
> t = set("aaaaaaaaaaaabbbbbbbbbbccccccccc")
> list(t)

Won't keep the ordering.
 
B

Bruno Desthuilliers

Matt Porter a écrit :
Hi guys,

I'm trying to compress a string.
E.g:
"AAAABBBC" -> "ABC"

The code I have so far feels like it could be made clearer and more
succinct, but a solution is currently escaping me.


def compress_str(str):

using 'str' as an indentifier will shadow the builtin str type.
new_str = ""
for i, c in enumerate(str):
try:
if c != str[i+1]:
new_str += c
except IndexError:
new_str += c
return new_str

Now everyone gave smart solutions, may I suggest the stupidier possible one:

def compress_string(astring):
compressed = []
for c in astring:
if c not in compressed:
compressed.append(c)
return ''.join(compressed)

Not elegant, but at least very clear.
 

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
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top