Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 124
Good morning! Here's your coding interview problem for today.

This problem was asked by Microsoft.

You have 100 fair coins and you flip them all at the same time. Any that come up tails you set aside. The ones that come up heads you flip again. How many rounds do you expect to play before only one coin remains?

Write a function that, given n, returns the number of rounds you'd expect to play until one coin remains.

Pull request description:
Day124
Resolves #255.

Patch:
diff --git a/README.md b/README.md
index 3f1fc11..170fce6 100644
--- a/README.md
+++ b/README.md
@@ -129,3 +129,4 @@ problems from
 * [Day 118](https://github.com/vaskoz/dailycodingproblem-go/issues/246)
 * [Day 120](https://github.com/vaskoz/dailycodingproblem-go/issues/248)
 * [Day 123](https://github.com/vaskoz/dailycodingproblem-go/issues/253)
+* [Day 124](https://github.com/vaskoz/dailycodingproblem-go/issues/255)
diff --git a/day124/problem.go b/day124/problem.go
new file mode 100644
index 0000000..c575240
--- /dev/null
+++ b/day124/problem.go
@@ -0,0 +1,11 @@
+package day124
+
+import "math"
+
+// FairCoinFlipDownToOne answers how many rounds
+// are necessary to reach one coin.
+// You put aside tails, and flip all heads on the next round.
+// How many rounds to reach just 1 coin.
+func FairCoinFlipDownToOne(coins int) int {
+	return int(math.Ceil(math.Log2(float64(coins))))
+}


Test patch:
diff --git a/day124/problem_test.go b/day124/problem_test.go
new file mode 100644
index 0000000..291c994
--- /dev/null
+++ b/day124/problem_test.go
@@ -0,0 +1,30 @@
+package day124
+
+import "testing"
+
+var testcases = []struct {
+	coins, rounds int
+}{
+	{100, 7},
+	{120, 7},
+	{130, 8},
+	{4, 2},
+	{8, 3},
+}
+
+func TestFairCoinFlipDownToOne(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if rounds := FairCoinFlipDownToOne(tc.coins); rounds != tc.rounds {
+			t.Errorf("For %v coins, expected %v rounds got %v", tc.coins, tc.rounds, rounds)
+		}
+	}
+}
+
+func BenchmarkFairCoinFlipDownToOne(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			FairCoinFlipDownToOne(tc.coins)
+		}
+	}
+}