Skip to content

Instantly share code, notes, and snippets.

@ankurp
Last active January 5, 2024 04:39
Show Gist options
  • Save ankurp/19a1977fdf5c1993e6a9006c26382b1a to your computer and use it in GitHub Desktop.
Save ankurp/19a1977fdf5c1993e6a9006c26382b1a to your computer and use it in GitHub Desktop.
Code to introduce even number of participants to each other every other week
people = %w[John Mary Bob Peter Susan Paula]
# Works only for even number of participants
if people.size % 2 == 0
half_index = (people.size / 2) - 1
first_person = people.shift
rotating_people = people[0...half_index] + people[half_index..].reverse
(0..people.size - 1).each do |i|
first_half = [first_person, *rotating_people[0...half_index]]
second_half = rotating_people[half_index..].reverse
puts "--------Week #{i + 1}---------"
puts first_half.zip(second_half).map { |a, b| [a, b].join("<->") }
rotating_people.rotate!(-1)
puts ""
end
end
# Output:
# --------Week 1---------
# John<->Peter
# Mary<->Susan
# Bob<->Paula
# --------Week 2---------
# John<->Susan
# Peter<->Paula
# Mary<->Bob
# --------Week 3---------
# John<->Paula
# Susan<->Bob
# Peter<->Mary
# --------Week 4---------
# John<->Bob
# Paula<->Mary
# Susan<->Peter
# --------Week 5---------
# John<->Mary
# Bob<->Peter
# Paula<->Susan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment