s4ros icon

steps.py

s4ros | PRO | 12/01/19 04:45:46 PM UTC | 0 ⭐ | 451 👁️ | Never ⏰ | []
Python |

1.03 KB

|

None

|

0 👍

/

0 👎

# https://challengerocket.com/step-counting
 
class App():
    def factorial(self, n):
        result = 1
        while n > 1:
            result *= n
            n -= 1
        return result
 
    def newton(self, n, k):
        return self.factorial(n)/(self.factorial(k)*(self.factorial(n-k)))
 
    def newtons_count(self, steps):
        return ((steps//2) - 1) if steps > 3 else 0
 
    def solve(self, steps):
        result = steps
        k = self.newtons_count(steps)
        if k:
            newton_n = steps - 2
            newton_k = 2
            for i in range(k):
                result += self.newton(newton_n, newton_k)
                newton_n -= 1
                newton_k += 1
        return int(result)
 
 
if __name__ == "__main__":
    app = App()
    print(app.solve(1))
    print(app.solve(2))
    print(app.solve(3))
    print(app.solve(4))
    print(app.solve(5))
    print(app.solve(6))
    print(app.solve(7))
    print(app.solve(8))
    print(app.solve(9))
    print(app.solve(10))

Comments