Just for fun: oneline code for 4 language permutation

hello,happy weekday!
in ruby1.9:
irb:>
%w{ruby c java php}.permutation.to_a

and my code is(all ruby):
irb:>
(1…1000).inject([]){|p,_|p<<%w{ruby c java php}.sort_by{rand}}.uniq

how about you?

On Fri, Nov 13, 2009 at 3:31 PM, Haoqi H. [email protected] wrote:

hello,happy weekday!
in ruby1.9:
irb:>
%w{ruby c java php}.permutation.to_a

and my code is(all ruby):
irb:>
(1…1000).inject([]){|p,_|p<<%w{ruby c java php}.sort_by{rand}}.uniq

.shuffle! is shorter than .sort_by{rand} by 5 characters.

I hate that your method is nondeterministic, but I’ve got it spinning
in a while loop and having broken it yet…

how about you?

Posted via http://www.ruby-forum.com/.


Paul S.
http://www.nomadicfun.co.uk

[email protected]

On Fri, Nov 13, 2009 at 3:43 PM, Paul S. [email protected]
wrote:

.shuffle! is shorter than .sort_by{rand} by 5 characters.

I hate that your method is nondeterministic, but I’ve got it spinning
in a while loop and having broken it yet…

And it just did.

FWIW, your method only gives the correct result with probablity 1 -
((23/24)^999) so it should fail once every 2.9 trillion runs
(3x10^18).


Paul S.
http://www.nomadicfun.co.uk

[email protected]


Paul S.
http://www.nomadicfun.co.uk

[email protected]

On Fri, Nov 13, 2009 at 3:48 PM, Paul S. [email protected]
wrote:

.shuffle! is shorter than .sort_by{rand} by 5 characters.

I hate that your method is nondeterministic, but I’ve got it spinning
in a while loop and having broken it yet…

And it just did.

FWIW, your method only gives the correct result with probablity 1 -
((23/24)^999) so it should fail once every 2.9 trillion runs
(3x10^18).

Actually 1 - ((23/24)^977) is a better lower bound. I’ll shut up now.


Paul S.
http://www.nomadicfun.co.uk

[email protected]


Paul S.
http://www.nomadicfun.co.uk

[email protected]