How to resolve the algorithm Round-robin tournament schedule step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Round-robin tournament schedule step by step in the Ruby programming language

Table of Contents

Problem Statement

A round-robin tournament is also known as an all-play-all-tournament; each participant plays every other participant once. For N participants the number of rounds is N-1 if N is an even number. When there are an odd number of participants then each round one contestor has no opponent (AKA as a "bye"). The number of rounds is N in that case. Write a program that prints out a tournament schedule for 12 participants (represented by numbers 1 to 12).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Round-robin tournament schedule step by step in the Ruby programming language

Goal of the Code: This Ruby code implements a tournament scheduling algorithm known as round-robin for an even or odd number of players.

Function Definition:

def round_robin(n)

Creating the List of Players:

 rotating_players = (2..n).map(&:to_s)

This creates an array of strings representing players 2 through n.

Adding a Bye for Odd Number of Players:

 rotating_players << "bye" if n.odd?

If there are an odd number of players, a "bye" is added to the list to ensure an even number of players in each round.

Creating the Round Schedule:

 Array.new(rotating_players.size) do |r|
   all = ["1"] + rotating_players.rotate(-r)
   [all[0, all.size/2], all[all.size/2..].reverse]
 end

This iterates over the number of rounds and creates a list of round schedules as follows:

  1. It prepends player 1 (represented by "1") to the rotated list of players.
  2. It splits the players into two equal halves: The first half is the home team, and the second half (reversed) is the away team.

Printing the Round Schedule:

round_robin(12).each.with_index(1) do |round, i|
 puts "Round #{i}"
 round.each do |players|
   puts players.map{|player| player.ljust(4)}.join
 end
 puts
end

This iterates over the round schedules and prints the round number, followed by the home team and away team players, left-justified with 4 spaces.

Example: For 12 players (n = 12), the output will be:

Round 1
1  2  3  4  5  6
7  8  9  10 11 bye

Round 2
1  bye 7  8  9  10
11 6  5  4  3  2

Round 3
1  bye 11 6  5  4
3  2  10 9  8  7

Round 4
1  bye 3  2  10 9
8  7  6  5  4  11

Round 5
1  bye 8  7  6  5
4  11 10 9  2  3

Round 6
1  bye 4  11 10 9
2  3  8  7  6  5

Source code in the ruby programming language

def round_robin( n )
  rotating_players = (2..n).map(&:to_s) #player 1 to be added later
  rotating_players << "bye" if n.odd? 
  Array.new(rotating_players.size) do |r|
    all = ["1"] + rotating_players.rotate(-r)
    [all[0, all.size/2], all[all.size/2..].reverse]
  end
end

round_robin(12).each.with_index(1) do |round, i|
  puts "Round #{i}"
  round.each do |players|
    puts players.map{|player| player.ljust(4)}.join
  end
  puts
end


  

You may also check:How to resolve the algorithm Summarize primes step by step in the Wren programming language
You may also check:How to resolve the algorithm Harmonic series step by step in the RPL programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the APL programming language
You may also check:How to resolve the algorithm Combinations step by step in the AWK programming language
You may also check:How to resolve the algorithm Cuban primes step by step in the Common Lisp programming language