[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to translate base 10 number into base 2 number

Li Chen

1/14/2007 4:46:00 PM

Hi all,

I want to write a method to tranlate base 10 number
into base 2 number. I want to call the method within
itself. But it doesn't work. What is the right way
to do something like this?

Thanks,

Li

#####
def ten_to_two(num1)
@array=[]
if num1>2
first,second=num1.divmod(2)
@array<<second

#call the method itself
ten_to_two(first)

else
@array<<second<<first
end
return @array.reverse
end

puts ten_to_two(5)

##output
>ruby assembly1.rb
nil
nil
>Exit code: 0






____________________________________________________________________________________
8:00? 8:25? 8:40? Find a flick in no time
with the Yahoo! Search movie showtime shortcut.
http://tools.search.yahoo.com/short...

5 Answers

Eric I.

1/14/2007 5:04:00 PM

0

Hi Li,

First, here's a solution:

def ten_to_two(num1)
return [] if num1 <= 0
first,second=num1.divmod(2)
ten_to_two(first) << second
end

And note that you can change the << into a call to unshift if you want
the bits in the reverse order.

As for why your solution didn't work, let me offer two things. First,
you never do anything with the result of your recursive call to
ten_to_two. Somehow you need to combine it with the partial result you
calculated immediately beforehand. Second, you're using instance
variables (tagged with the @). That's generally done in methods
defined within a class to hold the state of the instance. For a
"naked" method, you generally stick to local variables (no @).

Eric

================
Interested in hands-on, on-site Ruby training? See www.LearnRuby.com
for information about a well-reviewed class.

chen li wrote:
> Hi all,
>
> I want to write a method to tranlate base 10 number
> into base 2 number. I want to call the method within
> itself. But it doesn't work. What is the right way
> to do something like this?
>
> Thanks,
>
> Li
>
> #####
> def ten_to_two(num1)
> @array=[]
> if num1>2
> first,second=num1.divmod(2)
> @array<<second
>
> #call the method itself
> ten_to_two(first)
>
> else
> @array<<second<<first
> end
> return @array.reverse
> end
>
> puts ten_to_two(5)
>
> ##output
> >ruby assembly1.rb
> nil
> nil
> >Exit code: 0

khaines

1/14/2007 5:07:00 PM

0

Uma Geller

1/14/2007 6:25:00 PM

0

TAWLWTDIT

[0xFF].pack("c").unpack("b8")

there's a whole lotta ways to do it :-)


On 14/01/07, khaines@enigo.com <khaines@enigo.com> wrote:
> On Mon, 15 Jan 2007, chen li wrote:
>
> > Hi all,
> >
> > I want to write a method to tranlate base 10 number
> > into base 2 number. I want to call the method within
> > itself. But it doesn't work. What is the right way
> > to do something like this?
>
> This doesn't answer your question, but in case you were not aware, Ruby
> will do this conversion for you:
>
>
> a = 125
> puts a.to_s(2)
> 1111101
>
>
> Kirk Haines
>
>
>


--
best,
UG
---
Uma Geller
http://umageller.wor...

Robert Klemme

1/14/2007 7:09:00 PM

0

On 14.01.2007 19:25, Uma Geller wrote:
> TAWLWTDIT
>
> [0xFF].pack("c").unpack("b8")
>
> there's a whole lotta ways to do it :-)

Since we start collecting solutions...

irb(main):001:0> "%b" % 16
=> "10000"

:-)

robert

Uma Geller

1/14/2007 8:11:00 PM

0

> Since we start collecting solutions...
>
> irb(main):001:0> "%b" % 16
> => "10000"

readable and concise
beautiful !

UG
---
Uma Geller
http://umageller.wor...