Skip to content

Instantly share code, notes, and snippets.

@newtonapple
Last active June 22, 2021 21:26
Show Gist options
  • Save newtonapple/060060775b63fc6c375578105a4bb7df to your computer and use it in GitHub Desktop.
Save newtonapple/060060775b63fc6c375578105a4bb7df to your computer and use it in GitHub Desktop.
Ruby Interleaver
# frozen_string_literal: true
require "zlib"
require 'set'
class InterleaverArray
TEAM_A_NAME = "A"
TEAM_B_NAME = "B"
attr_accessor :a, :b, :a_index, :b_index, :a_size, :b_size
def initialize(a, b, random_seed_phrase)
@a = a
@b = b
@random_seed_phrase = random_seed_phrase
reset!
end
def reset!
@size = 0
@a_index = 0
@b_index = 0
@a_size = 0
@b_size = 0
@max_size = [@a.size, @b.size].min
@random = new_random
@drafted = Set.new
end
def each
while @size < @max_size do
if (a_size < b_size) || (a_size == b_size && team_a?)
yield draft!(:a), TEAM_A_NAME
else
yield draft!(:b), TEAM_B_NAME
end
end
end
def draft!(team_name)
team = send(team_name)
idx_meth = "#{team_name}_index"
idx_set_method = "#{team_name}_index="
idx = send(idx_meth)
item = team[idx]
while item && @drafted.member?(item)
idx = send(idx_set_method, idx + 1)
item = team[idx]
end
@drafted << item
send(idx_set_method, idx + 1)
team_size = send("#{team_name}_size")
send("#{team_name}_size=", team_size + 1)
@size += 1
item
end
def new_random
Random.new(Zlib.crc32(@random_seed_phrase))
end
def team_a?
@random.rand(2).zero?
end
end
def test(a, b)
i = InterleaverArray.new(a, b, 'hello')
puts("A: #{a}, B: #{b}")
puts("#{i.to_enum.to_a}\n\n")
end
test([1, 2, 3], [2, 4, 3])
test([1, 2, 3], [1, 2, 3])
test([1, 2], [5, 4, 3, 7])
test([1, 2, 0, 10], [5, 4])
test([1, 50, 2, 0, 10, 100], [100, 0, 10, 50, 5, 4])
puts("equal size random 10x: ")
(1..10).each do |i|
test(i.times.map{rand(10)}, i.times.map{rand(10)})
end
puts("unequal size random 20x: ")
(1..10).each do |i|
test((i+rand(i)).times.map{rand(10)}, i.times.map{rand(10)})
test(i.times.map{rand(10)}, (i+rand(i)).times.map{rand(10)})
end
## sample run
# ruby interleaver_array.rb
# A: [1, 2, 3], B: [2, 4, 3]
# [[1, "A"], [2, "B"], [4, "B"]]
# A: [1, 2, 3], B: [1, 2, 3]
# [[1, "A"], [2, "B"], [3, "B"]]
# A: [1, 2], B: [5, 4, 3, 7]
# [[1, "A"], [5, "B"]]
# A: [1, 2, 0, 10], B: [5, 4]
# [[1, "A"], [5, "B"]]
# A: [1, 50, 2, 0, 10, 100], B: [100, 0, 10, 50, 5, 4]
# [[1, "A"], [100, "B"], [0, "B"], [50, "A"], [10, "B"], [2, "A"]]
# equal size random 10x:
# A: [4], B: [5]
# [[4, "A"]]
# A: [9, 0], B: [1, 2]
# [[9, "A"], [1, "B"]]
# A: [8, 0, 6], B: [9, 0, 0]
# [[8, "A"], [9, "B"], [0, "B"]]
# A: [2, 9, 1, 2], B: [4, 0, 9, 0]
# [[2, "A"], [4, "B"], [0, "B"], [9, "A"]]
# A: [3, 7, 2, 6, 7], B: [3, 2, 1, 2, 4]
# [[3, "A"], [2, "B"], [1, "B"], [7, "A"], [4, "B"]]
# A: [0, 1, 5, 1, 7, 4], B: [3, 8, 3, 8, 2, 5]
# [[0, "A"], [3, "B"], [8, "B"], [1, "A"], [2, "B"], [5, "A"]]
# A: [6, 9, 5, 4, 5, 1, 1], B: [6, 0, 8, 8, 5, 9, 0]
# [[6, "A"], [0, "B"], [8, "B"], [9, "A"], [5, "B"], [4, "A"], [nil, "B"]]
# A: [2, 1, 9, 0, 4, 2, 8, 5], B: [2, 5, 7, 4, 8, 1, 4, 9]
# [[2, "A"], [5, "B"], [7, "B"], [1, "A"], [4, "B"], [9, "A"], [8, "B"], [0, "A"]]
# A: [3, 1, 8, 0, 8, 7, 8, 9, 4], B: [9, 6, 3, 3, 1, 8, 4, 8, 7]
# [[3, "A"], [9, "B"], [6, "B"], [1, "A"], [8, "B"], [0, "A"], [4, "B"], [7, "A"], [nil, "B"]]
# A: [5, 6, 3, 7, 4, 1, 9, 1, 9, 0], B: [6, 8, 9, 1, 6, 2, 5, 1, 0, 3]
# [[5, "A"], [6, "B"], [8, "B"], [3, "A"], [9, "B"], [7, "A"], [1, "B"], [4, "A"], [2, "B"], [0, "A"]]
# unequal size random 10x:
# A: [7], B: [0]
# [[7, "A"]]
# A: [4], B: [8]
# [[4, "A"]]
# A: [8, 4], B: [8, 4]
# [[8, "A"], [4, "B"]]
# A: [3, 9], B: [4, 5]
# [[3, "A"], [4, "B"]]
# A: [4, 0, 0], B: [4, 5, 6]
# [[4, "A"], [5, "B"], [6, "B"]]
# A: [9, 4, 1], B: [1, 5, 3]
# [[9, "A"], [1, "B"], [5, "B"]]
# A: [7, 1, 3, 9, 7], B: [2, 1, 0, 5]
# [[7, "A"], [2, "B"], [1, "B"], [3, "A"]]
# A: [8, 1, 3, 6], B: [1, 0, 4, 7, 5]
# [[8, "A"], [1, "B"], [0, "B"], [3, "A"]]
# A: [7, 0, 4, 7, 0, 5, 3, 2], B: [0, 1, 0, 6, 4]
# [[7, "A"], [0, "B"], [1, "B"], [4, "A"], [6, "B"]]
# A: [6, 4, 7, 3, 0], B: [3, 9, 5, 3, 9, 5, 1]
# [[6, "A"], [3, "B"], [9, "B"], [4, "A"], [5, "B"]]
# A: [8, 7, 4, 0, 5, 4, 4, 3, 4, 5, 9], B: [3, 2, 7, 8, 7, 6]
# [[8, "A"], [3, "B"], [2, "B"], [7, "A"], [6, "B"], [4, "A"]]
# A: [8, 5, 2, 6, 7, 3], B: [5, 2, 1, 3, 7, 0]
# [[8, "A"], [5, "B"], [2, "B"], [6, "A"], [1, "B"], [7, "A"]]
# A: [4, 4, 7, 5, 2, 3, 8, 5, 7, 0, 1, 5, 7], B: [6, 8, 6, 9, 3, 1, 7]
# [[4, "A"], [6, "B"], [8, "B"], [7, "A"], [9, "B"], [5, "A"], [3, "B"]]
# A: [8, 8, 1, 4, 3, 8, 9], B: [1, 0, 8, 4, 5, 2, 3]
# [[8, "A"], [1, "B"], [0, "B"], [4, "A"], [5, "B"], [3, "A"], [2, "B"]]
# A: [3, 0, 1, 5, 0, 5, 9, 6, 2, 5, 7], B: [9, 3, 8, 8, 4, 1, 3, 8]
# [[3, "A"], [9, "B"], [8, "B"], [0, "A"], [4, "B"], [1, "A"], [nil, "B"], [5, "A"]]
# A: [3, 6, 6, 3, 3, 6, 7, 1], B: [4, 4, 5, 2, 7, 4, 5, 2, 7, 0, 1, 6, 5, 3, 2]
# [[3, "A"], [4, "B"], [5, "B"], [6, "A"], [2, "B"], [7, "A"], [0, "B"], [1, "A"]]
# A: [9, 9, 1, 3, 3, 4, 6, 9, 3, 1, 7, 7, 5, 7, 8, 6, 7], B: [6, 9, 2, 2, 1, 1, 0, 9, 1]
# [[9, "A"], [6, "B"], [2, "B"], [1, "A"], [0, "B"], [3, "A"], [nil, "B"], [4, "A"], [nil, "B"]]
# A: [2, 3, 6, 6, 9, 5, 2, 3, 9], B: [5, 8, 9, 3, 3, 2, 4, 7, 3, 8, 2, 3, 2, 6, 6, 4, 2]
# [[2, "A"], [5, "B"], [8, "B"], [3, "A"], [9, "B"], [6, "A"], [4, "B"], [nil, "A"], [7, "B"]]
# A: [3, 7, 7, 5, 5, 5, 1, 2, 3, 8, 6, 4, 3, 8, 8, 2, 7], B: [1, 5, 1, 0, 3, 6, 3, 1, 8, 3]
# [[3, "A"], [1, "B"], [5, "B"], [7, "A"], [0, "B"], [2, "A"], [6, "B"], [8, "A"], [nil, "B"], [4, "A"]]
# A: [6, 5, 5, 4, 6, 7, 2, 3, 1, 4], B: [2, 4, 5, 7, 2, 1, 4, 7, 6, 1, 9, 6, 7, 3, 1, 4, 6]
# [[6, "A"], [2, "B"], [4, "B"], [5, "A"], [7, "B"], [3, "A"], [1, "B"], [nil, "A"], [9, "B"], [nil, "A"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment