Codegolf - Pascals Triangle

My take on the latest problem from http://www.codegolf.com

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1).join(" ")}

56 bytes.

Best regards,

Michael


Michael U.
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: [email protected]
Visit our Website: www.isis-papyrus.com

My take on the latest problem from http://www.codegolf.com

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1).join(" ")}

:slight_smile: That’s very impressive - I like the way you’ve implemented the filter
like box kernel (summing adjacent array entries) - I wouldn’t have
imagined the k=i would bind tighter than the k+k.

On 8/11/06, Kev J. [email protected] wrote:

like box kernel (summing adjacent array entries) - I wouldn’t have
Kev

Unfortunately, the p method puts things in “” on stdout, which would
cause your code to fail the acceptance test. :frowning:

-Harold

On 11 Aug 2006, at 15:58, [email protected] wrote:

My take on the latest problem from http://www.codegolf.com

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1).join(" ")}

:slight_smile: That’s very impressive - I like the way you’ve implemented the
filter
like box kernel (summing adjacent array entries) - I wouldn’t have
imagined the k=i would bind tighter than the k+k.

how about

a=[]
34.times{k=0;p (a.map!{|i|k+k=i}<<1).join(" ")}

I never use puts, and in this case using p saves you 3 characters -
yay for micro-tuning :slight_smile:

Kev

Kev J. wrote:

like box kernel (summing adjacent array entries) - I wouldn’t have

This does not produce the same output on my system;
your version gives lines like

“1 2 1”

instead of

1 2 1

Since the output format is fixed in codegolf.com, unfortunately
this is not a solution.

Best regards,

Michael


Michael U.
R&D Team
ISIS Information Systems Austria
tel: +43 2236 27551-219, fax: +43 2236 21081
e-mail: [email protected]
Visit our Website: www.isis-papyrus.com

You can make it even shorter by using ’ ’ over .join(’ '), Array#
is an alias for Array#join :slight_smile:

Michael U. [email protected] writes:

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1).join(" ")}

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1)*" "}

Michael U. [email protected] writes:

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1).join(" ")}

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1)*" "}

a=[]
9.times{k=0;puts (a.map!{|i|k+k=i}<<1)*" "}

:slight_smile:

Christian N. wrote:

Michael U. [email protected] writes:

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1).join(" ")}

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1)*" "}

Let’s reuse a not needed array and save 4 bytes :slight_smile: (doesn’t work if you
set RUBY_OPT to always require some module…)

34.times{k=0;puts ($".map!{|i|k+k=i}<<1)*" "}

Michael U. [email protected] writes:

My take on the latest problem from http://www.codegolf.com

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1).join(" ")}

56 bytes.

I think this may be very close to minimal - no approaches I tried got
this small. Note that you can slightly improve readability (but with
no change in character count) with:

a=[]
34.times{k=0;a.map!{|i|k+k=i}<<1;puts a.join(" ")}

Then there’s removing the parentheses around the argument to join, but
that generates a warning that probably means you fail their test.

On Fri, Aug 11, 2006, Carlos wrote:

Let’s reuse a not needed array and save 4 bytes :slight_smile: (doesn’t work if you
set RUBY_OPT to always require some module…)

34.times{k=0;puts ($".map!{|i|k+k=i}<<1)*" "}

This doesn’t work:

34.times{k=0;puts ($".map!{|i|k+k=i}<<1)*’ ‘}
TypeError: String can’t be coerced into Fixnum
from (irb):1:in `+’
from (irb):1
from (irb):1
from (irb):1
from :0

However, changing $" to $* works like a champ :slight_smile:

34.times{k=0;puts ($.map!{|i|k+k=i}<<1)’ '}

Ben

On Friday, August 11, 2006, at 11:16 PM, Daniel M. wrote:

I think this may be very close to minimal - no approaches I tried got
this small. Note that you can slightly improve readability (but with
no change in character count) with:

a=[]
34.times{k=0;a.map!{|i|k+k=i}<<1;puts a.join(" ")}

Then there’s removing the parentheses around the argument to join, but
that generates a warning that probably means you fail their test.

How about:

a=[]
34.times{k=0;puts (a.map!{|i|k+k=i}<<1)*’ '}

However, changing $" to $* works like a champ :slight_smile:

34.times{k=0;puts ($.map!{|i|k+k=i}<<1)’ '}

Ben

Now at 45 chars it ties the current leader.

Ben B. wrote:

On Fri, Aug 11, 2006, Carlos wrote:

Let’s reuse a not needed array and save 4 bytes :slight_smile: (doesn’t work if you
set RUBY_OPT to always require some module…)

34.times{k=0;puts ($".map!{|i|k+k=i}<<1)*" "}

This doesn’t work:

See caveat about environment variables, above.

34.times{k=0;puts ($.map!{|i|k+k=i}<<1)’ '}

Much better choice, yes.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs