Ruby: Refactoring a complicated nested-loop method - ruby

I'm trying to get rid of duplication in my code. I have a method that populates a checkerboard with checkers:
def populate_checkers
evens = [0, 2, 4, 6]
odds = [1, 3, 5, 7]
0.upto(2) do |x_coord|
if x_coord.even?
evens.each do |y_coord|
red_checker = Checker.new(x_coord, y_coord, :red)
#board[x_coord][y_coord] = red_checker
end
elsif x_coord.odd?
odds.each do |y_coord|
red_checker = Checker.new(x_coord, y_coord, :red)
#board[x_coord][y_coord] = red_checker
end
end
end
5.upto(7) do |x_coord|
if x_coord.even?
evens.each do |y_coord|
black_checker = Checker.new(x_coord, y_coord, :black)
#board[x_coord][y_coord] = black_checker
end
elsif x_coord.odd?
odds.each do |y_coord|
black_checker = Checker.new(x_coord, y_coord, :black)
#board[x_coord][y_coord] = black_checker
end
end
end
end
How can I remove duplication and still get the precise behavior I need?

You can try to extract a method and then extract a block into lambda. Then your code will be readable and loose of duplication
def populate_checkers
0.upto(2) do |x_coord|
populate_checker(x_coord, :red)
end
5.upto(7) do |x_coord|
populate_checker(x_cord, :black)
end
end
def populate_checker(x_coord, color)
evens = [0, 2, 4, 6]
odds = [1, 3, 5, 7]
apply_checker = lambda do |y_coord|
checker = Checker.new(x_coord, y_coord, color)
#board[x_coord][y_coord] = checker
end
if x_coord.even?
evens.each(&apply_checker)
elsif x_coord.odd?
odds.each(&apply_checker)
end
end

def populate_checkers
evens = [0, 2, 4, 6]
odds = [1, 3, 5, 7]
[0.upto(2), 5.upto(7)].each_with_index do |enum, i|
enum.each do |x_coord|
(x_coord.even? ? evens : odds).each do |y_coord|
checker = Checker.new(x_coord, y_coord, i == 0 ? :red : :black)
#board[x_coord][y_coord] = checker
end
end
end
end
There's probably a better way to do the counting bit, but that's what I got.
Here's a possibly better solution...
def populate_checkers
{ :red => (0..2), :black => (5..7) }.each do |color, range|
range.each do |x_coord|
(x_coord.even? ? 0 : 1).step(7, 2) do |y_coord|
checker = Checker.new(x_coord, y_coord, color)
#board[x_coord][y_coord] = checker
end
end
end
end

0.upto(2) do |x|
0.upto(7) do |y|
#board[x][y]=Checker.new(x, y, :red) if (x+y).even?
end
end
This is only for the reds.

Related

Ruby - Can't replace last range's element with another one

