Last active
August 29, 2015 13:56
-
-
Save tmiller/3b9036970024d67cf9c1 to your computer and use it in GitHub Desktop.
Pairing Algorithm
This file contains 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 characters
require 'pp' | |
devs = %w{ Andrew Anthony Ben Matt Tom Zac } | |
devs << nil if devs.length.odd? | |
top, bottom = devs.each_slice(devs.length/2).to_a | |
pivot, *top = top | |
result = [] | |
(devs.length-1).times do | |
top.unshift(bottom.shift) | |
bottom.push(top.pop) | |
result << [pivot,*top].zip(bottom) | |
end | |
pp result | |
# [[["Andrew", "Tom"], ["Matt", "Zac"], ["Anthony", "Ben"]], | |
# [["Andrew", "Zac"], ["Tom", "Ben"], ["Matt", "Anthony"]], | |
# [["Andrew", "Ben"], ["Zac", "Anthony"], ["Tom", "Matt"]], | |
# [["Andrew", "Anthony"], ["Ben", "Matt"], ["Zac", "Tom"]], | |
# [["Andrew", "Matt"], ["Anthony", "Tom"], ["Ben", "Zac"]]] | |
# ******************************************************************** | |
# Smaller less readable version | |
require 'pp' | |
devs = %w{ Andrew Anthony Ben Matt Tom Zac } | |
devs << nil if devs.length.odd? | |
pivot, *rest = devs | |
result = [] | |
(rest.length).times do | |
bottom, top = rest.rotate!.each_slice(devs.length / 2).to_a | |
result << [pivot,*top].zip(bottom.reverse) | |
end | |
pp result | |
# [[["Andrew", "Tom"], ["Zac", "Matt"], ["Anthony", "Ben"]], | |
# [["Andrew", "Zac"], ["Anthony", "Tom"], ["Ben", "Matt"]], | |
# [["Andrew", "Anthony"], ["Ben", "Zac"], ["Matt", "Tom"]], | |
# [["Andrew", "Ben"], ["Matt", "Anthony"], ["Tom", "Zac"]], | |
# [["Andrew", "Matt"], ["Tom", "Ben"], ["Zac", "Anthony"]]] | |
# ******************************************************************** | |
# Here is an approach without manipulating existing arrays | |
# That uses math instead of Queues. This would be the memory | |
# optimized version. | |
require 'pp' | |
devs = %w{ Andrew Anthony Ben Matt Tom Zac } | |
index = Array.new devs.length | |
i = 0 | |
x = 1 | |
while i < devs.length | |
index[i] = x - 1 | |
index[i+1] = devs.length - x | |
i += 2 | |
x += 1 | |
end | |
result = Array.new(devs.length - 1) { Array.new(devs.length/2) } | |
result.each do |row| | |
i = j = 0 | |
while j < devs.length | |
row[i] = [ devs[index[j]], devs[index[j+1]] ] | |
i += 1 | |
j += 2 | |
end | |
(devs.length - 1).times do |i| | |
index[i+1] = (index[i+1] + 1) % (devs.length - 1) + 1 | |
end | |
end | |
pp result | |
# [[["Andrew", "Zac"], ["Anthony", "Tom"], ["Ben", "Matt"]], | |
# [["Andrew", "Ben"], ["Matt", "Anthony"], ["Tom", "Zac"]], | |
# [["Andrew", "Tom"], ["Zac", "Matt"], ["Anthony", "Ben"]], | |
# [["Andrew", "Anthony"], ["Ben", "Zac"], ["Matt", "Tom"]], | |
# [["Andrew", "Matt"], ["Tom", "Ben"], ["Zac", "Anthony"]]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm having way tooooooo much fun with this.