[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Compact if statement for Codegolf

Drew Olson

10/11/2006 3:54:00 PM

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-....

10 Answers

khaines

10/11/2006 3:58:00 PM

0

James Gray

10/11/2006 3:59:00 PM

0

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

a==0?1:2

James Edward Gray II

dblack

10/11/2006 4:01:00 PM

0

Drew Olson

10/11/2006 4:02:00 PM

0

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-....

Ara.T.Howard

10/11/2006 4:23:00 PM

0

dblack

10/11/2006 4:55:00 PM

0

Joel VanderWerf

10/11/2006 6:16:00 PM

0

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

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

Ara.T.Howard

10/11/2006 7:01:00 PM

0

Relm

10/12/2006 8:33:00 AM

0

Rajat Garg

11/23/2007 6:19:00 PM

0

Relm wrote:
> On Thu, 12 Oct 2006 ara.t.howard@noaa.gov wrote:
>
>> >> 1
>>
>> a>0?2:1
>
> or for positive integers
>
> 2-0**a

Very useful

~r
http://www.piloto...
--
Posted via http://www.ruby-....