monty_hall_problem 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6f60ba7f7ad7fa47cdbe018339b9b5a646ea3c3eb0c55dc22f6a111e83e8c058
4
+ data.tar.gz: 4fd644683723c099f39d59a486566ced64f3a1846496cfee08d8424015e6c14b
5
+ SHA512:
6
+ metadata.gz: 31e23af302ceff776890ca5280e9bc036ba0423e6beffba26986bb832045cac01eec7c247f6333c8e6a32e7709cacaf08c44e74148ce74a42e559ffed62ea1c4
7
+ data.tar.gz: c94ef33e5c181e7148d0222ce0035899ce836f0b4e1481e34758a8edc34b817c80defcd3e8f4437866235cdd83eef8524cf00d595614aef5609e1a48d6469676
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 xb
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # MontyHallProblem
2
+
3
+ Simulate the [Monty Hall Problem](https://en.wikipedia.org/wiki/Monty_Hall_problem)
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ require 'monty_hall_problem'
9
+
10
+ MontyHallProblem::Playground.new(100).play(:change).play(:no_change)
11
+ # change - all:100; win:69; lose:31; P(win)=69.0%
12
+ # no_change - all:100; win:34; lose:66; P(win)=34.0%
13
+
14
+ MontyHallProblem::Playground.new(100, door_size: 10, car_size: 1, open_size: 8).play(:change).play(:no_change)
15
+ # change - all:100; win:91; lose:9; P(win)=91.0%
16
+ # no_change - all:100; win:9; lose:91; P(win)=9.0%
17
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MontyHallProblem
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "monty_hall_problem/version"
4
+
5
+ module MontyHallProblem
6
+ class Error < StandardError; end
7
+ class Host
8
+ def initialize(doors, car_size=1)
9
+ @doors = doors
10
+ @car_size = car_size
11
+ end
12
+
13
+ def put_cars
14
+ @car_size.times do
15
+ loop do
16
+ break if @doors.put(rand(@doors.size))
17
+ end
18
+ end
19
+ end
20
+
21
+ def open_wronth_door(idx, open_size = 1)
22
+ idxs = (@doors.no_car_door_idx - Array(idx)).sample(open_size)
23
+ @doors.opened_door_idx += idxs unless idxs.empty?
24
+ idxs
25
+ end
26
+
27
+ def make_known(idx)
28
+ @doors.door[idx]
29
+ end
30
+ end
31
+
32
+ class Player
33
+ def initialize(doors)
34
+ @doors = doors
35
+ end
36
+
37
+ def choose_door
38
+ rand(@doors.size)
39
+ end
40
+
41
+ def change_door(choosed_door_idx)
42
+ (Array.new(@doors.size) { |i| i } - (@doors.opened_door_idx + Array(choosed_door_idx))).sample || choosed_door_idx
43
+ end
44
+ end
45
+
46
+ class Doors
47
+ attr_accessor :size
48
+ attr_accessor :door
49
+ attr_accessor :opened_door_idx
50
+
51
+ def initialize(size=3)
52
+ self.size = size
53
+ self.door = Array.new(size) { false }
54
+ self.opened_door_idx = []
55
+ end
56
+
57
+ def put(door_idx)
58
+ if door[door_idx]
59
+ return false
60
+ else
61
+ door[door_idx] = true
62
+ return true
63
+ end
64
+ end
65
+
66
+ def car_door_idx
67
+ door.map.with_index{ |e, i| i if !!e }.compact
68
+ end
69
+
70
+ def no_car_door_idx
71
+ door.map.with_index{ |e, i| i if !e }.compact
72
+ end
73
+ end
74
+
75
+ class Playground
76
+ attr_accessor :times
77
+
78
+ def initialize(times, door_size: 3, car_size: 1, open_size: 1)
79
+ self.times = times
80
+ @door_size = door_size
81
+ @car_size = car_size
82
+ @open_size = open_size
83
+ end
84
+
85
+ def play_change
86
+ doors = Doors.new(@door_size)
87
+ host = Host.new(doors, @car_size)
88
+ player = Player.new(doors)
89
+
90
+ host.put_cars
91
+ idx = player.choose_door
92
+ host.open_wronth_door(idx, @open_size)
93
+ idx = player.change_door(idx)
94
+ host.make_known(idx) ? :win : :lose
95
+ end
96
+
97
+ def play_no_change
98
+ doors = Doors.new(@door_size)
99
+ host = Host.new(doors, @car_size)
100
+ player = Player.new(doors)
101
+
102
+ host.put_cars
103
+ idx = player.choose_door
104
+ host.open_wronth_door(idx, @open_size)
105
+ host.make_known(idx) ? :win : :lose
106
+ end
107
+
108
+ def play(mode)
109
+ result = self.times.times.map do
110
+ mode == :change ? play_change : play_no_change
111
+ end
112
+
113
+ win_count = result.count { |e| e == :win }
114
+ lose_count = result.count { |e| e == :lose }
115
+ puts "#{mode} - all:#{self.times}; win:#{win_count}; lose:#{lose_count}; P(win)=#{(win_count.to_f/self.times.to_f * 100).round(2)}%"
116
+ self
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,4 @@
1
+ module MontyHallProblem
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monty_hall_problem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - XB
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-12-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: https://en.wikipedia.org/wiki/Monty_Hall_problem
14
+ email:
15
+ - hsw840139210@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - lib/monty_hall_problem.rb
26
+ - lib/monty_hall_problem/version.rb
27
+ - sig/monty_hall_problem.rbs
28
+ homepage: https://github.com/doraemon0711/monty_hall_problem
29
+ licenses: []
30
+ metadata:
31
+ allowed_push_host: https://rubygems.org/
32
+ homepage_uri: https://github.com/doraemon0711/monty_hall_problem
33
+ source_code_uri: https://github.com/doraemon0711/monty_hall_problem
34
+ changelog_uri: https://github.com/doraemon0711/monty_hall_problem
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.0.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.5.4
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Simulate the Monty Hall Problem
54
+ test_files: []