回答編集履歴
9
モンティー・ホールは選択しない!
answer
CHANGED
@@ -28,9 +28,18 @@
|
|
28
28
|
boxes[next_select_box]
|
29
29
|
end.to_f / count
|
30
30
|
|
31
|
+
monty_hall_rate = count.times.count do
|
32
|
+
select_box = boxes.keys.sample
|
33
|
+
open_box = (boxes.keys - [select_box])
|
34
|
+
.reject(&boxes.method(:[])).sample
|
35
|
+
next_select_box = (boxes.keys - [select_box, open_box]).first
|
36
|
+
boxes[next_select_box]
|
37
|
+
end.to_f / count
|
38
|
+
|
31
39
|
printf("%.1f%%\n", sonomama_rate * 100)
|
32
40
|
printf("%.1f%%\n", naosu_rate * 100)
|
33
41
|
printf("%.1f%%\n", monty_rate * 100)
|
42
|
+
printf("%.1f%%\n", monty_hall_rate * 100)
|
34
43
|
```
|
35
44
|
|
36
45
|
```CoffeeScript
|
@@ -66,9 +75,21 @@
|
|
66
75
|
boxes[next_selectBox]
|
67
76
|
.length / count
|
68
77
|
|
78
|
+
montyHallRate = [1..count].filter ->
|
79
|
+
selectBox = sample(Object.keys(boxes))
|
80
|
+
openBox = sample(Object.keys(boxes)
|
81
|
+
.filter((box) -> box != selectBox)
|
82
|
+
.filter((box) -> !boxes[box]))
|
83
|
+
next_selectBox = Object.keys(boxes)
|
84
|
+
.filter((box) -> box != selectBox && box != openBox)
|
85
|
+
boxes[next_selectBox]
|
86
|
+
.length / count
|
87
|
+
|
88
|
+
|
69
89
|
console.log "#{(sonomamaRate * 100).toFixed(1)}%"
|
70
90
|
console.log "#{(naosuRate * 100).toFixed(1)}%"
|
71
91
|
console.log "#{(montyRate * 100).toFixed(1)}%"
|
92
|
+
console.log "#{(montyHallRate * 100).toFixed(1)}%"
|
72
93
|
```
|
73
94
|
|
74
95
|
```Kotlin
|
@@ -97,19 +118,32 @@
|
|
97
118
|
|
98
119
|
val montyRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
99
120
|
val selectBox = boxes.keys.shuffled().first()
|
100
|
-
val
|
121
|
+
val openBox = boxes.keys
|
101
122
|
.filterNot({ selectBox.equals(it) })
|
102
123
|
.filterNot({ boxes.getOrDefault(it, false) })
|
103
124
|
.shuffled().first()
|
104
125
|
val nextSelectBox = boxes.keys
|
105
|
-
.filterNot({
|
126
|
+
.filterNot({ openBox.equals(it) })
|
106
127
|
.shuffled().first()
|
107
128
|
return boxes.getOrDefault(nextSelectBox, false)
|
108
129
|
}).toDouble() / count
|
109
130
|
|
131
|
+
val montyHallRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
132
|
+
val selectBox = boxes.keys.shuffled().first()
|
133
|
+
val openBox = boxes.keys
|
134
|
+
.filterNot({ selectBox.equals(it) })
|
135
|
+
.filterNot({ boxes.getOrDefault(it, false) })
|
136
|
+
.shuffled().first()
|
137
|
+
val nextSelectBox = boxes.keys
|
138
|
+
.filterNot({ selectBox.equals(it) || openBox.equals(it) })
|
139
|
+
.shuffled().first()
|
140
|
+
return boxes.getOrDefault(nextSelectBox, false)
|
141
|
+
}).toDouble() / count
|
142
|
+
|
110
143
|
println("%.1f%%".format(sonomamaRate * 100))
|
111
144
|
println("%.1f%%".format(naosuRate * 100))
|
112
145
|
println("%.1f%%".format(montyRate * 100))
|
146
|
+
println("%.1f%%".format(montyHallRate * 100))
|
113
147
|
}
|
114
148
|
```
|
115
149
|
※ 3番目のコードはKotlinです。Kotlinはシンタックス指定が対応していないっぽい。
|
8
最初Kotlinスクリプトで書いていたから外出ししていたけど、なんでトップにあんねん!とおもってmainの中に引っ込めた。
answer
CHANGED
@@ -74,39 +74,39 @@
|
|
74
74
|
```Kotlin
|
75
75
|
import kotlin.ranges.IntRange
|
76
76
|
|
77
|
+
fun main(args: Array<String>) {
|
77
|
-
val boxes = mapOf(
|
78
|
+
val boxes = mapOf(
|
78
|
-
|
79
|
+
"a" to true,
|
79
|
-
|
80
|
+
"b" to false,
|
80
|
-
|
81
|
+
"c" to false)
|
81
82
|
|
82
|
-
val count = 100000 // 10万回
|
83
|
+
val count = 100000 // 10万回
|
83
84
|
|
84
|
-
val sonomamaRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
85
|
+
val sonomamaRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
85
|
-
|
86
|
+
val selectBox = boxes.keys.shuffled().first()
|
86
|
-
|
87
|
+
return boxes.getOrDefault(selectBox, false)
|
87
|
-
}).toDouble() / count
|
88
|
+
}).toDouble() / count
|
88
89
|
|
89
|
-
val naosuRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
90
|
+
val naosuRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
90
|
-
|
91
|
+
val selectBox = boxes.keys.shuffled().first()
|
91
|
-
|
92
|
+
val nextSelectBox = boxes.keys
|
92
|
-
|
93
|
+
.filterNot({ selectBox.equals(it) })
|
93
|
-
|
94
|
+
.shuffled().first()
|
94
|
-
|
95
|
+
return boxes.getOrDefault(nextSelectBox, false)
|
95
|
-
}).toDouble() / count
|
96
|
+
}).toDouble() / count
|
96
97
|
|
97
|
-
val montyRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
98
|
+
val montyRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
98
|
-
|
99
|
+
val selectBox = boxes.keys.shuffled().first()
|
99
|
-
|
100
|
+
val open_box = boxes.keys
|
100
|
-
|
101
|
+
.filterNot({ selectBox.equals(it) })
|
101
|
-
|
102
|
+
.filterNot({ boxes.getOrDefault(it, false) })
|
102
|
-
|
103
|
+
.shuffled().first()
|
103
|
-
|
104
|
+
val nextSelectBox = boxes.keys
|
104
|
-
|
105
|
+
.filterNot({ open_box.equals(it) })
|
105
|
-
|
106
|
+
.shuffled().first()
|
106
|
-
|
107
|
+
return boxes.getOrDefault(nextSelectBox, false)
|
107
|
-
}).toDouble() / count
|
108
|
+
}).toDouble() / count
|
108
109
|
|
109
|
-
fun main(args: Array<String>) {
|
110
110
|
println("%.1f%%".format(sonomamaRate * 100))
|
111
111
|
println("%.1f%%".format(naosuRate * 100))
|
112
112
|
println("%.1f%%".format(montyRate * 100))
|
7
上ってどこだ?
answer
CHANGED
@@ -112,4 +112,4 @@
|
|
112
112
|
println("%.1f%%".format(montyRate * 100))
|
113
113
|
}
|
114
114
|
```
|
115
|
-
※
|
115
|
+
※ 3番目のコードはKotlinです。Kotlinはシンタックス指定が対応していないっぽい。
|
6
はじめてのKotlin
answer
CHANGED
@@ -111,4 +111,5 @@
|
|
111
111
|
println("%.1f%%".format(naosuRate * 100))
|
112
112
|
println("%.1f%%".format(montyRate * 100))
|
113
113
|
}
|
114
|
-
```
|
114
|
+
```
|
115
|
+
※ 上のコードはKotlinです。Kotlinはシンタックス指定が対応していないっぽい。
|
5
Javaが抜けていたので追加した。
answer
CHANGED
@@ -69,4 +69,46 @@
|
|
69
69
|
console.log "#{(sonomamaRate * 100).toFixed(1)}%"
|
70
70
|
console.log "#{(naosuRate * 100).toFixed(1)}%"
|
71
71
|
console.log "#{(montyRate * 100).toFixed(1)}%"
|
72
|
+
```
|
73
|
+
|
74
|
+
```Kotlin
|
75
|
+
import kotlin.ranges.IntRange
|
76
|
+
|
77
|
+
val boxes = mapOf(
|
78
|
+
"a" to true,
|
79
|
+
"b" to false,
|
80
|
+
"c" to false)
|
81
|
+
|
82
|
+
val count = 100000 // 10万回
|
83
|
+
|
84
|
+
val sonomamaRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
85
|
+
val selectBox = boxes.keys.shuffled().first()
|
86
|
+
return boxes.getOrDefault(selectBox, false)
|
87
|
+
}).toDouble() / count
|
88
|
+
|
89
|
+
val naosuRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
90
|
+
val selectBox = boxes.keys.shuffled().first()
|
91
|
+
val nextSelectBox = boxes.keys
|
92
|
+
.filterNot({ selectBox.equals(it) })
|
93
|
+
.shuffled().first()
|
94
|
+
return boxes.getOrDefault(nextSelectBox, false)
|
95
|
+
}).toDouble() / count
|
96
|
+
|
97
|
+
val montyRate = IntRange(1, count).count(fun(_: Int): Boolean {
|
98
|
+
val selectBox = boxes.keys.shuffled().first()
|
99
|
+
val open_box = boxes.keys
|
100
|
+
.filterNot({ selectBox.equals(it) })
|
101
|
+
.filterNot({ boxes.getOrDefault(it, false) })
|
102
|
+
.shuffled().first()
|
103
|
+
val nextSelectBox = boxes.keys
|
104
|
+
.filterNot({ open_box.equals(it) })
|
105
|
+
.shuffled().first()
|
106
|
+
return boxes.getOrDefault(nextSelectBox, false)
|
107
|
+
}).toDouble() / count
|
108
|
+
|
109
|
+
fun main(args: Array<String>) {
|
110
|
+
println("%.1f%%".format(sonomamaRate * 100))
|
111
|
+
println("%.1f%%".format(naosuRate * 100))
|
112
|
+
println("%.1f%%".format(montyRate * 100))
|
113
|
+
}
|
72
114
|
```
|
4
Rubyに引っ張られてスネークケースになっていたのでキャメルケースに直す
answer
CHANGED
@@ -44,29 +44,29 @@
|
|
44
44
|
sample = (arr) ->
|
45
45
|
arr[Math.floor(Math.random() * arr.length)]
|
46
46
|
|
47
|
-
|
47
|
+
sonomamaRate = [1..count].filter ->
|
48
|
-
|
48
|
+
selectBox = sample(Object.keys(boxes))
|
49
|
-
boxes[
|
49
|
+
boxes[selectBox]
|
50
50
|
.length / count
|
51
51
|
|
52
|
-
|
52
|
+
naosuRate = [1..count].filter ->
|
53
|
-
|
53
|
+
selectBox = sample(Object.keys(boxes))
|
54
|
-
|
54
|
+
next_selectBox = sample(Object.keys(boxes)
|
55
|
-
.filter((box) -> box !=
|
55
|
+
.filter((box) -> box != selectBox))
|
56
|
-
boxes[
|
56
|
+
boxes[next_selectBox]
|
57
57
|
.length / count
|
58
58
|
|
59
|
-
|
59
|
+
montyRate = [1..count].filter ->
|
60
|
-
|
60
|
+
selectBox = sample(Object.keys(boxes))
|
61
|
-
|
61
|
+
openBox = sample(Object.keys(boxes)
|
62
|
-
.filter((box) -> box !=
|
62
|
+
.filter((box) -> box != selectBox)
|
63
63
|
.filter((box) -> !boxes[box]))
|
64
|
-
|
64
|
+
next_selectBox = sample(Object.keys(boxes)
|
65
|
-
.filter((box) -> box !=
|
65
|
+
.filter((box) -> box != openBox))
|
66
|
-
boxes[
|
66
|
+
boxes[next_selectBox]
|
67
67
|
.length / count
|
68
68
|
|
69
|
-
console.log "#{(
|
69
|
+
console.log "#{(sonomamaRate * 100).toFixed(1)}%"
|
70
|
-
console.log "#{(
|
70
|
+
console.log "#{(naosuRate * 100).toFixed(1)}%"
|
71
|
-
console.log "#{(
|
71
|
+
console.log "#{(montyRate * 100).toFixed(1)}%"
|
72
72
|
```
|
3
JavaScriptのタグがあることに気付いた
answer
CHANGED
@@ -31,4 +31,42 @@
|
|
31
31
|
printf("%.1f%%\n", sonomama_rate * 100)
|
32
32
|
printf("%.1f%%\n", naosu_rate * 100)
|
33
33
|
printf("%.1f%%\n", monty_rate * 100)
|
34
|
+
```
|
35
|
+
|
36
|
+
```CoffeeScript
|
37
|
+
boxes =
|
38
|
+
a: true
|
39
|
+
b: false
|
40
|
+
c: false
|
41
|
+
|
42
|
+
count = 100000 # 10万回
|
43
|
+
|
44
|
+
sample = (arr) ->
|
45
|
+
arr[Math.floor(Math.random() * arr.length)]
|
46
|
+
|
47
|
+
sonomama_rate = [0...count].filter ->
|
48
|
+
select_box = sample(Object.keys(boxes))
|
49
|
+
boxes[select_box]
|
50
|
+
.length / count
|
51
|
+
|
52
|
+
naosu_rate = [0...count].filter ->
|
53
|
+
select_box = sample(Object.keys(boxes))
|
54
|
+
next_select_box = sample(Object.keys(boxes)
|
55
|
+
.filter((box) -> box != select_box))
|
56
|
+
boxes[next_select_box]
|
57
|
+
.length / count
|
58
|
+
|
59
|
+
monty_rate = [0...count].filter ->
|
60
|
+
select_box = sample(Object.keys(boxes))
|
61
|
+
open_box = sample(Object.keys(boxes)
|
62
|
+
.filter((box) -> box != select_box)
|
63
|
+
.filter((box) -> !boxes[box]))
|
64
|
+
next_select_box = sample(Object.keys(boxes)
|
65
|
+
.filter((box) -> box != open_box))
|
66
|
+
boxes[next_select_box]
|
67
|
+
.length / count
|
68
|
+
|
69
|
+
console.log "#{(sonomama_rate * 100).toFixed(1)}%"
|
70
|
+
console.log "#{(naosu_rate * 100).toFixed(1)}%"
|
71
|
+
console.log "#{(monty_rate * 100).toFixed(1)}%"
|
34
72
|
```
|
2
rateは最大1で%にするときに100かけた方が綺麗になるような気がした。たぶん、木の精だけど。
answer
CHANGED
@@ -12,13 +12,13 @@
|
|
12
12
|
sonomama_rate = count.times.count do
|
13
13
|
select_box = boxes.keys.sample
|
14
14
|
boxes[select_box]
|
15
|
-
end.to_f / count
|
15
|
+
end.to_f / count
|
16
16
|
|
17
17
|
naosu_rate = count.times.count do
|
18
18
|
select_box = boxes.keys.sample
|
19
19
|
next_select_box = (boxes.keys - [select_box]).sample
|
20
20
|
boxes[next_select_box]
|
21
|
-
end.to_f / count
|
21
|
+
end.to_f / count
|
22
22
|
|
23
23
|
monty_rate = count.times.count do
|
24
24
|
select_box = boxes.keys.sample
|
@@ -26,9 +26,9 @@
|
|
26
26
|
.reject(&boxes.method(:[])).sample
|
27
27
|
next_select_box = (boxes.keys - [open_box]).sample
|
28
28
|
boxes[next_select_box]
|
29
|
-
end.to_f / count
|
29
|
+
end.to_f / count
|
30
30
|
|
31
|
-
printf("%.1f%%\n", sonomama_rate)
|
31
|
+
printf("%.1f%%\n", sonomama_rate * 100)
|
32
|
-
printf("%.1f%%\n", naosu_rate)
|
32
|
+
printf("%.1f%%\n", naosu_rate * 100)
|
33
|
-
printf("%.1f%%\n", monty_rate)
|
33
|
+
printf("%.1f%%\n", monty_rate * 100)
|
34
34
|
```
|
1
sampleメソッドの方が良かった
answer
CHANGED
@@ -10,21 +10,21 @@
|
|
10
10
|
count = 100_000 # 10万回
|
11
11
|
|
12
12
|
sonomama_rate = count.times.count do
|
13
|
-
select_box = boxes.keys.
|
13
|
+
select_box = boxes.keys.sample
|
14
14
|
boxes[select_box]
|
15
15
|
end.to_f / count * 100
|
16
16
|
|
17
17
|
naosu_rate = count.times.count do
|
18
|
-
select_box = boxes.keys.
|
18
|
+
select_box = boxes.keys.sample
|
19
|
-
next_select_box = (boxes.keys - [select_box]).
|
19
|
+
next_select_box = (boxes.keys - [select_box]).sample
|
20
20
|
boxes[next_select_box]
|
21
21
|
end.to_f / count * 100
|
22
22
|
|
23
23
|
monty_rate = count.times.count do
|
24
|
-
select_box = boxes.keys.
|
24
|
+
select_box = boxes.keys.sample
|
25
25
|
open_box = (boxes.keys - [select_box])
|
26
|
-
.reject(&boxes.method(:[])).
|
26
|
+
.reject(&boxes.method(:[])).sample
|
27
|
-
next_select_box = (boxes.keys - [open_box]).
|
27
|
+
next_select_box = (boxes.keys - [open_box]).sample
|
28
28
|
boxes[next_select_box]
|
29
29
|
end.to_f / count * 100
|
30
30
|
|