From 0648f5c152b6753815020e835cab040219f2e22d Mon Sep 17 00:00:00 2001 From: Junichi Ito Date: Tue, 22 Oct 2013 21:33:04 +0900 Subject: [PATCH 1/5] Use regex replace --- 20131019/murase_fujiwara/src/relax_seating.rb | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/20131019/murase_fujiwara/src/relax_seating.rb b/20131019/murase_fujiwara/src/relax_seating.rb index 6d6eb93..8cf4abe 100644 --- a/20131019/murase_fujiwara/src/relax_seating.rb +++ b/20131019/murase_fujiwara/src/relax_seating.rb @@ -1,19 +1,13 @@ -require 'pp' - -def vacant_seat_index(seats) +def x(seats, c) virtual_seats = "-" + seats + "-" - - (virtual_seats =~ /---/) or - (virtual_seats =~ /--\w|\w--/) or - seats.index('-') - - # if idx = (virtual_seats =~ /---/) - # return idx - # end - # if idx = (virtual_seats =~ /--\w|\w--/) - # return idx - # end - # seats.index('-') + + rs = [/(?<=-)-(?=-)/, /(?<=-)-(?=\w)|(?<=\w)-(?=-)/, /(?<=\w)-(?=\w)/] + rs.each do |r| + if virtual_seats =~ r + replaced = virtual_seats.sub r, c + return replaced[1...-1] + end + end end def relax_seating(input) @@ -21,11 +15,9 @@ def relax_seating(input) seats = "-" * num.to_i sequence.each_char do |c| if c.upcase == c - idx = vacant_seat_index(seats) - seats[idx] = c + seats = x(seats, c) else - idx = seats.index(c.upcase) - seats[idx] = '-' + seats = seats.sub(c.upcase, '-') end end seats From 9bbb25de9535b9dfb0c7bc4c16f4298b96bbc1f6 Mon Sep 17 00:00:00 2001 From: Junichi Ito Date: Tue, 22 Oct 2013 21:33:22 +0900 Subject: [PATCH 2/5] Add .rspec file --- 20131019/murase_fujiwara/.rspec | 1 + 1 file changed, 1 insertion(+) create mode 100644 20131019/murase_fujiwara/.rspec diff --git a/20131019/murase_fujiwara/.rspec b/20131019/murase_fujiwara/.rspec new file mode 100644 index 0000000..4e1e0d2 --- /dev/null +++ b/20131019/murase_fujiwara/.rspec @@ -0,0 +1 @@ +--color From 80b08d6f43e40b1e132d6b120f70c78dbf5e4118 Mon Sep 17 00:00:00 2001 From: Junichi Ito Date: Tue, 22 Oct 2013 21:45:24 +0900 Subject: [PATCH 3/5] Refactoring --- 20131019/murase_fujiwara/src/relax_seating.rb | 24 ++++++------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/20131019/murase_fujiwara/src/relax_seating.rb b/20131019/murase_fujiwara/src/relax_seating.rb index 8cf4abe..d616208 100644 --- a/20131019/murase_fujiwara/src/relax_seating.rb +++ b/20131019/murase_fujiwara/src/relax_seating.rb @@ -1,24 +1,14 @@ -def x(seats, c) - virtual_seats = "-" + seats + "-" +def replace(seats, c) + virtual_seats = "-#{seats}-" rs = [/(?<=-)-(?=-)/, /(?<=-)-(?=\w)|(?<=\w)-(?=-)/, /(?<=\w)-(?=\w)/] - rs.each do |r| - if virtual_seats =~ r - replaced = virtual_seats.sub r, c - return replaced[1...-1] - end - end + virtual_seats.sub(rs.find{|r| virtual_seats.match(r) }, c)[1...-1] end def relax_seating(input) num, sequence = input.split(':') - seats = "-" * num.to_i - sequence.each_char do |c| - if c.upcase == c - seats = x(seats, c) - else - seats = seats.sub(c.upcase, '-') - end - end - seats + + sequence.each_char.inject("-" * num.to_i) {|seats, c| + c.upcase == c ? replace(seats, c) : seats.sub(c.upcase, '-') + } end From 24fef20c4c118e17d4b56347f938d8e713c3f838 Mon Sep 17 00:00:00 2001 From: Junichi Ito Date: Tue, 22 Oct 2013 22:26:59 +0900 Subject: [PATCH 4/5] Refactoring --- 20131019/murase_fujiwara/src/relax_seating.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/20131019/murase_fujiwara/src/relax_seating.rb b/20131019/murase_fujiwara/src/relax_seating.rb index d616208..c50f4d5 100644 --- a/20131019/murase_fujiwara/src/relax_seating.rb +++ b/20131019/murase_fujiwara/src/relax_seating.rb @@ -1,8 +1,9 @@ +REGEX_ARRAY = [/(?<=-)-(?=-)/, /(?<=-)-(?=\w)|(?<=\w)-(?=-)/, /(?<=\w)-(?=\w)/] + def replace(seats, c) virtual_seats = "-#{seats}-" - rs = [/(?<=-)-(?=-)/, /(?<=-)-(?=\w)|(?<=\w)-(?=-)/, /(?<=\w)-(?=\w)/] - virtual_seats.sub(rs.find{|r| virtual_seats.match(r) }, c)[1...-1] + REGEX_ARRAY.map{|r| virtual_seats.sub(r, c) }.find{|s| s != virtual_seats}[1...-1] end def relax_seating(input) From d2da801dbdeac8254dc1fb136a01fdc0c97a3843 Mon Sep 17 00:00:00 2001 From: Junichi Ito Date: Tue, 22 Oct 2013 22:42:16 +0900 Subject: [PATCH 5/5] Refactoring --- 20131019/murase_fujiwara/src/relax_seating.rb | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/20131019/murase_fujiwara/src/relax_seating.rb b/20131019/murase_fujiwara/src/relax_seating.rb index c50f4d5..6143a7a 100644 --- a/20131019/murase_fujiwara/src/relax_seating.rb +++ b/20131019/murase_fujiwara/src/relax_seating.rb @@ -1,15 +1,12 @@ -REGEX_ARRAY = [/(?<=-)-(?=-)/, /(?<=-)-(?=\w)|(?<=\w)-(?=-)/, /(?<=\w)-(?=\w)/] - -def replace(seats, c) - virtual_seats = "-#{seats}-" - - REGEX_ARRAY.map{|r| virtual_seats.sub(r, c) }.find{|s| s != virtual_seats}[1...-1] -end +REGEX_ARRAY = [/(?<=-)-(?=-)/, /(?<=-)-(?=\w)|(?<=\w)-(?=-)/, /(?<=\w)-(?=\w)/].freeze +ESCAPE_FROM_VIRTUAL = (1...-1).freeze def relax_seating(input) num, sequence = input.split(':') + sequence.each_char.inject('-' * num.to_i) {|seats, c| c == c.upcase ? replace(seats, c) : seats.sub(c.upcase, '-') } +end - sequence.each_char.inject("-" * num.to_i) {|seats, c| - c.upcase == c ? replace(seats, c) : seats.sub(c.upcase, '-') - } +def replace(seats, c) + virtual_seats = "-#{seats}-" + REGEX_ARRAY.map {|r| virtual_seats.sub(r, c) }.find {|s| s != virtual_seats }[ESCAPE_FROM_VIRTUAL] end