forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.py
More file actions
41 lines (28 loc) · 1.02 KB
/
misc.py
File metadata and controls
41 lines (28 loc) · 1.02 KB
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
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
import math
from python_toolbox import misc_tools
from python_toolbox import math_tools
from python_toolbox import cute_iter_tools
infinity = float('inf')
class MISSING_ELEMENT(misc_tools.NonInstantiable):
'''A placeholder for a missing element used in internal calculations.'''
def get_short_factorial_string(number, *, minus_one=False):
'''
Get a short description of the factorial of `number`.
If the number is long, just uses factorial notation.
Examples:
>>> get_short_factorial_string(4)
'24'
>>> get_short_factorial_string(14)
'14!'
'''
assert number >= 0 and \
isinstance(number, math_tools.PossiblyInfiniteIntegral)
if number == infinity:
return "float('inf')"
elif number <= 10:
return str(math.factorial(number) - int(minus_one))
else:
assert number > 10
return '%s!%s' % (number, ' - 1' if minus_one else '')