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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Round-robin tournament schedule step by step in the Nim 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 Nim programming language

Source code in the nim programming language

import std/[algorithm, sequtils, strformat]

proc roundRobin(n: Positive) =
  assert n >= 2
  var n = n
  var list1 = toSeq(2..n)
  if n mod 2 == 1:
    list1.add 0  # 0 denotes a "bye".
    inc n
  for r in 1..<n:
    stdout.write &"Round {r:2}:"
    let list2 = 1 & list1
    for i in 0..<(n div 2):
      stdout.write &" ({list2[i]:>2} vs {list2[n - i - 1]:<2})"
    echo()
    list1.rotateLeft(-1)

echo "Round robin for 12 players:\n"
roundRobin(12)
echo "\n\nRound robin for 5 players (0 denotes a bye) :\n"
roundRobin(5)


  

You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Scala programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the LotusScript programming language
You may also check:How to resolve the algorithm Discordian date step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Brace expansion step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Equilibrium index step by step in the NetRexx programming language