Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of rootulp

rootulp's solution

to Binary in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Convert a binary number, represented as a string (e.g. '101010'), to its decimal equivalent using first principles.

Implement binary to decimal conversion. Given a binary input string, your program should produce a decimal output. The program should handle invalid inputs.

Note

  • Implement the conversion yourself. Do not use something else to perform the conversion for you.

About Binary (Base-2)

Decimal is a base-10 system.

A number 23 in base 10 notation can be understood as a linear combination of powers of 10:

  • The rightmost digit gets multiplied by 10^0 = 1
  • The next number gets multiplied by 10^1 = 10
  • ...
  • The nth number gets multiplied by 10^(n-1).
  • All these values are summed.

So: 23 => 2*10^1 + 3*10^0 => 2*10 + 3*1 = 23 base 10

Binary is similar, but uses powers of 2 rather than powers of 10.

So: 101 => 1*2^2 + 0*2^1 + 1*2^0 => 1*4 + 0*2 + 1*1 => 4 + 1 => 5 base 10.

Running the tests

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

Source

All of Computer Science http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

BinaryTest.java

import org.junit.Test;
import org.junit.Ignore;

import static org.junit.Assert.assertEquals;

public class BinaryTest {

    private Binary binary;

    @Test
    public void testBinary0IsDecimal0() {
	binary = new Binary("0");
	assertEquals(0, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testBinary1IsDecimal1() {
	binary = new Binary("1");
	assertEquals(1, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testBinary10IsDecimal2() {
	binary = new Binary("10");
	assertEquals(2, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testBinary11IsDecimal3() {
	binary = new Binary("11");
	assertEquals(3, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testBinary100IsDecimal4() {
	binary = new Binary("100");
	assertEquals(4, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testBinary1001IsDecimal9() {
	binary = new Binary("1001");
	assertEquals(9, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testBinary11010IsDecimal26() {
	binary = new Binary("11010");
	assertEquals(26, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testBinary10001101000IsDecimal1128() {
	binary = new Binary("10001101000");
	assertEquals(1128, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testBinaryIgnoresLeadingZeros() {
	binary = new Binary("000011111");
	assertEquals(31, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void test2NotValidBinaryDigit() {
	binary = new Binary("2");
	assertEquals(0, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testNumberContainingNonBinaryDigitInvalid() {
	binary = new Binary("01201");
	assertEquals(0, binary.getDecimal());
   }

    @Ignore("Remove to run test")
    @Test
    public void testNumberWithTrailingNonBinaryCharactersInvalid() {
	binary = new Binary("10nope");
	assertEquals(0, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testNumberWithLeadingNonBinaryCharactersInvalid() {
	binary = new Binary("nope10");
	assertEquals(0, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testNumberWithInternalNonBinaryCharactersInvalid() {
	binary = new Binary("10nope10");
	assertEquals(0, binary.getDecimal());
    }

    @Ignore("Remove to run test")
    @Test
    public void testNumberAndWordWhitespaceSeparatedInvalid() {
	binary = new Binary("001 nope");
	assertEquals(0, binary.getDecimal());
    }

}
import java.lang.Math;

public class Binary {

  private final String input;

  public Binary(String input) {
    this.input = input;
  }

  public int getDecimal() {

    // Remove non 1's and 0's
    String cleanInput = input.replaceAll("[^01]","");

    // Return 0 if invalid string
    if (!input.equals(cleanInput)) { return 0; }

    // Reverse cleanInput
    String reverse = new StringBuffer(cleanInput).reverse().toString();

    // Increment total based on which indexes have 1's
    int total = 0;
    for (int i = 0, n = reverse.length(); i < n; i++) {
      if (reverse.charAt(i) == '1') { total += Math.pow(2, i); }
    }

    // Return total
    return total;
  }

}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?