find_an()
looks like an internal function, used by arithmetic_partial_sum()
. If it is not for external use, it should be named with a leading underscore, to suggest it is private.
arithmetic_partial_sum()
appears to handle only integer values, yet it return a floating-point value (1275.0
in the built-in example). It should return an integer, since it is adding up integers. Use the Python3.x integer-division operator: //
.
n = 1 + (an - a1) // d
S = n * (a1 + an) // 2
Or, not assume the terms are integer, and use float(...)
instead.
geometric_partial_sum()
fails if the first 2 values are not int
values:
>>> geometric_partial_sum(f"1+{1/3}+{1/9}+{1/27}+{1/81}")
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
geometric_partial_sum(f"1+{1/3}+{1/9}+{1/27}+{1/81}")
File "...\partial_sum.py", line 63, in geometric_partial_sum
r = int(series[1]) / a1
ValueError: invalid literal for int() with base 10: '0.3333333333333333'
You should convert the terms to floating-point values, not integers:
a1 = float(series[0])
r = float(series[1]) / a1
find_an()
assumes \$a_n\$ is immediately after the '.'
term, so will fail with:
arithmetic_partial_sum("3+7+11+15+.+97+99")
arithmetic_partial_sum("3+7+11+15+17")
Why not just retrieve the last term?
def find_an(parsed_series):
return int(parsed_series[-1])
Now the following all succeed and return the correct values
arithmetic_partial_sum("3+7+11+15+.+97+99")
arithmetic_partial_sum("3+7+11+15+...+97+99")
arithmetic_partial_sum("3+7+11+15+17")
The """docstrings"""
for arithmetic_partial_sum()
and geometric_partial_sum()
appear unhelpful. For example:
>>> help(arithmetic_partial_sum)
Help on function arithmetic_partial_sum in module __main__:
arithmetic_partial_sum(series)
Returns the partial sum of an arithmetic series
Formula:
S = n( (a1 + an) / 2 )
Find an:
an = a1 + (n - 1)d
Find n:
n = 1 + ( (an - a1) / d )
:param series: Arithmetic series to solve
The function is not returning an
or n
. Even the formula is not particularly helpful. """docstrings"""
should tell a user how to use the function. For example (adding Python 3.6 type hints as well):
def arithmetic_partial_sum(series:str) -> int:
"""
Returns the sum of an arithmetic series
Example:
s = arithmetic_partial_sum("1+3+5+.+99") # returns 2500
:param series: A string representing the arithmetic series to solve
"""
Now type help(arithmetic_partial_sum)
:
>>> help(arithmetic_partial_sum)
Help on function arithmetic_partial_sum in module __main__:
arithmetic_partial_sum(series: str) -> int
Returns the sum of an arithmetic series
Example:
s = arithmetic_partial_sum("1+3+5+.+99") # returns 2500
:param series: A string representing the arithmetic series to solve
The user is told the function takes a string and returns an integer. The format of the string should be clear from the example.