-
Notifications
You must be signed in to change notification settings - Fork 256
/
Copy pathadd_binary.rb
28 lines (27 loc) · 832 Bytes
/
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
=begin
Given two binary strings a and b, return their sum as a binary string.
=end
#Solution
def add_binary(a, b)
result = ""
carry = 0
i= a.length-1
j =b.length-1
while i>=0 or j>=0
char_a = i>=0 ? a[i] : '0'
char_b = j>=0 ? b[j] : '0'
if char_a =='0' and char_b=='0'
char,carry = carry>0 ? [1,0] : [0,0]
result="#{char}#{result}" #instead of adding har in front you can append and reverse result in the end
elsif (char_a=='0' and char_b=='1') or (char_a=='1' and char_b=='0')
char,carry = carry>0 ? [0,1] : [1,0]
result="#{char}#{result}"
else
char,carry = carry>0 ? [1,1] : [0,1]
result="#{char}#{result}"
end
i-=1
j-=1
end
carry>0 ? "1#{result}" : result
end