Initial Commit
This commit is contained in:
43
go/binary/README.md
Normal file
43
go/binary/README.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# Binary
|
||||
|
||||
Write a program that will 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 *n*th 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`.
|
||||
|
||||
To run the tests simply run the command `go test` in the exercise directory.
|
||||
|
||||
If the test suite contains benchmarks, you can run these with the `-bench`
|
||||
flag:
|
||||
|
||||
go test -bench .
|
||||
|
||||
For more detailed info about the Go track see the [help
|
||||
page](http://exercism.io/languages/go).
|
||||
|
||||
## Source
|
||||
|
||||
All of Computer Science [view source](http://www.wolframalpha.com/input/?i=binary&a=*C.binary-_*MathWorld-)
|
22
go/binary/binary.go
Normal file
22
go/binary/binary.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package binary
|
||||
|
||||
import "fmt"
|
||||
|
||||
// ParseBinary takes a binary string and returns the
|
||||
// integer representation of it.
|
||||
func ParseBinary(bin string) (int, error) {
|
||||
var ret int
|
||||
currSpot := 1
|
||||
for len(bin) > 0 {
|
||||
v := bin[len(bin)-1]
|
||||
if v != '0' && v != '1' {
|
||||
return 0, fmt.Errorf("Invalid String")
|
||||
}
|
||||
if v == '1' {
|
||||
ret = ret + currSpot
|
||||
}
|
||||
currSpot = currSpot * 2
|
||||
bin = bin[:len(bin)-1]
|
||||
}
|
||||
return ret, nil
|
||||
}
|
55
go/binary/binary_test.go
Normal file
55
go/binary/binary_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package binary
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// You must implement the function,
|
||||
//
|
||||
// func ParseBinary(string) (int, error)
|
||||
//
|
||||
// It is standard for Go functions to return error values to report error conditions.
|
||||
// The test cases below are all valid binary numbers however. For this exercise you
|
||||
// may simply return nil for the error value in all cases.
|
||||
//
|
||||
// For bonus points though, what errors might be possible when parsing a number?
|
||||
// Can you add code to detect error conditions and return appropriate error values?
|
||||
|
||||
var testCases = []struct {
|
||||
binary string
|
||||
expected int
|
||||
}{
|
||||
{"1", 1},
|
||||
{"10", 2},
|
||||
{"11", 3},
|
||||
{"100", 4},
|
||||
{"1001", 9},
|
||||
{"11010", 26},
|
||||
{"10001101000", 1128},
|
||||
{"0", 0},
|
||||
}
|
||||
|
||||
func TestParseBinary(t *testing.T) {
|
||||
for _, tt := range testCases {
|
||||
actual, err := ParseBinary(tt.binary)
|
||||
// We don't expect errors for any of the test cases.
|
||||
if err != nil {
|
||||
t.Fatalf("ParseBinary(%v) returned error %q. Error not expected.",
|
||||
tt.binary, err)
|
||||
}
|
||||
// Well, we don't expect wrong answers either.
|
||||
if actual != tt.expected {
|
||||
t.Fatalf("ParseBinary(%v): actual %d, expected %v",
|
||||
tt.binary, actual, tt.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark combined time for all tests
|
||||
func BenchmarkBinary(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, tt := range testCases {
|
||||
ParseBinary(tt.binary)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user