Mercurial > hg > config
annotate python/example/factorial.py @ 820:737b9eda7f77
[git submodules] logging stub
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 31 Jan 2017 14:31:03 -0800 |
parents | 47a434dd5068 |
children |
rev | line source |
---|---|
784
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
2 """how many 0s are in a factorial""" |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
3 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
4 import argparse |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
5 import sys |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
6 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
7 def factorial(N): |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
8 """factorial of `N`""" |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
9 if N == 1: |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
10 return 1 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
11 return reduce(int.__mul__, range(2, N+1)) |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
12 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
13 def factorial_zeros(N): |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
14 """how many 0s are in a factorial?""" |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
15 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
16 return N/5 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
17 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
18 def main(args=sys.argv[1:]): |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
19 """CLI""" |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
20 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
21 parser = argparse.ArgumentParser(description=__doc__) |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
22 parser.add_argument('N', type=int, nargs='+') |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
23 options = parser.parse_args(args) |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
24 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
25 # sanity |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
26 if any([i < 1 for i in options.N]): |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
27 parser.error("Input values must be >= 1") |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
28 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
29 for i in options.N: |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
30 f = factorial(i) |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
31 print f |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
32 |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
33 if __name__ == '__main__': |
47a434dd5068
example: factorial using reduce
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
34 main() |