Skip to content

Commit 1ae360a

Browse files
committed
problem14
1 parent 84ffdbb commit 1ae360a

File tree

6 files changed

+88
-9
lines changed

6 files changed

+88
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ Number | Difficulty | Asked By
3030
[#11](problem011) | MEDIUM | Twitter
3131
[#12](problem012) | HARD | Amazon
3232
[#13](problem013) | HARD | Amazon
33+
[#14](problem014) | MEDIUM | Google

problem011/problem011.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ func (node *trie) subStrings() []string {
4848
if len(*node) == 0 {
4949
return []string{""}
5050
}
51-
childrenChannel := make(chan []string, len(*node))
51+
substringChannel := make(chan []string, len(*node))
5252
for keyString, subNode := range *node {
5353
go func(prefix string, node *trie) {
5454
var subStrings []string
5555
for _, subSubString := range node.subStrings() {
5656
subStrings = append(subStrings, fmt.Sprintf("%s%s", prefix, subSubString))
5757
}
58-
childrenChannel <- subStrings
58+
substringChannel <- subStrings
5959
}(keyString, subNode)
6060
}
6161
finished := 0
6262
var result []string
6363
for finished < len(*node) {
6464
select {
65-
case subStrings := <-childrenChannel:
65+
case subStrings := <-substringChannel:
6666
result = append(result, subStrings...)
6767
finished++
6868
}

problem013/problem013.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ func Run(input string, distinctCharactersLimit uint) uint {
55
characterCount := make(map[rune]int)
66
substringStartsAt, substringEndsAt := 0, 0
77
for substringEndsAt < len(input) {
8-
substringEndsWith := []rune(input)[substringEndsAt]
9-
characterCount[substringEndsWith] += 1
8+
startingCharacter := []rune(input)[substringEndsAt]
9+
characterCount[startingCharacter] += 1
1010
for uint(len(characterCount)) > distinctCharactersLimit {
11-
substringStartsWith := []rune(input)[substringStartsAt]
12-
if characterCount[substringStartsWith] > 1 {
13-
characterCount[substringStartsWith] -= 1
11+
endingCharacter := []rune(input)[substringStartsAt]
12+
if characterCount[endingCharacter] > 1 {
13+
characterCount[endingCharacter]--
1414
} else {
15-
delete(characterCount, substringStartsWith)
15+
delete(characterCount, endingCharacter)
1616
}
1717
substringStartsAt++
1818
}

problem014/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Daily Coding Problem: Problem #14 [Medium]
2+
3+
Good morning! Here's your coding interview problem for today.
4+
5+
This problem was asked by Google.
6+
7+
The area of a circle is defined as πr^2. Estimate π to 3 decimal places using a Monte Carlo method.
8+
9+
Hint: The basic equation of a circle is `x^ + y^2 = r^2`.

problem014/problem014.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package problem014
2+
3+
import (
4+
"math"
5+
"math/rand"
6+
"time"
7+
)
8+
9+
type tester func(float64, float64) bool
10+
type generator func() float64
11+
12+
func Run(totalRepeat int) float64 {
13+
const concurrency = 8
14+
rand.Seed(time.Now().UnixNano())
15+
circleTester := func(x float64, y float64) bool { return math.Pow(x, 2)+math.Pow(y, 2) < 1 }
16+
xGenerator := func() float64 { return rand.Float64() }
17+
subResults := make(chan struct{ successCount, trialCount int }, concurrency)
18+
for i := 0; i < concurrency; i++ {
19+
margin := i
20+
yGenerator := func() float64 { return (float64(margin) + rand.Float64()) / float64(concurrency) }
21+
go func(testerFunction tester, xGenerator generator, yGenerator generator, repeat int) {
22+
successCount, trialCount := simulate(circleTester, xGenerator, yGenerator, repeat)
23+
subResults <- struct{ successCount, trialCount int }{successCount: successCount, trialCount: trialCount}
24+
}(circleTester, xGenerator, yGenerator, totalRepeat/concurrency)
25+
}
26+
finished := 0
27+
totalSuccessCount, totalTrialCount := 0, 0
28+
for finished < concurrency {
29+
select {
30+
case result, _ := <-subResults:
31+
totalSuccessCount += result.successCount
32+
totalTrialCount += result.trialCount
33+
finished++
34+
}
35+
}
36+
approxPi := float64(totalSuccessCount*4) / float64(totalTrialCount)
37+
return math.Round(1000*approxPi) / 1000
38+
}
39+
40+
func simulate(testerFunction tester, xGenerator generator, yGenerator generator, repeat int) (int, int) {
41+
successCount, trialCount := 0, 0
42+
for i := 0; i < repeat; i++ {
43+
if testerFunction(xGenerator(), yGenerator()) {
44+
successCount++
45+
}
46+
trialCount++
47+
}
48+
return successCount, trialCount
49+
}

problem014/problem014_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package problem014
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
func TestRun(t *testing.T) {
9+
result := Run(10000)
10+
if result < math.Pi * 0.95 || result > math.Pi * 1.05 {
11+
t.FailNow()
12+
}
13+
}
14+
15+
func TestRun2(t *testing.T) {
16+
result := Run(10000000)
17+
if result < math.Pi * 0.999 || result > math.Pi * 1.001 {
18+
t.FailNow()
19+
}
20+
}

0 commit comments

Comments
 (0)