Last active
July 10, 2024 01:55
-
-
Save gouf/e87bdd2c507f41244ad7db33d4965c2a to your computer and use it in GitHub Desktop.
Revisions
-
gouf revised this gist
Jul 10, 2024 . 1 changed file with 60 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ describe OrderStringAnalyzer do describe '#read_and_execute!' do context 'order string pattern 1' do let(:order_string) do <<~ORDER_STRING.lines.map(&:strip).join("\n") make 10 nana getnum 1 getname 1 ORDER_STRING end it 'reads and executes the commands' do analyzer = OrderStringAnalyzer.new(order_string) analyzer.read_and_execute! expect(analyzer.employee_recorder.getnum(1)).to eq(10) end context 'when the command was done successfully' do it 'can read data from the execution histories' do analyzer = OrderStringAnalyzer.new(order_string) analyzer.read_and_execute! result = [10, 'nana'] expect(analyzer.execution_histories).to eq(result) end end end context 'order string pattern 2' do let(:order_string) do <<~ORDER_STRING.lines.map(&:strip).join("\n") make 2742 mako getnum 1 make 2782 taisei getname 2 make 31 megumi getname 1 getname 3 ORDER_STRING end it 'reads and executes the commands' do analyzer = OrderStringAnalyzer.new(order_string) analyzer.read_and_execute! expect(analyzer.employee_recorder.getnum(1)).to eq(2742) end context 'when the command was done successfully' do it 'can read data from the execution histories' do analyzer = OrderStringAnalyzer.new(order_string) analyzer.read_and_execute! result = [2742, 'taisei', 'mako', 'megumi'] expect(analyzer.execution_histories).to eq(result) end end end end end -
gouf created this gist
Jul 10, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,8 @@ class Employee attr_reader :number, :name def initialize(number, name) @number = number.to_i @name = name.to_s end end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ class EmployeeRecorder attr_reader :employees def initialize @employees = [] end def make(number, name) @employees << Employee.new(number, name) nil end def getnum(index) @employees.each.with_index(1) do |employee, i| return employee.number if index.to_i.eql?(i) end end def getname(index) @employees.each.with_index(1) do |employee, i| return employee.name if index.to_i.eql?(i) end end end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ solver = Solver.new(Solver.read_input!) puts solver.solve! This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ class OrderStringAnalyzer attr_reader :order_string, :employee_recorder def initialize(order_string) @order_string = order_string @employee_recorder = EmployeeRecorder.new @execution_histories = [] end def read_and_execute! @execution_histories = order_string.lines.map do |line| command, *args = line.split employee_recorder.public_send(command, *args) end nil end def execution_histories @execution_histories.compact end end This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ class Solver attr_accessor :order_string def initialize(order_string) @order_string = order_string end def solve! OrderStringAnalyzer.new(order_string) .tap { _1.read_and_execute! } .then { _1.execution_histories } end class << self def read_input! gets.to_i.times.map do gets.chomp end.join("\n") end end end