Write a method which transforms an integer into a binary number, no libraries using (on a whiteboard).
Anonymous
def num_bits(n) nbits = 0 while n > 1 n /= 2 nbits += 1 end nbits end def int2bin(n) nbits = x = num_bits(n) total = 0 bin = "" for i in (0..nbits) addon = (2 ** x) if((total + addon) <= n) bin += "1" total += addon else bin += "0" end x -= 1 end bin end
Check out your Company Bowl for anonymous work chats.