Last active
January 5, 2024 04:39
-
-
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
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
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