Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

python encoder and decoder

I would like to build an an encoder and decoder using text coding.

A string "AAABBBBCDDDDDDDDDDEEDDDD" as input, returning a string "A3B4C1D10E2D4", where each alphabet symbol is followed by its frequency in the string. The decoder reverses the process.

Would like help getting started in python.

Answer*

Draft saved
Draft discarded
Cancel
7
  • -1: First of all it assumes that input does not contain digits. Secondly: regular expressions? Seriously? And finally: giving solutions to such questions is an antistackoverflow behaviour. Commented Jan 26, 2013 at 17:25
  • First of all the input could hardly contain digits to get that kind of output, otherwise you would need some sort of separator between the letter and the letter count. Secondly, yes, regular expressions, they get the job done – use your tools. And finally: thanks for clearing that up, I'll try to do better in the future. Commented Jan 26, 2013 at 17:30
  • Yeah, I guess you're right about digits. As for regular expressions: I just find it a bit overkill in this scenario, especially if we exclude digits in input. Commented Jan 26, 2013 at 17:35
  • What's the alternative? Going through the string character by character, essentially building your own little parser? I would agree with you if the count could only be single digit, but clearly it can be of arbitrary length so you would have to parse it somehow. Commented Jan 26, 2013 at 17:44
  • @pyrrrat: there's a one-line groupby solution (two lines going the other way, although I guess I could pack it into one if I had to). Commented Jan 26, 2013 at 17:50

lang-py

AltStyle によって変換されたページ (->オリジナル) /