-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_add_binary.rb
64 lines (46 loc) · 1.32 KB
/
5_add_binary.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
# This question is asked by Apple.
# Given two binary strings (strings containing only 1s and 0s)
# return their sum (also as a binary string).
# NOTE: neither binary string will contain leading 0s unless the string itself is 0
require_relative '../test_helper'
class Binary
def initialize(binary)
@binary = binary
end
def add(other_binary)
max_length = max_length(binary, other_binary)
binary_term = pad_binary(binary, max_length)
other_binary_term = pad_binary(other_binary, max_length)
carry = 0
result = ''
(max_length - 1).downto(0) do |i|
sum = carry + binary_term[i].to_i + other_binary_term[i].to_i
result = "#{sum.odd? ? '1' : '0'}#{result}"
carry = sum < 2 ? 0 : 1
end
carry.zero? ? result : "1#{result}"
end
private
attr_reader :binary
def max_length(binary, other_binary)
[binary.length, other_binary.length].max
end
def pad_binary(binary, max_length)
binary.rjust(max_length, '0')
end
end
class TestBinary < Minitest::Test
def test_1
assert_equal '101', Binary.new('100').add('1')
end
def test_2
assert_equal '100', Binary.new('11').add('1')
end
def test_3
assert_equal '1', Binary.new('1').add('0')
end
def test_4
assert_equal '10001', Binary.new('1101').add('100')
end
end