-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import "strings" | ||
|
||
func addBinary(a string, b string) string { | ||
smaller, bigger, offset := getSmaller(a, b) | ||
var resultString strings.Builder | ||
carry := 0 | ||
nextCarry := 0 | ||
|
||
for i := len(smaller) - 1; i >= 0; i-- { | ||
carry = nextCarry | ||
nextCarry = 0 | ||
if smaller[i] == '1' && bigger[i+offset] == '1' { | ||
if carry == 1 { | ||
resultString.WriteString("1") | ||
} else { | ||
resultString.WriteString("0") | ||
} | ||
nextCarry = 1 | ||
} else if smaller[i] == '0' && bigger[i+offset] == '0' { | ||
if carry == 1 { | ||
resultString.WriteString("1") | ||
} else { | ||
resultString.WriteString("0") | ||
} | ||
} else { | ||
if carry == 1 { | ||
resultString.WriteString("0") | ||
nextCarry = 1 | ||
} else { | ||
resultString.WriteString("1") | ||
} | ||
} | ||
} | ||
|
||
for i := offset - 1; i >= 0; i-- { | ||
carry = nextCarry | ||
nextCarry = 0 | ||
if carry == 1 { | ||
if bigger[i] == '1' { | ||
resultString.WriteString("0") | ||
nextCarry = 1 | ||
} else { | ||
if carry == 1 { | ||
resultString.WriteString("1") | ||
} else { | ||
resultString.WriteString("0") | ||
} | ||
} | ||
} else { | ||
resultString.WriteByte(bigger[i]) | ||
} | ||
|
||
} | ||
|
||
if nextCarry == 1 { | ||
resultString.WriteString("1") | ||
} | ||
var reverse strings.Builder | ||
result := resultString.String() | ||
for i := resultString.Len() - 1; i >= 0; i-- { | ||
reverse.WriteByte(result[i]) | ||
} | ||
return reverse.String() | ||
} | ||
|
||
func getSmaller(a, b string) (string, string, int) { | ||
if len(a) < len(b) { | ||
offset := len(b) - len(a) | ||
return a, b, offset | ||
} else { | ||
offset := len(a) - len(b) | ||
return b, a, offset | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestAddBinary(t *testing.T) { | ||
t.Run("11, 1", func(t *testing.T) { | ||
s1, s2 := "11", "1" | ||
got := addBinary(s1, s2) | ||
want := "100" | ||
|
||
if got != want { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
}) | ||
t.Run("1, 1", func(t *testing.T) { | ||
s1, s2 := "1", "1" | ||
got := addBinary(s1, s2) | ||
want := "10" | ||
|
||
if got != want { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
}) | ||
t.Run("1010, 1011", func(t *testing.T) { | ||
s1, s2 := "1010", "1011" | ||
got := addBinary(s1, s2) | ||
want := "10101" | ||
|
||
if got != want { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
}) | ||
|
||
t.Run("HUGE", func(t *testing.T) { | ||
|
||
//24847893154024981730169397005 | ||
s1 := "10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101" | ||
//526700554598729746900966573811 | ||
s2 := "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011" | ||
got := addBinary(s1, s2) | ||
//551548447752754728631135970816 | ||
want := "110111101100010011000101110110100000011101000101011001000011011000001100011110011010010011000000000" | ||
|
||
if got != want { | ||
t.Errorf("got %v, want %v", got, want) | ||
} | ||
}) | ||
} |