Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by Anonymous Monk
on Sep 11, 2007 at 23:40 UTC ( #638460=note: print w/replies, xml ) Need Help??


in reply to Re: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
in thread Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

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

  • Comment on Re^2: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
  • Download Code

Replies are listed 'Best First'.
Re^3: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
by erroneousBollock (Curate) on Sep 12, 2007 at 10:08 UTC
    I find them equivalently unreadable ;-)
Re^3: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
by MonkOfAnotherSect (Sexton) on Sep 13, 2007 at 04:32 UTC
    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)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://638460]
help
Chatterbox?
[stevieb]: james28909: what's the problem/question?
[james28909]: it is not an absolute date like "27" or "31". sometimes it is like wednesday or friday
[james28909]: and i need to format those special instances into an absolute date instead of "yesterday"
[stevieb]: ask a question on SoPW, and include at least a half-dozen examples of the input, and at least one example of expected output
[davido]: Exactly: SoPW. This isn't going to be solved easily in the CB.
[james28909]: in need "yesterday" and so on, to be absolute like "1" or "31"
[stevieb]: ...and throw some of your existing code into the equation as well, just so readers know you've given a try at it ;)
[james28909]: ok
[stevieb]: davido thanks for the link ;) I was being the typical lazy
[davido]: date parsing is hard. The more examples you can provide of the input (within reason) and expected output, the better.

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2017-04-29 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (531 votes). Check out past polls.