回答編集履歴

9

モンティー・ホールは選択しない!

2018/04/19 14:56

投稿

raccy
raccy

スコア21735

test CHANGED
@@ -58,12 +58,30 @@
58
58
 
59
59
 
60
60
 
61
+ monty_hall_rate = count.times.count do
62
+
63
+ select_box = boxes.keys.sample
64
+
65
+ open_box = (boxes.keys - [select_box])
66
+
67
+ .reject(&boxes.method(:[])).sample
68
+
69
+ next_select_box = (boxes.keys - [select_box, open_box]).first
70
+
71
+ boxes[next_select_box]
72
+
73
+ end.to_f / count
74
+
75
+
76
+
61
77
  printf("%.1f%%\n", sonomama_rate * 100)
62
78
 
63
79
  printf("%.1f%%\n", naosu_rate * 100)
64
80
 
65
81
  printf("%.1f%%\n", monty_rate * 100)
66
82
 
83
+ printf("%.1f%%\n", monty_hall_rate * 100)
84
+
67
85
  ```
68
86
 
69
87
 
@@ -134,12 +152,36 @@
134
152
 
135
153
 
136
154
 
155
+ montyHallRate = [1..count].filter ->
156
+
157
+ selectBox = sample(Object.keys(boxes))
158
+
159
+ openBox = sample(Object.keys(boxes)
160
+
161
+ .filter((box) -> box != selectBox)
162
+
163
+ .filter((box) -> !boxes[box]))
164
+
165
+ next_selectBox = Object.keys(boxes)
166
+
167
+ .filter((box) -> box != selectBox && box != openBox)
168
+
169
+ boxes[next_selectBox]
170
+
171
+ .length / count
172
+
173
+
174
+
175
+
176
+
137
177
  console.log "#{(sonomamaRate * 100).toFixed(1)}%"
138
178
 
139
179
  console.log "#{(naosuRate * 100).toFixed(1)}%"
140
180
 
141
181
  console.log "#{(montyRate * 100).toFixed(1)}%"
142
182
 
183
+ console.log "#{(montyHallRate * 100).toFixed(1)}%"
184
+
143
185
  ```
144
186
 
145
187
 
@@ -196,7 +238,7 @@
196
238
 
197
239
  val selectBox = boxes.keys.shuffled().first()
198
240
 
199
- val open_box = boxes.keys
241
+ val openBox = boxes.keys
200
242
 
201
243
  .filterNot({ selectBox.equals(it) })
202
244
 
@@ -206,7 +248,7 @@
206
248
 
207
249
  val nextSelectBox = boxes.keys
208
250
 
209
- .filterNot({ open_box.equals(it) })
251
+ .filterNot({ openBox.equals(it) })
210
252
 
211
253
  .shuffled().first()
212
254
 
@@ -216,12 +258,38 @@
216
258
 
217
259
 
218
260
 
261
+ val montyHallRate = IntRange(1, count).count(fun(_: Int): Boolean {
262
+
263
+ val selectBox = boxes.keys.shuffled().first()
264
+
265
+ val openBox = boxes.keys
266
+
267
+ .filterNot({ selectBox.equals(it) })
268
+
269
+ .filterNot({ boxes.getOrDefault(it, false) })
270
+
271
+ .shuffled().first()
272
+
273
+ val nextSelectBox = boxes.keys
274
+
275
+ .filterNot({ selectBox.equals(it) || openBox.equals(it) })
276
+
277
+ .shuffled().first()
278
+
279
+ return boxes.getOrDefault(nextSelectBox, false)
280
+
281
+ }).toDouble() / count
282
+
283
+
284
+
219
285
  println("%.1f%%".format(sonomamaRate * 100))
220
286
 
221
287
  println("%.1f%%".format(naosuRate * 100))
222
288
 
223
289
  println("%.1f%%".format(montyRate * 100))
224
290
 
291
+ println("%.1f%%".format(montyHallRate * 100))
292
+
225
293
  }
226
294
 
