IWETHEY v. 0.3.0 | TODO
1,095 registered users | 1 active user | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Let me golf the one-liner a little...

ruby -e'p"BCB".split(//).map{|b|"A"..b}.inject{|a,b|a.map{|x|b.map{|y|x+y}}.flatten}'

Mostly this was just removing unneeded spaces. I also switched puts to p, (changes the output, but Scott had already changed the output so I thought that fair), and used split rather than an unpack.

And for those who haven't bothered to understand the algorithm, here is a commented version of the one-liner.
\nputs \\\n  "BCB".split(         # Take the string and split it\n    //                 #   on the spaces between strings\n  ).map{|b|            # foreach character b that you get\n    "A"..b             #   make an array "A"..b\n  }.inject{|a,b|       # foreach of those arrays, let a be the partial\n                       #     answer so far and b be next array (the\n                       #     first array is the first partial answer)\n    a.map{|x|          #  each x in a turns into\n      b.map{|y|        #    an array of turning each y in b into\n        x+y            #      concatenate x and y\n      } \n    }.flatten          #   and then flatten the array of arrays into a\n                       #   single-level array of strings\n  }                    # the last partial answer is our full one\n

BTW it may be me, but I find the Ruby version much more readable than the Python. My guess is that I am reacting to the fact that all of the logic is naturally reading left to right, top to bottom.

Cheers,
Ben
"good ideas and bad code build communities, the other three combinations do not"
- [link|http://archives.real-time.com/pipermail/cocoon-devel/2000-October/003023.html|Stefano Mazzocchi]
New Ooo ... Good call on split
And split returns single character strings rather than integers so ".chr" is not needed. You could also use scan(/./), but it would be the same golf score.

Just a minor nit-pick on the comments
).map{|b|            # foreach character b that you get\n    "A"..b             #   make an array "A"..b
Should be "make a range from "A" to b. A range object doesn't store every element of the range, but only the first and last elements, so ("A".."B") takes the same amount of storage as ("A".."Z").

Which makes the call to inject interesting. The first time inject invokes its block, a range object is passed as parameter a. The remaining times the flattened array from the previous invocation is passed. Since map works on both arrays and ranges, we don't care.
--
-- Jim Weirich jweirich@one.net [link|http://onestepback.org|http://onestepback.org]
---------------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
     Combinatron - (tablizer) - (39)
         Python example (using generators) - (admin) - (3)
             Nice language - reads as easy as pseudocode - (deSitter) - (2)
                 Post the code. -NT - (admin) - (1)
                     The idiom - (deSitter)
         OK, so the generators were nifty, but not needed :-) - (admin) - (34)
             Probably more efficient: - (admin) - (33)
                 And the readable version :P - (FuManChu) - (32)
                     Ick. Globals. - (admin) - (31)
                         Granted. - (FuManChu) - (30)
                             Like this? - (admin) - (29)
                                 This is probably worth decomposing, too - (admin)
                                 Now you've EARNED my nasty Perl tricks... - (ben_tilly) - (25)
                                     Yabut... - (admin) - (20)
                                         Re: Yabut... - (deSitter) - (1)
                                             Appearances *are* complexity - (FuManChu)
                                         They can? - (ben_tilly) - (17)
                                             Ruby? (Perl was impressive, knock me out dude) -NT - (deSitter) - (15)
                                                 Sorry, I am rusty enough not to attempt it - (ben_tilly)
                                                 Two Ruby Versions - (JimWeirich) - (13)
                                                     Recursive + stack space... - (admin) - (10)
                                                         Re: Recursive + stack space... - (JimWeirich) - (7)
                                                             Ah, ok. - (admin) - (6)
                                                                 Proof by induction - (deSitter) - (5)
                                                                     ... - (admin) - (4)
                                                                         (sheepish grimace) - (deSitter)
                                                                         Recursion and Lisp - (JimWeirich) - (2)
                                                                             You still misunderstand me. - (admin) - (1)
                                                                                 Re: You still misunderstand me. - (deSitter)
                                                         Only other people's... :) - (FuManChu) - (1)
                                                             No, I should go look that one up. -NT - (admin)
                                                     Let me golf the one-liner a little... - (ben_tilly) - (1)
                                                         Ooo ... Good call on split - (JimWeirich)
                                             Wrong interpretation. - (admin)
                                     Please deconstruct it for me - (broomberg) - (3)
                                         Deconstructed - (ben_tilly) - (2)
                                             Ahh - (broomberg) - (1)
                                                 Ditto, but I remember the stupid trick for golf -NT - (ben_tilly)
                                 Mein Gott. - (FuManChu) - (1)
                                     Yep. It's a great language. -NT - (admin)

IBM is good at two things:
  1. Shooting itself in the foot, and
  2. Reloading.

408 ms