GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python_reference/useful_scripts/combinations.py at master · devbib/python_reference

Expand file tree

/

combinations.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
executable file
·
71 lines (46 loc) · 2.11 KB
/

combinations.py

Copy path

File metadata and controls

executable file
·
71 lines (46 loc) · 2.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# Sebastian Raschka 2014
# Functions to calculate factorial, combinations, and permutations
# bundled in an simple command line interface.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
def combinations(n, r):
numerator = factorial(n)
denominator = factorial(r) * factorial(n-r)
return int(numerator/denominator)
def permutations(n, r):
numerator = factorial(n)
denominator = factorial(n-r)
return int(numerator/denominator)
assert(factorial(3) == 6)
assert(combinations(20, 8) == 125970)
assert(permutations(30, 3) == 24360)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description='Script to calculate the number of combinations or permutations ("n choose r")',
formatter_class=argparse.RawTextHelpFormatter,
prog='Combinations',
epilog='Example: ./combinations.py -c 20 3'
)
parser.add_argument('-c', '--combinations', type=int, metavar='NUMBER', nargs=2,
help='Combinations: Number of ways to combine n items with sequence length r where the item order does not matter.')
parser.add_argument('-p', '--permutations', type=int, metavar='NUMBER', nargs=2,
help='Permutations: Number of ways to combine n items with sequence length r where the item order does not matter.')
parser.add_argument('-f', '--factorial', type=int, metavar='NUMBER', help='n! e.g., 5! = 5*4*3*2*1 = 120.')
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
args = parser.parse_args()
if not any((args.combinations, args.permutations, args.factorial)):
parser.print_help()
quit()
if args.factorial:
print(factorial(args.factorial))
if args.combinations:
print(combinations(args.combinations[0], args.combinations[1]))
if args.permutations:
print(permutations(args.permutations[0], args.permutations[1]))
if args.factorial:
print(factorial(args.factorial))
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.