227
295
  ```

8

最初Kotlinスクリプトで書いていたから外出ししていたけど、なんでトップにあんねん!とおもってmainの中に引っ込めた。

2018/04/19 14:55

投稿

raccy
raccy

スコア21735

test CHANGED
@@ -150,72 +150,72 @@
150
150
 
151
151
 
152
152
 
153
- val boxes = mapOf(
154
-
155
- "a" to true,
156
-
157
- "b" to false,
158
-
159
- "c" to false)
160
-
161
-
162
-
163
- val count = 100000 // 10万回
164
-
165
-
166
-
167
- val sonomamaRate = IntRange(1, count).count(fun(_: Int): Boolean {
168
-
169
- val selectBox = boxes.keys.shuffled().first()
170
-
171
- return boxes.getOrDefault(selectBox, false)
172
-
173
- }).toDouble() / count
174
-
175
-
176
-
177
- val naosuRate = IntRange(1, count).count(fun(_: Int): Boolean {
178
-
179
- val selectBox = boxes.keys.shuffled().first()
180
-
181
- val nextSelectBox = boxes.keys
182
-
183
- .filterNot({ selectBox.equals(it) })
184
-
185
- .shuffled().first()
186
-
187
- return boxes.getOrDefault(nextSelectBox, false)
188
-
189
- }).toDouble() / count
190
-
191
-
192
-
193
- val montyRate = IntRange(1, count).count(fun(_: Int): Boolean {
194
-
195
- val selectBox = boxes.keys.shuffled().first()
196
-
197
- val open_box = boxes.keys
198
-
199
- .filterNot({ selectBox.equals(it) })
200
-
201
- .filterNot({ boxes.getOrDefault(it, false) })
202
-
203
- .shuffled().first()
204
-
205
- val nextSelectBox = boxes.keys
206
-
207
- .filterNot({ open_box.equals(it) })
208
-
209
- .shuffled().first()
210
-
211
- return boxes.getOrDefault(nextSelectBox, false)
212
-
213
- }).toDouble() / count
214
-
215
-
216
-
217
153
  fun main(args: Array<String>) {
218
154
 
155
+ val boxes = mapOf(
156
+
157
+ "a" to true,
158
+
159
+ "b" to false,
160
+
161
+ "c" to false)
162
+
163
+
164
+
165
+ val count = 100000 // 10万回
166
+
167
+
168
+
169
+ val sonomamaRate = IntRange(1, count).count(fun(_: Int): Boolean {
170
+
171
+ val selectBox = boxes.keys.shuffled().first()
172
+
173
+ return boxes.getOrDefault(selectBox, false)
174
+
175
+ }).toDouble() / count
176
+
177
+
178
+
179
+ val naosuRate = IntRange(1, count).count(fun(_: Int): Boolean {
180
+
181
+ val selectBox = boxes.keys.shuffled().first()
182
+
183
+ val nextSelectBox = boxes.keys
184
+
185
+ .filterNot({ selectBox.equals(it) })
186
+
187
+ .shuffled().first()
188
+
189
+ return boxes.getOrDefault(nextSelectBox, false)
190
+
191
+ }).toDouble() / count
192
+
193
+
194
+
195
+ val montyRate = IntRange(1, count).count(fun(_: Int): Boolean {
196
+
197
+ val selectBox = boxes.keys.shuffled().first()
198
+
199
+ val open_box = boxes.keys
200
+
201
+ .filterNot({ selectBox.equals(it) })
202
+
203
+ .filterNot({ boxes.getOrDefault(it, false) })
204
+
205
+ .shuffled().first()
206
+
207
+ val nextSelectBox = boxes.keys
208
+
209
+ .filterNot({ open_box.equals(it) })
210
+
211
+ .shuffled().first()
212
+
213
+ return boxes.getOrDefault(nextSelectBox, false)
214
+
215
+ }).toDouble() / count
216
+
217
+
218
+
219
219
  println("%.1f%%".format(sonomamaRate * 100))
220
220
 
221
221
  println("%.1f%%".format(naosuRate * 100))

7

上ってどこだ?

2018/04/19 14:43

投稿

raccy
raccy

スコア21735

test CHANGED
@@ -226,4 +226,4 @@
226
226
 
227
227
  ```
228
228
 
229
- のコードはKotlinです。Kotlinはシンタックス指定が対応していないっぽい。
229
+ 3番目のコードはKotlinです。Kotlinはシンタックス指定が対応していないっぽい。

6

はじめてのKotlin

2018/04/19 14:20

投稿

raccy
raccy

スコア21735

test CHANGED
@@ -225,3 +225,5 @@
225
225
  }
226
226
 
227
227
  ```
228
+
229
+ ※ 上のコードはKotlinです。Kotlinはシンタックス指定が対応していないっぽい。

5

Javaが抜けていたので追加した。

2018/04/19 14:19

投稿

raccy
raccy

スコア21735

test CHANGED
@@ -141,3 +141,87 @@
141
141
  console.log "#{(montyRate * 100).toFixed(1)}%"
142
142
 
143
143
  ```
