Avatar of glennj

glennj's solution

to Binary in the PL/SQL Track

Published at Mar 23 2019 · 0 comments
Instructions
Test suite
Solution

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.

Setup

Go through the setup instructions for PL/SQL to get ready to code:

http://exercism.io/languages/plsql

Running the Tests

Execute the tests by calling the run method in the respective ut_<exercise># package. The necessary code should be contained at the end of the test package. As an example, the test for the hamming exercise would be run using

begin
  ut_hamming#.run;
end;
/

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.

ut_binary#.plsql

create or replace package ut_binary#
is
  procedure run;
end ut_binary#;
/
 
create or replace package body ut_binary#
is
  procedure test (
    i_descn                                       varchar2
   ,i_exp                                         pls_integer
   ,i_act                                         pls_integer
  )
  is
  begin
    if i_exp = i_act then
      dbms_output.put_line('SUCCESS: ' || i_descn);
    else
      dbms_output.put_line('FAILURE: ' || i_descn || ' - expected ' || nvl('' || i_exp, 'null') || ', but received ' || nvl('' || i_act, 'null'));
    end if;
  end test;
 
  procedure run
  is
  begin
    test(i_descn => 'test_binary_1_is_decimal_1'              , i_exp => 1   , i_act => binary#.to_decimal('1'          ));
    test(i_descn => 'test_binary_10_is_decimal_2'             , i_exp => 2   , i_act => binary#.to_decimal('10'         ));
    test(i_descn => 'test_binary_11_is_decimal_3'             , i_exp => 3   , i_act => binary#.to_decimal('11'         ));
    test(i_descn => 'test_binary_100_is_decimal_4'            , i_exp => 4   , i_act => binary#.to_decimal('100'        ));
    test(i_descn => 'test_binary_1001_is_decimal_9'           , i_exp => 9   , i_act => binary#.to_decimal('1001'       ));
    test(i_descn => 'test_binary_11010_is_decimal_26'         , i_exp => 26  , i_act => binary#.to_decimal('11010'      ));
    test(i_descn => 'test_binary_10001101000_is_decimal_1128' , i_exp => 1128, i_act => binary#.to_decimal('10001101000'));
    test(i_descn => 'test_invalid_binary_postfix_is_decimal_0', i_exp => 0   , i_act => binary#.to_decimal('10110a'     ));
    test(i_descn => 'test_invalid_binary_prefix_is_decimal_0' , i_exp => 0   , i_act => binary#.to_decimal('a10110'     ));
    test(i_descn => 'test_invalid_binary_infix_is_decimal_0'  , i_exp => 0   , i_act => binary#.to_decimal('101a10'     ));
    test(i_descn => 'test_invalid_binary_is_decimal_0'        , i_exp => 0   , i_act => binary#.to_decimal('101210'     ));
  end run;
end ut_binary#;
/
 
begin
  ut_binary#.run;
end;
/
create or replace package binary#
is
    function to_decimal         (binary_string varchar2) return pls_integer;
    function to_dec_loop        (binary_string varchar2) return pls_integer;
    function to_dec_bin_to_num  (binary_string varchar2) return pls_integer;
end binary#;
/

create or replace package body binary#
is
    function to_dec_loop (binary_string varchar2)
    return pls_integer
    is
        digit   varchar2(1);
        answer  pls_integer := 0;
        i       pls_integer;
    begin
        for i in 1 .. length(nvl(binary_string, '')) loop
            digit := substr(binary_string, i, 1);
            answer := answer * 2;
            if digit = '1' then
                answer := answer + 1;
            elsif digit <> '0' then
                -- invalid binary digit
                return 0;
            end if;
        end loop;

        return answer;

    exception when others then raise;
    end to_dec_loop;

    -- https://stackoverflow.com/a/49718779/7552
    function to_dec_bin_to_num (binary_string varchar2)
    return pls_integer
    is
        vector      varchar2(255);
        answer      pls_integer;
    begin

        vector := replace(replace(nvl(binary_string, ''), '1', '1,'), '0', '0,');
        vector := trim(trailing ',' from vector);
        execute immediate 'select bin_to_num(' || vector || ') from dual' into answer;
        return answer;

    exception when others then
        if SQLCODE in (-904, -1428) then
            -- ORA-00904: "invalid identifier" -- non-digit in vector
            -- ORA-01428: "out of range" error -- vector contains number not 1 or 0
            return 0;
        else
            raise;
        end if;
    end to_dec_bin_to_num;

    --
    function to_decimal (binary_string varchar2)
    return pls_integer
    is
    begin
        --return to_dec_loop(binary_string);
        return to_dec_bin_to_num(binary_string);
    exception when others then raise;
    end to_decimal;
end binary#;
/

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?