1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
|
|
# Copyright 2014-2018 by Christopher C. Little. |
4
|
|
|
# This file is part of Abydos. |
5
|
|
|
# |
6
|
|
|
# Abydos is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# Abydos is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with Abydos. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
1 |
|
"""abydos.util._prod. |
20
|
|
|
|
21
|
|
|
The util._prod module defines prod, which computes the product of a collection |
22
|
|
|
of numbers (akin to sum, but for product). |
23
|
|
|
""" |
24
|
|
|
|
25
|
1 |
|
from __future__ import unicode_literals |
26
|
|
|
|
27
|
1 |
|
from operator import mul |
28
|
|
|
|
29
|
1 |
|
from six.moves import reduce |
30
|
|
|
|
31
|
1 |
|
__all__ = ['prod'] |
32
|
|
|
|
33
|
|
|
|
34
|
1 |
|
def prod(nums): |
35
|
|
|
"""Return the product of nums. |
36
|
|
|
|
37
|
|
|
The product is Π(nums). |
38
|
|
|
|
39
|
|
|
Cf. https://en.wikipedia.org/wiki/Product_(mathematics) |
40
|
|
|
|
41
|
|
|
:param nums: a collection (list, tuple, set, etc.) of numbers |
42
|
|
|
:returns: the product of a nums |
43
|
|
|
:rtype: numeric |
44
|
|
|
|
45
|
|
|
>>> prod([1,1,1,1]) |
46
|
|
|
1 |
47
|
|
|
>>> prod((2,4,8)) |
48
|
|
|
64 |
49
|
|
|
>>> prod({1,2,3,4}) |
50
|
|
|
24 |
51
|
|
|
>>> prod(2**i for i in range(5)) |
52
|
|
|
1024 |
53
|
|
|
""" |
54
|
1 |
|
return reduce(mul, nums, 1) |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
if __name__ == '__main__': |
58
|
|
|
import doctest |
59
|
|
|
|
60
|
|
|
doctest.testmod() |
61
|
|
|
|