Compact if statement for Codegolf

I'm working on my codegolfing skills and was wondering, what is the
shortest possible way to write the following in ruby:

if a==0
   1
else
   2
end

Thanks for the help!

···

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

a==0?1:2

Kirk Haines

···

On Thu, 12 Oct 2006, Drew Olson wrote:

I'm working on my codegolfing skills and was wondering, what is the
shortest possible way to write the following in ruby:

if a==0
  1
else
  2
end

a==0?1:2

James Edward Gray II

···

On Oct 11, 2006, at 10:54 AM, Drew Olson wrote:

I'm working on my codegolfing skills and was wondering, what is the
shortest possible way to write the following in ruby:

if a==0
   1
else
   2
end

Hi --

···

On Thu, 12 Oct 2006, Drew Olson wrote:

I'm working on my codegolfing skills and was wondering, what is the
shortest possible way to write the following in ruby:

if a==0
  1
else
  2
end

The smallest I can think of is:

   a==0?1:2

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

unknown wrote:

···

On Thu, 12 Oct 2006, Drew Olson wrote:

I'm working on my codegolfing skills and was wondering, what is the
shortest possible way to write the following in ruby:

if a==0
  1
else
  2
end

a==0?1:2

Kirk Haines

Exactly what I was looking for! Thanks!

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

Hi --

I'm working on my codegolfing skills and was wondering, what is the
shortest possible way to write the following in ruby:

if a==0
  1
else
  2
end

The smallest I can think of is:

a==0?1:2

if a will only have values 0 or 1 you could use

   2**a

if a is always positive you could use

   >0?2:1

cheers.

-a

···

On Thu, 12 Oct 2006 dblack@wobblini.net wrote:

On Thu, 12 Oct 2006, Drew Olson wrote:

--
my religion is very simple. my religion is kindness. -- the dalai lama

Hi --

···

On Thu, 12 Oct 2006, ara.t.howard@noaa.gov wrote:

On Thu, 12 Oct 2006 dblack@wobblini.net wrote:

Hi --

On Thu, 12 Oct 2006, Drew Olson wrote:

I'm working on my codegolfing skills and was wondering, what is the
shortest possible way to write the following in ruby:

if a==0
  1
else
  2
end

The smallest I can think of is:

a==0?1:2

if a will only have values 0 or 1 you could use

2**a

if a is always positive you could use

>0?2:1

And if a is always 0 you could use:

   1

Sorry, couldn't resist :slight_smile:

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

In that case:

a+1

···

ara.t.howard@noaa.gov wrote:

On Thu, 12 Oct 2006 dblack@wobblini.net wrote:

Hi --

On Thu, 12 Oct 2006, Drew Olson wrote:

I'm working on my codegolfing skills and was wondering, what is the
shortest possible way to write the following in ruby:

if a==0
  1
else
  2
end

The smallest I can think of is:

a==0?1:2

if a will only have values 0 or 1 you could use

  2**a

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

or for positive integers

  2-0**a

···

On Thu, 12 Oct 2006 ara.t.howard@noaa.gov wrote:

On Thu, 12 Oct 2006 dblack@wobblini.net wrote:

> Hi --
>
> On Thu, 12 Oct 2006, Drew Olson wrote:
>
>> I'm working on my codegolfing skills and was wondering, what is the
>> shortest possible way to write the following in ruby:
>>
>> if a==0
>> 1
>> else
>> 2
>> end
>
> The smallest I can think of is:
>
> a==0?1:2
>

if a is always positive you could use

   >0?2:1

--
Relm

touche techer, touche.

-a

···

On Thu, 12 Oct 2006, Joel VanderWerf wrote:

ara.t.howard@noaa.gov wrote:

On Thu, 12 Oct 2006 dblack@wobblini.net wrote:

Hi --

On Thu, 12 Oct 2006, Drew Olson wrote:

I'm working on my codegolfing skills and was wondering, what is the
shortest possible way to write the following in ruby:

if a==0
  1
else
  2
end

The smallest I can think of is:

a==0?1:2

if a will only have values 0 or 1 you could use

  2**a

In that case:

a+1

--
my religion is very simple. my religion is kindness. -- the dalai lama

Relm wrote:

···

On Thu, 12 Oct 2006 ara.t.howard@noaa.gov wrote:

>> 1

   >0?2:1

or for positive integers

  2-0**a

Very useful

~r

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