Created
October 12, 2014 23:17
-
-
Save keathley/6c9003dbd152ed4821b8 to your computer and use it in GitHub Desktop.
"Hello World" in Go
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
package main | |
import ( | |
"fmt" | |
"math" | |
"math/rand" | |
"time" | |
"sort" | |
) | |
type Game struct { | |
turns int | |
square int | |
} | |
type ByTurns []Game | |
func (g ByTurns) Len() int { return len(g) } | |
func (g ByTurns) Swap(i, j int) { g[i], g[j] = g[j], g[i] } | |
func (g ByTurns) Less(i, j int) bool { return g[i].turns < g[j].turns } | |
func (g *Game) Play() { | |
for g.square < 100 { | |
g.turns += 1 | |
g.square += rand.Intn(5) + 1 | |
g.square = checkLadders(g.square) | |
g.square = checkChutes(g.square) | |
} | |
} | |
func checkLadders(square int) int { | |
switch square { | |
case 1: | |
return 23 | |
case 4: | |
return 14 | |
case 9: | |
return 31 | |
case 21: | |
return 42 | |
case 28: | |
return 84 | |
case 36: | |
return 44 | |
case 51: | |
return 67 | |
case 71: | |
return 91 | |
case 80: | |
return 100 | |
} | |
return square | |
} | |
func checkChutes(square int) int { | |
switch square { | |
case 98: | |
return 78 | |
case 95: | |
return 75 | |
case 93: | |
return 73 | |
case 87: | |
return 24 | |
case 62: | |
return 19 | |
case 64: | |
return 60 | |
case 56: | |
return 53 | |
case 49: | |
return 11 | |
case 48: | |
return 26 | |
case 16: | |
return 6 | |
} | |
return square | |
} | |
func playAll(games []Game) { | |
for i := range(games) { | |
games[i].Play() | |
} | |
} | |
func printGameStats(games []Game) { | |
sort.Sort(ByTurns(games)) | |
fmt.Printf("Total turns: %d\n", totalTurns(games)) | |
fmt.Printf("Mean turns: %f\n", meanTurns(games)) | |
fmt.Printf("Median: %f\n", medianTurns(games)) | |
fmt.Printf("Standard Deviation: %f\n", stdDeviation(games)) | |
fmt.Printf("Max Turns: %d\n", maxTurns(games)) | |
fmt.Printf("Min Turns: %d\n", minTurns(games)) | |
} | |
func totalTurns(games []Game) int { | |
total := 0 | |
for _, game := range(games) { | |
total += game.turns | |
} | |
return total | |
} | |
func meanTurns(games []Game) float64 { | |
return float64(totalTurns(games)) / float64(len(games)) | |
} | |
func medianTurns(games []Game) float64 { | |
middle := len(games) / 2 | |
result := float64(games[middle].turns) | |
if len(games)%2 == 0 { | |
result = (result + float64(games[middle - 1].turns)) / 2.0 | |
} | |
return result | |
} | |
func stdDeviation(games []Game) float64 { | |
mean := meanTurns(games) | |
sum := 0.0 | |
for _, g := range(games) { | |
sum += math.Pow((float64(g.turns) - mean), 2) | |
} | |
return math.Sqrt(sum / float64(len(games))) | |
} | |
func maxTurns(games []Game) int { | |
return games[len(games) - 1].turns | |
} | |
func minTurns(games []Game) int { | |
return games[0].turns | |
} | |
func main() { | |
rand.Seed( time.Now().UTC().UnixNano()) | |
fmt.Println("Making games") | |
games := make([]Game, 1000000) | |
playAll(games) | |
printGameStats(games) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment