-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #190 from vaskoz/day90
Day90
- Loading branch information
Showing
3 changed files
with
91 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
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,41 @@ | ||
package day90 | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
var random *rand.Rand | ||
|
||
// getRandom returns a *math.Rand. | ||
func getRandom() *rand.Rand { | ||
if random == nil { | ||
random = rand.New(rand.NewSource(time.Now().UnixNano())) | ||
} | ||
return random | ||
} | ||
|
||
// RandMissingNumbers randomly generates a number from 0 to n-1 that isn't in l. | ||
// Runs in O(N) time and O(l) space. | ||
func RandMissingNumbers(n int, l []int) int { | ||
m := make(map[int]struct{}) | ||
for _, v := range l { | ||
m[v] = struct{}{} | ||
} | ||
size := n - len(m) | ||
if size == 0 { | ||
return -1 | ||
} | ||
pos := getRandom().Intn(size) | ||
val := -1 | ||
var i int | ||
for i = 0; i < n; i++ { | ||
if _, found := m[i]; !found { | ||
val++ | ||
} | ||
if val == pos { | ||
break | ||
} | ||
} | ||
return i | ||
} |
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,49 @@ | ||
package day90 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
n int | ||
l []int | ||
distribution float64 | ||
}{ | ||
{5, []int{0, 1, 2, 2}, 0.5}, | ||
{5, []int{0, 1, 0, 1}, 0.333}, | ||
{5, []int{0, 0, 0}, 0.25}, | ||
{5, []int{0, 1, 2, 3}, 1.0}, | ||
} | ||
|
||
func TestRandMissingNumbers(t *testing.T) { | ||
//t.Parallel()//don't run in parallel | ||
for _, tc := range testcases { | ||
results := make(map[int]int) | ||
const iterations = 1000 | ||
for i := 0; i < iterations; i++ { | ||
results[RandMissingNumbers(tc.n, tc.l)]++ | ||
} | ||
for _, v := range results { | ||
delta := (float64(v) / float64(iterations)) - tc.distribution | ||
if delta < 0 { | ||
delta = -delta | ||
} | ||
if delta > 0.1 { | ||
t.Errorf("Exceeded 0.1 tolerance for expected %v distribution", tc.distribution) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestRandMissingNoNumbers(t *testing.T) { | ||
//t.Parallel()//don't run in parallel | ||
if result := RandMissingNumbers(5, []int{0, 1, 2, 3, 4}); result != -1 { | ||
t.Errorf("Expected -1 for a list that includes all possible numbers") | ||
} | ||
} | ||
|
||
func BenchmarkRandMissingNumbers(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
RandMissingNumbers(tc.n, tc.l) | ||
} | ||
} | ||
} |