Skip to content

Commit

Permalink
Feat: reflected binary code (TheAlgorithms#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
i-redbyte authored Dec 1, 2021
1 parent c85f931 commit e6cf582
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
4. [`MeanUsingAndXor`](./math/binary/arithmeticmean.go#L12): MeanUsingAndXor This function finds arithmetic mean using "AND" and "XOR" operations
5. [`MeanUsingRightShift`](./math/binary/arithmeticmean.go#L17): MeanUsingRightShift This function finds arithmetic mean using right shift
6. [`ReverseBits`](./math/binary/reversebits.go#L14): ReverseBits This function initialized the result by 0 (all bits 0) and process the given number starting from its least significant bit. If the current bit is 1, set the corresponding most significant bit in the result and finally move on to the next bit in the input number. Repeat this till all its bits are processed.
7. [`XorSearchMissingNumber`](./math/binary/xorsearch.go#L11): XorSearchMissingNumber This function finds a missing number in a sequence
7. [`SequenceGrayCode`](./math/binary/rbc.go#L11): SequenceGrayCode The function generates an "Gray code" sequence of length n
8. [`XorSearchMissingNumber`](./math/binary/xorsearch.go#L11): XorSearchMissingNumber This function finds a missing number in a sequence

---
</details><details>
Expand Down Expand Up @@ -644,8 +645,9 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
---
##### Functions:

1. [`MonteCarloPi`](./math/pi/montecarlopi.go#L15): No description provided.
2. [`Spigot`](./math/pi/spigotpi.go#L12): No description provided.
1. [`MonteCarloPi`](./math/pi/montecarlopi.go#L17): No description provided.
2. [`MonteCarloPiConcurrent`](./math/pi/montecarlopi.go#L36): MonteCarloPiConcurrent approximates the value of pi using the Monte Carlo method. Unlike the MonteCarloPi function (first version), this implementation uses goroutines and channels to parallelize the computation. More details on the Monte Carlo method available at https://en.wikipedia.org/wiki/Monte_Carlo_method. More details on goroutines parallelization available at https://go.dev/doc/effective_go#parallel.
3. [`Spigot`](./math/pi/spigotpi.go#L12): No description provided.

---
</details><details>
Expand Down
18 changes: 18 additions & 0 deletions math/binary/rbc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// rbc.go
// description: Reflected binary code (RBC)
// details:
// The reflected binary code (RBC), also known just as reflected binary (RB) or Gray code after Frank Gray, is an ordering of the binary numeral system such that two successive values differ in only one bit (binary digit). - [RBC](https://en.wikipedia.org/wiki/Gray_code)
// author(s) [red_byte](https://github.com/i-redbyte)
// see rbc_test.go

package binary

// SequenceGrayCode The function generates an "Gray code" sequence of length n
func SequenceGrayCode(n uint) []uint {
result := make([]uint, 0)
var i uint
for i = 0; i < 1<<n; i++ {
result = append(result, i^(i>>1))
}
return result
}
37 changes: 37 additions & 0 deletions math/binary/rbc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// rbc_test.go
// description: Test for Reflected binary code
// author(s) [red_byte](https://github.com/i-redbyte)
// see rbc.go

package binary

import (
"reflect"
"testing"
)

func TestSequenceGrayCode(t *testing.T) {
tests := []struct {
name string
n uint
want []uint
}{
{"Sequence of values for 1", 1, []uint{0, 1}},
{"Sequence of values for 2", 2, []uint{0, 1, 3, 2}},
{"Sequence of values for 6", 6, []uint{0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16, 48, 49, 51, 50, 54, 55, 53, 52, 60, 61, 63, 62, 58, 59, 57, 56, 40, 41, 43, 42, 46, 47, 45, 44, 36, 37, 39, 38, 34, 35, 33, 32}},
{"Sequence of values for 8", 8, []uint{0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16, 48, 49, 51, 50, 54, 55, 53, 52, 60, 61, 63, 62, 58, 59, 57, 56, 40, 41, 43, 42, 46, 47, 45, 44, 36, 37, 39, 38, 34, 35, 33, 32, 96, 97, 99, 98, 102, 103, 101, 100, 108, 109, 111, 110, 106, 107, 105, 104, 120, 121, 123, 122, 126, 127, 125, 124, 116, 117, 119, 118, 114, 115, 113, 112, 80, 81, 83, 82, 86, 87, 85, 84, 92, 93, 95, 94, 90, 91, 89, 88, 72, 73, 75, 74, 78, 79, 77, 76, 68, 69, 71, 70, 66, 67, 65, 64, 192, 193, 195, 194, 198, 199, 197, 196, 204, 205, 207, 206, 202, 203, 201, 200, 216, 217, 219, 218, 222, 223, 221, 220, 212, 213, 215, 214, 210, 211, 209, 208, 240, 241, 243, 242, 246, 247, 245, 244, 252, 253, 255, 254, 250, 251, 249, 248, 232, 233, 235, 234, 238, 239, 237, 236, 228, 229, 231, 230, 226, 227, 225, 224, 160, 161, 163, 162, 166, 167, 165, 164, 172, 173, 175, 174, 170, 171, 169, 168, 184, 185, 187, 186, 190, 191, 189, 188, 180, 181, 183, 182, 178, 179, 177, 176, 144, 145, 147, 146, 150, 151, 149, 148, 156, 157, 159, 158, 154, 155, 153, 152, 136, 137, 139, 138, 142, 143, 141, 140, 132, 133, 135, 134, 130, 131, 129, 128}},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := SequenceGrayCode(test.n); !reflect.DeepEqual(got, test.want) {
t.Errorf("SequenceGrayCode() = %v, want %v", got, test.want)
}
})
}
}

func BenchmarkSequenceGrayCode(b *testing.B) {
for i := 0; i < b.N; i++ {
SequenceGrayCode(6)
}
}

0 comments on commit e6cf582

Please sign in to comment.