144
+
145
+
146
+
147
+ ```Kotlin
148
+
149
+ import kotlin.ranges.IntRange
150
+
151
+
152
+
153
+ val boxes = mapOf(
154
+
155
+ "a" to true,
156
+
157
+ "b" to false,
158
+
159
+ "c" to false)
160
+
161
+
162
+
163
+ val count = 100000 // 10万回
164
+
165
+
166
+
167
+ val sonomamaRate = IntRange(1, count).count(fun(_: Int): Boolean {
168
+
169
+ val selectBox = boxes.keys.shuffled().first()
170
+
171
+ return boxes.getOrDefault(selectBox, false)
172
+
173
+ }).toDouble() / count
174
+
175
+
176
+
177
+ val naosuRate = IntRange(1, count).count(fun(_: Int): Boolean {
178
+
179
+ val selectBox = boxes.keys.shuffled().first()
180
+
181
+ val nextSelectBox = boxes.keys
182
+
183
+ .filterNot({ selectBox.equals(it) })
184
+
185
+ .shuffled().first()
186
+
187
+ return boxes.getOrDefault(nextSelectBox, false)
188
+
189
+ }).toDouble() / count
190
+
191
+
192
+
193
+ val montyRate = IntRange(1, count).count(fun(_: Int): Boolean {
194
+
195
+ val selectBox = boxes.keys.shuffled().first()
196
+
197
+ val open_box = boxes.keys
198
+
199
+ .filterNot({ selectBox.equals(it) })
200
+
201
+ .filterNot({ boxes.getOrDefault(it, false) })
202
+
203
+ .shuffled().first()
204
+
205
+ val nextSelectBox = boxes.keys
206
+
207
+ .filterNot({ open_box.equals(it) })
208
+
209
+ .shuffled().first()
210
+
211
+ return boxes.getOrDefault(nextSelectBox, false)
212
+
213
+ }).toDouble() / count
214
+
215
+
216
+
217
+ fun main(args: Array<String>) {
218
+
219
+ println("%.1f%%".format(sonomamaRate * 100))
220
+
221
+ println("%.1f%%".format(naosuRate * 100))
222
+
223
+ println("%.1f%%".format(montyRate * 100))
224
+
225
+ }
226
+
227
+ ```

4

Rubyに引っ張られてスネークケースになっていたのでキャメルケースに直す

2018/04/19 14:18

投稿

raccy
raccy

スコア21735

test CHANGED
@@ -90,54 +90,54 @@
90
90
 
91
91
 
92
92
 
93
- sonomama_rate = [0...count].filter ->
93
+ sonomamaRate = [1..count].filter ->
94
94
 
95
- select_box = sample(Object.keys(boxes))
95
+ selectBox = sample(Object.keys(boxes))
96
96
 
97
- boxes[select_box]
97
+ boxes[selectBox]
98
98
 
99
99
  .length / count
100
100
 
101
101
 
102
102
 
103
- naosu_rate = [0...count].filter ->
103
+ naosuRate = [1..count].filter ->
104
104
 
105
- select_box = sample(Object.keys(boxes))
105
+ selectBox = sample(Object.keys(boxes))
106
106
 
107
- next_select_box = sample(Object.keys(boxes)
107
+ next_selectBox = sample(Object.keys(boxes)
108
108
 
109
- .filter((box) -> box != select_box))
109
+ .filter((box) -> box != selectBox))
110
110
 
111
- boxes[next_select_box]
111
+ boxes[next_selectBox]
112
112
 
113
113
  .length / count
114
114
 
115
115
 
116
116
 
117
- monty_rate = [0...count].filter ->
117
+ montyRate = [1..count].filter ->
118
118
 
119
- select_box = sample(Object.keys(boxes))
119
+ selectBox = sample(Object.keys(boxes))
120
120
 
