【每日一题20220523】单利复利

:mage:‍ 在经济生活中,贷款的简单利息计算方法很简单,就是将初始金额(本金,p)乘以利率(r)和时间周期(n)。复利的计算方法是将每个期限后的利息加上所欠金额,然后根据本金加上以前所有期限的利息计算下一次支付的利息。 给定一个本金 p,利率 r,以及若干个周期 n,返回一个数组[单利的总额,复利的总额]
备注:

  1. 所有结果四舍五入到最接近的整数 2. 本金总是0到9999之间的整数 3. 利率是0到1之间的小数 4. 时间段的数量是0到50之间的整数

【示例】
输入:p=100, r=0.1, n=2
输出:[120, 121]
解释:本金100元,利率0.1,经过2年后,单利总额是120元,复利总额是121元

题目难度:简单
题目来源:codewars-Simple Interest and Compound Interest

def solution(p: int, r: float, n: int)-> list:
    # your code here

assert solution(100, 0.1, 1) == [110, 110]
assert solution(100, 0.1, 2) == [120, 121]
assert solution(100, 0.1, 10) == [200, 259]

最后应该返回一个列表而不是bool吧

def solution(p: int, r: float, n: int) -> list:
    # your code here
    return [round(p * (1 + r * n)), round(p * ((1 + r) ** n))]


assert solution(100, 0.1, 1) == [110, 110]
assert solution(100, 0.1, 2) == [120, 121]
assert solution(100, 0.1, 10) == [200, 259]
def solution(p: int, r: float, n: int)-> bool:
    # your code here
    num1 = p * r * n + p
    num2 = p
    for i in range(1,n+1):
        num2 = num2 * r + num2
    return [round(num1),round(num2)]

assert solution(100, 0.1, 1) == [110, 110]
assert solution(100, 0.1, 2) == [120, 121]
assert solution(100, 0.1, 10) == [200, 259]
def solution(p: int, r: float, n: int)-> list:
    # your code here
    if 0 <= p <= 9999 and 0 < r < 1 and 0 <= n <= 50:
        return [int(p*(1+r*n)),int(p*((1+r)**n))]
    else:
        return -1

assert solution(100, 0.1, 1) == [110, 110]
assert solution(100, 0.1, 2) == [120, 121]
assert solution(100, 0.1, 10) == [200, 259]
def solution(p: int, r: float, n: int)-> list:
    return [round(p+p*r*n),round(p*pow((1+r),n))]```
def solution(p: int, r: float, n: int)-> list:
    return [int(i) for i in map(lambda x:Decimal(x).quantize(Decimal('0'),rounding=ROUND_HALF_UP),[p+(p*r*n),p*(1+r)**n])]
def solution(p: int, r: float, n: int)-> list:
    a=round(p*(1+r*n))
    b=round((1+r)**n*p)
    return [a,b]

assert solution(100, 0.1, 1) == [110, 110]
assert solution(100, 0.1, 2) == [120, 121]
assert solution(100, 0.1, 10) == [200, 259]
def solution(p: int, r: float, n: int)-> list:
    # your code here
    return [round(p * (1 + r * n)), round(p * (1 + r) ** n)]


assert solution(100, 0.1, 1) == [110, 110]
assert solution(100, 0.1, 2) == [120, 121]
assert solution(100, 0.1, 10) == [200, 259]
def solution(p: int, r: float, n: int)-> list:
    return [round(p*(r*n+1)),round(p*(r+1)**n)]

assert solution(100, 0.1, 1) == [110, 110]
assert solution(100, 0.1, 2) == [120, 121]
assert solution(100, 0.1, 10) == [200, 259]