So I want to merge overlapping ranges and it should like the following:
Input: ranges = [(1..2), (3..6), (5..8)]
Output: expected = [(1..2), (3..8)]
but when the code iterate over the intervals and goes to the else statement I just get a message "function_merge.rb:9:in block in merge': undefined methodend=' for 2..19:Range (NoMethodError)"
I tried to save merged.last.end and interval.end to variables, rewrote the if statement over couple of lines (if interval.end > merged.last.end merged.last.end = interval.end end) but all that didn't work :-(
def merge(intervals)
merged = []
intervals.sort_by! { |interval| interval.begin }
intervals.each do |interval|
if merged.empty? || merged.last.end < interval.begin
merged << interval
else
merged.last.end = interval.end if interval.end > merged.last.end
end
end
return merged
end
I don't understand why I get this error message since "end" is a range method? I just want to "update" the merged.last.end with the interval.end number.
If you have any tips how to solve it, would be very nice :-)
As Sebastian points out, Ranges are immutable. Instead of trying to change the Range you'll have to make a new one.
def merge(intervals)
merged = []
intervals.sort_by! { |interval| interval.begin }
intervals.each do |interval|
if merged.empty? || merged.last.end < interval.begin
merged << interval
else
merged[-1] = Range.new(merged.last.begin, interval.end, interval.exclude_end?)
end
end
return merged
end
It has been explained that ranges are immutable. The question implies the elements covered by the ranges are all comparable (e.g, not ['a'..'z', 1..10]). I assume that the array of ranges does not contain a mix of finite and infinite ranges.
Solution
Code
def distill(arr)
a = arr.reject { |r| r.exclude_end? ? (r.end <= r.begin) : r.end < r.begin }.
sort_by(&:begin)
return [] if a.empty?
combined = []
curr = a.shift
loop do
break (combined << curr) if a.empty?
nxt = a.shift
if nxt.begin > curr.end
combined << curr
curr = nxt
else
last = [curr, nxt].max_by { |r| [r.end, r.exclude_end? ? 0 : 1] }
curr = last.exclude_end? ? (curr.begin...last.end) :
curr.begin..last.end
end
end
end
Examples
distill [5..8, 7...9, 9..11, 1...4, 38..37]
#=> [1...4, 5..11]
distill [1.5...2.2, 2.2..3.0, 3.0...4.5, 4.7..5.3, 5.2..4.6]
#=> [1.5...4.5, 4.7..5.3]
distill ['a'..'d', 'c'..'f', 'b'..'g']
# 'a'..'g'
Explanation
See Range#exclude_end?.
The steps for the first example are as follows.
arr = [5..8, 7...9, 9..11, 1...4, 38..37]
a = arr.reject { |r| r.exclude_end? ? (r.end <= r.begin) : r.end < r.begin }.
sort_by(&:begin)
#=> [1...4, 5..8, 7...9, 9..11]
a.empty?
#=> false, so do not return
combined = []
curr = a.shift
#=> 1...4
a #=> [5..8, 7...9, 9..11]
The calculations within the loop can be best explained by salting the code with puts statements and displaying the results.
loop do
puts "a.empty? #=> true, so break #{combined + [curr]}" if a.empty?
break (combined << curr) if a.empty?
puts "a.empty? #=> false"
nxt = a.shift
puts "nxt=#{nxt}, a=#{a}"
puts "nxt.begin=#{nxt.begin} > #{curr.end} = curr.end = #{nxt.begin > curr.end}"
if nxt.begin > curr.end
combined << curr
puts "combined << #{curr} = #{combined}"
curr = nxt
puts "curr = nxt = #{curr}"
else
last = [curr, nxt].max_by { |r| [r.end, r.exclude_end? ? 0 : 1] }
puts "last=#{last}, last.exclude_end?=#{last.exclude_end?}"
curr = last.exclude_end? ? (curr.begin...last.end) :
curr.begin..last.end
puts "new value of curr=#{curr}"
end
puts
end
a.empty? #=> false
nxt=5..8, a=[7...9, 9..11]
nxt.begin=5 > 4 = curr.end = true
combined << 1...4 = [1...4]
curr = nxt = 5..8
a.empty? #=> false
nxt=7...9, a=[9..11]
nxt.begin=7 > 8 = curr.end = false
last=7...9, last.exclude_end?=true
new value of curr=5...9
a.empty? #=> false
nxt=9..11, a=[]
nxt.begin=9 > 9 = curr.end = false
last=9..11, last.exclude_end?=false
new value of curr=5..11
a.empty? #=> true, so break [1...4, 5..11]
It is sometimes convenient to be able to return an empty (but valid) range such as 38..37; one should not think of empty ranges as necessarily being an indication that something is amiss.
Alternative solution
If the ranges are all finite, as in the example, and the combined sizes of the ranges is not excessive, one could write the following.
Code
def distill(arr)
arr.flat_map(&:to_a).
uniq.
sort.
chunk_while { |x,y| y == x.next }.
map { |a| a.first..a.last }
end
Examples
distill [5..8, 7...9, 9..11, 1...4, 38..37]
#=> [1..3, 5..11]
distill ['a'..'d', 'c'..'f', 'b'..'g']
# 'a'..'g'
Explanation
The steps for the first example are as follows.
arr = [5..8, 7...9, 9..11, 1...4, 38..37]
a = arr.flat_map(&:to_a)
#=> => [5, 6, 7, 8, 7, 8, 9, 10, 11, 1, 2, 3]
b = a.uniq
#=> [5, 6, 7, 8, 9, 10, 11, 1, 2, 3]
c = b.sort
#=> [1, 2, 3, 5, 6, 7, 8, 9, 10, 11]
d = c.chunk_while { |x,y| y == x.next }
#=> #<Enumerator: #<Enumerator::Generator:0x00005c2683af8dd0>:each>
e = d.map { |a| a.first..a.last }
#=> [1..3, 5..11]
One can convert the enumerator d to an array to see the elements it will generate and pass to chunk_while's block:
d.to_a
#=> [[1, 2, 3], [5, 6, 7, 8, 9, 10, 11]]
See Enumerable#chunk_while. One could alternatively use Enumerable#slice_when.

Keys of a hash whose values sum to a particular value