121
- open_box = sample(Object.keys(boxes)
121
+ openBox = sample(Object.keys(boxes)
122
122
 
123
- .filter((box) -> box != select_box)
123
+ .filter((box) -> box != selectBox)
124
124
 
125
125
  .filter((box) -> !boxes[box]))
126
126
 
127
- next_select_box = sample(Object.keys(boxes)
127
+ next_selectBox = sample(Object.keys(boxes)
128
128
 
129
- .filter((box) -> box != open_box))
129
+ .filter((box) -> box != openBox))
130
130
 
131
- boxes[next_select_box]
131
+ boxes[next_selectBox]
132
132
 
133
133
  .length / count
134
134
 
135
135
 
136
136
 
137
- console.log "#{(sonomama_rate * 100).toFixed(1)}%"
137
+ console.log "#{(sonomamaRate * 100).toFixed(1)}%"
138
138
 
139
- console.log "#{(naosu_rate * 100).toFixed(1)}%"
139
+ console.log "#{(naosuRate * 100).toFixed(1)}%"
140
140
 
141
- console.log "#{(monty_rate * 100).toFixed(1)}%"
141
+ console.log "#{(montyRate * 100).toFixed(1)}%"
142
142
 
143
143
  ```

3

JavaScriptのタグがあることに気付いた

2018/04/19 14:14

投稿

raccy
raccy

スコア21735

test CHANGED
@@ -65,3 +65,79 @@
65
65
  printf("%.1f%%\n", monty_rate * 100)
66
66
 
67
67
  ```
68
+
69
+
70
+
71
+ ```CoffeeScript
72
+
73
+ boxes =
74
+
75
+ a: true
76
+
77
+ b: false
78
+
79
+ c: false
80
+
81
+
82
+
83
+ count = 100000 # 10万回
84
+
85
+
86
+
87
+ sample = (arr) ->
88
+
89
+ arr[Math.floor(Math.random() * arr.length)]
90
+
91
+
92
+
93
+ sonomama_rate = [0...count].filter ->
94
+
95
+ select_box = sample(Object.keys(boxes))
96
+
97
+ boxes[select_box]
98
+
99
+ .length / count
100
+
101
+
102
+
103
+ naosu_rate = [0...count].filter ->
104
+
105
+ select_box = sample(Object.keys(boxes))
106
+
107
+ next_select_box = sample(Object.keys(boxes)
108
+
109
+ .filter((box) -> box != select_box))
110
+
111
+ boxes[next_select_box]
112
+
113
+ .length / count
114
+
115
+
116
+
117
+ monty_rate = [0...count].filter ->
118
+
119
+ select_box = sample(Object.keys(boxes))
120
+
121
+ open_box = sample(Object.keys(boxes)
122
+
123
+ .filter((box) -> box != select_box)
124
+
125
+ .filter((box) -> !boxes[box]))
126
+
127
+ next_select_box = sample(Object.keys(boxes)
128
+
129
+ .filter((box) -> box != open_box))
130
+
131
+ boxes[next_select_box]
132
+
133
+ .length / count
134
+
135
+
136
+
137
+ console.log "#{(sonomama_rate * 100).toFixed(1)}%"
138
+
139
+ console.log "#{(naosu_rate * 100).toFixed(1)}%"
140
+
141
+ console.log "#{(monty_rate * 100).toFixed(1)}%"
142
+
143
+ ```

2

rateは最大1で%にするときに100かけた方が綺麗になるような気がした。たぶん、木の精だけど。

2018/04/19 13:08

投稿

raccy
raccy

スコア21735

test CHANGED
@@ -26,7 +26,7 @@
26
26
 
27
27
  boxes[select_box]
28
28
 
29
- end.to_f / count * 100
29
+ end.to_f / count
30
30
 
31
31
 
32
32
 
@@ -38,7 +38,7 @@
38
38
 
39
39
  boxes[next_select_box]
40
40
 
41
- end.to_f / count * 100
41
+ end.to_f / count
42
42
 
43
43
 
44
44
 
@@ -54,14 +54,14 @@
54
54
 
55
55
  boxes[next_select_box]
56
56
 
57
- end.to_f / count * 100
57
+ end.to_f / count
58
58
 
59
59
 
60
60
 
61
- printf("%.1f%%\n", sonomama_rate)
61
+ printf("%.1f%%\n", sonomama_rate * 100)
62
62
 
63
- printf("%.1f%%\n", naosu_rate)
63
+ printf("%.1f%%\n", naosu_rate * 100)
64
64
 
65
- printf("%.1f%%\n", monty_rate)
65
+ printf("%.1f%%\n", monty_rate * 100)
66
66
 
67
67
  ```

1

sampleメソッドの方が良かった

2018/04/19 11:47

投稿

raccy
raccy

スコア21735

test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
  sonomama_rate = count.times.count do
24
24
 
25
- select_box = boxes.keys.shuffle.first
25
+ select_box = boxes.keys.sample
26
26
 
27
27
  boxes[select_box]
28
28
 
@@ -32,9 +32,9 @@
32
32
 
33
33
  naosu_rate = count.times.count do
34
34
 
35
- select_box = boxes.keys.shuffle.first
35
+ select_box = boxes.keys.sample
36
36
 
37
- next_select_box = (boxes.keys - [select_box]).shuffle.first
37
+ next_select_box = (boxes.keys - [select_box]).sample
38
38
 
39
39
  boxes[next_select_box]
40
40
 
@@ -44,13 +44,13 @@
44
44
 
45
45
  monty_rate = count.times.count do
46
46
 
47
- select_box = boxes.keys.shuffle.first
47
+ select_box = boxes.keys.sample
48
48
 
49
49
  open_box = (boxes.keys - [select_box])
50
50
 
51
- .reject(&boxes.method(:[])).shuffle.first
51
+ .reject(&boxes.method(:[])).sample
52
52
 
53
- next_select_box = (boxes.keys - [open_box]).shuffle.first
53
+ next_select_box = (boxes.keys - [open_box]).sample
54
54
 
55
55
  boxes[next_select_box]
56
56