-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddBinary.rb
More file actions
40 lines (36 loc) · 823 Bytes
/
addBinary.rb
File metadata and controls
40 lines (36 loc) · 823 Bytes
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
# Given 2 binary strings return their sum
# a= "100" b= "11"
# Constraints
# max length ?
# empty strings any one or both return other string
# null return null
def add_binary(a,b)
return nil if( a.nil? && b.nil?)
return a if( b.nil? || b.empty?)
return b if( a.nil? || a.empty?)
a_n = a.size
b_n = b.size
i = 0
carry = 0
result = ''
while(i < a_n || i < b_n)
if i >= a_n
a_i = 0
b_i = b[b_n - i -1].to_i
elsif i >= b_n
b_i = 0
a_i = a[a_n - i - 1].to_i
else
b_i = b[b_n - i -1].to_i
a_i = a[a_n - i - 1].to_i
end
add = a_i + b_i + carry
result.prepend(add == 0 ? '0' : '1')
carry = add < 2 ? 0 : 1
i = i + 1
end
result.prepend('1') if carry == 1
return result
end
puts add_binary("100", "111")
puts add_binary("100", "11")