I have a hash:
a = {"Q1"=>1, "Q2"=>2, "Q5"=>3, "Q8"=>3}
I want to retrieve a set of keys from it such that the sum of their values equals a certain number, for example 5. In such case, the output should be:
Q2 Q5
Please help me on how to get this.
def find_combo(h, tot)
arr = h.to_a
(1..arr.size).find do |n|
enum = arr.combination(n).find { |e| e.map(&:last).sum == tot }
return enum.map(&:first) unless enum.nil?
end
end
h = {"Q1"=>1, "Q2"=>2, "Q5"=>3, "Q8"=>3}
find_combo(h, 5) #=> ["Q2", "Q5"]
find_combo(h, 2) #=> ["Q2"]
find_combo(h, 6) #=> ["Q5", "Q8"]
find_combo(h, 4) #=> ["Q1", "Q5"]
find_combo(h, 8) #=> ["Q2", "Q5", "Q8"]
find_combo(h, 9) #=> ["Q1", "Q2", "Q5", "Q8"]
find_combo(h, 10) #=> nil
Just out of curiosity:
hash = {"Q1"=>1, "Q2"=>2, "Q5"=>3, "Q8"=>3}
arr = hash.to_a
1.upto(hash.size).
lazy.
find do |i|
res = arr.combination(i).find do |h|
h.map(&:last).sum == 5
end
break res if res
end.tap { |result| break result.to_h if result }
#⇒ {"Q2" => 2, "Q5" => 3}

Change position according to facing in Ruby

