5

Just for kicks I decided to write a snippet that takes an integer and converts it into binary. It seems to be performing the conversion but I was wondering if there's anything that I might be missing from this. It will be great to get some feedback.

Thanks

def convertToBinary(n: Int, bin: List[Int]): String = {
  if(n/2 == 1) {
    (1::(n%2)::bin).mkString(" ")
  } else {
     val r = n%2;val q = n/2;converttobinary(q,r::bin)
  }
}
2
  • 2
    The question might be better suited for codereview.stackexchange.com ; for one thing, it will spin forever with n = 0 Commented Apr 6, 2014 at 22:43
  • Your's only does it for Int. I just added an answer to another thread for doing this which works for Boolean, Byte, Short, Char, Int, and Long. stackoverflow.com/a/54950845/501113 Commented Mar 1, 2019 at 19:29

4 Answers 4

13
def toBinary(n: Int): String = n.toBinaryString
Sign up to request clarification or add additional context in comments.

1 Comment

Your's only does it for Int and doesn't include leading zeros. I just added an answer to another thread for doing this which works for Boolean, Byte, Short, Char, Int, and Long. stackoverflow.com/a/54950845/501113
5

1) Formatting :-)

def converttobinary(n:Int, bin:List[Int]):String = {
  if(n/2 == 1) (1:: (n % 2) :: bin).mkString(" ")
  else {
    val r = n % 2;
    val q = n / 2;
    converttobinary(q, r::bin)
  }
}

2) Signature:

I would omit convert part, made accumulator optional parameter (function user doesn't have to supply arguments used only for internal implementation, right?)

def toBinary(n:Int, bin: List[Int] = List.empty[Int]): String = {
  if(n/2 == 1) (1:: (n % 2) :: bin).mkString(" ")
  else {
    val r = n % 2
    val q = n / 2
    toBinary(q, r::bin)
  }
}

Now it can be used as:

val str = toBinary(42)

Somebody might propose you to pimp such function, so call might look like

val str = 42.toBinary // btw, there is such function in std lib: 42.toBinaryString

But I don't see much profit.

The other thing that bothers me is that you're using List & mkString for this purpose, why not StringBuilder? What are the q and r?

And final minor point -- place @annotation.tailrec to ensure that function will be optimized even in case of any future changes

1 Comment

R is for remainder and Q stands for quotient. I should be using Stringbuilder instead of List and mkstring. My snippet also doesn't handle the case of passing in 0.
5

1) A short one:

scala> def toBinary(n: Int): String = n match {
     |   case 0|1 => s"$n"
     |   case _   => s"${toBinary(n/2)}${n%2}"
     | }
toBinary: (n: Int)String

scala> toBinary(155)
res0: String = 10011011

Comments

1

Tail recursion

def toBinary(n: Int): String = {
  @tailrec def binary(acc: String, n: Int): String = {
    n match {
      case 0 | 1 => n+acc
      case _ => binary((n % 2)+acc, (n / 2))
    }
  }
  binary("",n)
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.