How to resolve the algorithm Two bullet roulette step by step in the Odin programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Two bullet roulette step by step in the Odin programming language
Table of Contents
Problem Statement
The following is supposedly a question given to mathematics graduates seeking jobs on Wall Street:
Youtube video on the Russian 1895 Nagant revolver [[1]]
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Two bullet roulette step by step in the Odin programming language
Source code in the odin programming language
/* imports */
import "core:fmt"
import "core:strings"
import "core:math/rand"
/* globals */
cylinder := [6]bool{}
/* main block */
main :: proc() {
rand.set_global_seed(42)
tests := 100000
sequence := [?]string{"LSLSFSF", "LSLSFF", "LLSFSF", "LLSFF"}
for m in sequence {
sum := 0
for t in 0 ..< tests {
sum += method(m)
}
pc: f64 = cast(f64)sum * 100 / cast(f64)tests
fmt.printf("%-40s produces %6.3f%% deaths.\n", mstring(m), pc)
}
}
/* definitions */
rshift :: proc() {
t := cylinder[len(cylinder) - 1]
copy(cylinder[1:], cylinder[0:])
cylinder[0] = t
}
unload :: proc() {
cylinder = false // array programming
}
load :: proc() {
for cylinder[0] {
rshift()
}
cylinder[0] = true
rshift()
}
spin :: proc() {
data: []int = {1, 2, 3, 4, 5, 6}
lim := rand.choice(data[:])
for i in 0 ..< lim {
rshift()
}
}
fire :: proc() -> bool {
shot := cylinder[0]
rshift()
return shot
}
method :: proc(s: string) -> int {
unload()
for character in s {
switch character {
case 'L':
load()
case 'S':
spin()
case 'F':
if fire() {
return 1
}
}
}
return 0
}
mstring :: proc(s: string) -> string {
l: [dynamic]string
for character in s {
switch character {
case 'L':
append(&l, "load")
case 'S':
append(&l, "spin")
case 'F':
append(&l, "fire")
}
}
return strings.join(l[:], ", ")
}
You may also check:How to resolve the algorithm Sort an integer array step by step in the AWK programming language
You may also check:How to resolve the algorithm Aliquot sequence classifications step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the Haskell programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the Raku programming language
You may also check:How to resolve the algorithm Random Latin squares step by step in the Pascal programming language