LearnFrenzy
Sign in Take a free test

Modules and Packages · Modules and Packages Coding Problems · Question 5

Match fixtures and team pairings with itertools in Python Solution, output & explanation

  • Program
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

For four teams, list every match of a single round-robin with combinations, count home-and-away fixtures with permutations, and the number of 2-member squads from 6 players.

Solution
main.py Run
from itertools import combinations, permutations
import math

teams = ["CSK", "MI", "RCB", "KKR"]
matches = list(combinations(teams, 2))
for home, away in matches:
    print(f"{home} vs {away}")
print(len(matches), "matches;", len(list(permutations(teams, 2))), "home-and-away fixtures")
print(math.comb(6, 2), "possible 2-member squads from 6 players")

Output Verified by running it on Python 3

CSK vs MI
CSK vs RCB
CSK vs KKR
MI vs RCB
MI vs KKR
RCB vs KKR
6 matches; 12 home-and-away fixtures
15 possible 2-member squads from 6 players

combinations ignores order (each pair once); permutations counts both orders; math.comb computes the count without listing.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links