**Adventures in Python: Combinations and Permutations**

This program offers the user a choice of one of three calculations:

1. Combinations, no
repeats

2. Permutations

3. Combinations, with
repeats

The program demonstrates how a menu is made. Equality is tested with two equal signs
(==). A single equal sign (=) represents
assignment for the Python.

The math.factorial function uses integers.

# Program 005: Combinations, Permutations,
Repeated Combination

# Demonstration of the If structure

import math

# factorial is only
for integers

# choices

# we will need to
format the choice variable as an integer (number)

print("Make
your choice:")

print("1. Combination")

print("2. Permutation")

print("3. Repeated Combination")

choice =
int(input("Choice: "))

# error condition,
also input routine

if choice < 1 or
choice > 3:

print("Not a valid choice")

else:

n = int(input("n = "))

r = int(input("r = "))

nf = math.factorial(n)

rf = math.factorial(r)

df = math.factorial(n-r)

# calculation
test. Equality is tested using 2 equal
signs

if choice == 1:

calc = nf/(df*rf)

print("Combinations = ",calc)

if choice == 2:

calc = nf/df

print("Permutations = ",calc)

if choice == 3:

calc =
math.factorial(n+r-1)/(rf*math.factorial(n-1))

print("Combinations = ",calc)

Example: n = 52, r = 5

Make your
choice:

1. Combination

2. Permutation

3. Repeated Combination

Choice: 1

n = 52

r = 5

Combinations
= 2598960.0

>>>

Make your
choice:

1. Combination

2. Permutation

3. Repeated Combination

Choice: 2

n = 52

r = 5

Permutations
= 311875200.0

>>>

Make your
choice:

1. Combination

2. Permutation

3. Repeated Combination

Choice: 3

n = 52

r = 5

Combinations
= 3819816.0

Eddie

This blog is property of Edward Shore, 2017.

## No comments:

## Post a Comment