I need to implement a move method that change position according to facing, position is a [x,y] and I thinking that if move to south is y+1, to north y-1, to east x-1 and to west x+1. this movements are into a matrix.
This is my code. Thank you so much for your help!
# Models the Robot behavior for the game
class Robot
FACINGS = [:south, :east, :north, :west]
def initialize(attr = {})
#position = attr[:position] || [1, 1]
# #move = attr[:move]
#facing_index = facing_index(attr[:facing]) || 0 # south
#facing = facing
# #errors =
end
def position
#position
end
def move
end
def facing
#facing = FACINGS[#facing_index]
end
def errors
end
private
def facing_index(facing)
facing if facing.is_a? Integer
FACINGS.index(facing&.to_sym)
end
end
DIRECTION_NUMBER = { :north=>0, :east=>1, :south=>2, :west=>3 }
#left = { :north=>:west, :west=>:south, :south=>:east, :east=>:north }
#right = #left.invert
#=> {:west=>:north, :south=>:west, :east=>:south, :north=>:east}
def turn_left
#facing = #left[#facing]
end
def turn_right
#facing = #right[#facing]
end
def move(direction)
x, y = #location
#location =
case direction
when :north
[x,y+1]
when :east
[x+1,y]
when :south
[x,y-1]
else
[x-1,y]
end
update_facing(direction)
end
private
def update_facing(direction)
change = (DIRECTION_NUMBER[direction] - DIRECTION_NUMBER[#facing]) % 4
case change
when 1
turn_right
when 2
turn_right; turn_right
when 3
turn_left
end
end
#location = [3, 3]
#facing = :east
move(:south)
#location #=> [3, 2]
#facing #=> :south
move(:north)
#location #=> [3, 3]
#facing #=> :north
move(:west)
#location #=> [2, 3]
#facing #=> :west
move(:east)
#location #=> [3, 3]
#facing #=> :east
Add MOVES which says how to move based on how you're facing.
MOVES = {
north: [0, 1],
south: [0, -1],
east: [1, 0],
west: [-1,0]
}
def move
move = MOVES.fetch(#facing)
#position[0] += move[0]
#position[1] += move[1]
end
MOVES.fetch(#facing) is used instead of MOVES[#facing] so an error will be raised if there is no move for that facing.
You could also do this with a case statement, but this keeps move simple and data driven. You can add more directions like northeast: [1,1]. And if you make this an instance variable, you can customize how individual robots move.
# Define `moves` and `moves=` to get and set `#moves`
attr_accessor :moves
def initialize(attr = {})
...
# Initialize `moves` with either Robot.new(moves: {...})
# or the default MOVES
#moves ||= attr[:moves] || MOVES
...
end
def move
move = moves.fetch(#facing)
#position[0] += move[0]
#position[1] += move[1]
end
FACINGS enum example.
module FACINGS
NORTH = [0, 1]
SOURTH = [0, -1]
EAST = [1, 0]
WEST = [-1,0]
end
class Robot
attr_reader :position
def initialize(attr = {})
#position = attr[:position] || [1, 1]
end
def move(facings)
#position[0] += facings[0]
#position[1] += facings[1]
end
end
r = Robot.new
r.move(FACINGS::NORTH)
r.move(FACINGS::SOURTH)
r.move(FACINGS::WEST)
r.move(FACINGS::EAST)

How to convert a string of values with a range to an array in Ruby

I'm trying to parse a string of numbers and ranges joined by ",", and convert it to a numerical array. I have this as input: "1,3,6-8,5", and would like to have an array like this: [1,3,5,6,7,8].
I can only do it without the range, like this:
"12,2,6".split(",").map { |s| s.to_i }.sort #=> [2, 6, 12]
With a range, I cannot do it:
a = "12,3-5,2,6"
b = a.gsub(/-/, "..") #=> "12,3..5,2,6"
c = b.split(",") #=> ["12", "3..5", "2", "6"]
d = c.sort_by(&:to_i) #=> ["2", "3..5", "6", "12"]
e = d.split(",").map { |s| s.to_i } #>> Error
How can I do this?
I was also thinking to use the splat operator in map, but splat doesn't accept strings like [*(3..5)].
"12,3-5,2,6".
gsub(/(\d+)-(\d+)/) { ($1..$2).to_a.join(',') }.
split(',').
map(&:to_i)
#⇒ [12, 3, 4, 5, 2, 6]
"1,3,6-8,5".split(',').map do |str|
if matched = str.match(/(\d+)\-(\d+)/)
(matched[1].to_i..matched[2].to_i).to_a
else
str.to_i
end
end.flatten
or
"1,3,6-8,5".split(',').each_with_object([]) do |str, output|
if matched = str.match(/(\d+)\-(\d+)/)
output.concat (matched[1].to_i..matched[2].to_i).to_a
else
output << str.to_i
end
end
or strict
RANGE_PATTERN = /\A(\d+)\-(\d+)\z/
INT_PATTERN = /\A\d+\z/
"1,3,6-8,5".split(',').each_with_object([]) do |str, output|
if matched = str.match(RANGE_PATTERN)
output.concat (matched[1].to_i..matched[2].to_i).to_a
elsif str.match(INT_PATTERN)
output << str.to_i
else
raise 'Wrong format given'
end
end
"1,3,6-8,5".split(',').flat_map do |s|
if s.include?('-')
f,l = s.split('-').map(&:to_i)
(f..l).to_a
else
s.to_i
end
end.sort
#=> [1, 3, 5, 6, 7, 8]
"1,3,6-8,5"
.scan(/(\d+)\-(\d+)|(\d+)/)
.flat_map{|low, high, num| num&.to_i || (low.to_i..high.to_i).to_a}
#=> [1, 3, 6, 7, 8, 5]

Problem with alpha-beta pruning method, returns beta? maybe I don't understand how this works

Shouldn't it return a DRAW?
def alphabeta(alpha, beta, player)
best_score = -INFINITY
if not self.has_available_moves?
return DRAW
elsif self.has_this_player_won?(player) == player
return WIN
elsif self.has_this_player_won?(1 - player) == 1 - player
return LOSS
else
self.remaining_moves.each do |move|
if alpha >= beta then return alpha end
self.make_move_with_index(move, player)
move_score = -alphabeta(-beta, -alpha, 1 - player)
self.undo_move(move)
if move_score > alpha
alpha = move_score
next_move = move
end
best_score = alpha
end
end
return best_score
end
constants:
WIN = 1
LOSS = -1
DRAW = 0
INFINITY = 100
COMPUTER = 0
HUMAN = 1
test case:
# computer is 0, human is 1
# c h c
# _ h h
# _ c h -- computer's turn
test "make sure alpha-beta pruning works (human went first) 1" do
#board.state = [0,1,0,nil,1,1,nil,0,1]
score = #board.alphabeta(100, -100, Board::COMPUTER)
assert_equal(Board::DRAW, score)
end
relevant methods and other things to help read the code above:
self.state = Array.new(9)
WAYS_TO_WIN = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8],[0, 4, 8], [2, 4, 6]]
def make_move_with_index(index, player)
self.state[index] = player
end
def undo_move(index)
self.state[index] = nil
end
def has_this_player_won?(player)
WAYS_TO_WIN.each do |way_to_win|
return true if self.state.values_at(*way_to_win).uniq.size == 1 and self.state[way_to_win[0]] == player
end
return false
end
def remaining_moves
self.state.each_with_index.map{|e,i| (e.nil?) ? i : nil }.compact
end
def has_available_moves?
return self.state.include? nil
end
You ignored my comment in a previous question. has_this_player_won? returns a boolean value which can never be equal to an integer player. Besides, your logic at the beginning is wrong: the game can have a winner even if there are no more moves left. And finally, the first call to the recursive function should be made with alpha=-inf, beta=+inf. The relevant sections of code:
if self.has_this_player_won?(1 - player)
return LOSS
elsif not self.has_available_moves?
return DRAW
else
...
score = #board.alphabeta(-INFINITY, INFINITY, Board::COMPUTER)

Resources