Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: how to translate base 10 number into base 2 number
Li Chen
1/17/2007 2:46:00 PM
--- "Eric I." <rubytraining@gmail.com> wrote:
> 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
Hi Eric,
Thanks for the explanations. It helps me out.
Li
____________________________________________________________________________________
Get your own web address.
Have a HUGE year through Yahoo! Small Business.
http://smallbusiness.yahoo.com/domains/?...
1 Answer
Giles Bowkett
1/23/2007 7:45:00 PM
0
On 1/23/07, Roland Mai <roland.mai@gmail.com> wrote:
> I wonder why there isn't support for negative radix; even though the
> solutions might not be unique... who cares?
Come on, that's like asking why fractional dimension isn't a primitive
type in Java.
--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://gilesgoatboy.bl...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: how to translate base 10 number into base 2 number
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password