2017-11-19 10:42:39 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2017-11-18 16:45:28 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
Napisz funkcję sum_from_one_to_n zwracającą sume liczb od 1 do n.
|
|
|
|
Jeśli podany argument jest mniejszy od 1 powinna być zwracana wartość 0.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def sum_from_one_to_n(n):
|
2017-11-27 07:18:35 +01:00
|
|
|
suma=0
|
|
|
|
if n > 0:
|
|
|
|
for i in range(1,n+1):
|
|
|
|
suma += i
|
|
|
|
return suma
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sum_from_one_to_n(n):
|
|
|
|
def test_special_cases(self):
|
|
|
|
"""Testy przypadków szczególnych."""
|
|
|
|
self.assertEqual(sum_from_one_to_n(-100), 0)
|
|
|
|
self.assertEqual(sum_from_one_to_n(-1), 0)
|
|
|
|
self.assertEqual(sum_from_one_to_n(0), 0)
|
|
|
|
self.assertEqual(sum_from_one_to_n(1), 1)
|
|
|
|
self.assertEqual(sum_from_one_to_n(2), 5)
|
|
|
|
|
|
|
|
def test_regular(self):
|
|
|
|
"""Testy dla kilku liczb"""
|
|
|
|
self.assertEqual(sum_from_one_to_n(3), 14)
|
|
|
|
self.assertEqual(sum_from_one_to_n(4), 385)
|
2017-11-18 16:45:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
def tests(f):
|
|
|
|
inputs = [[999], [-100]]
|
|
|
|
outputs = [499500, 0]
|
|
|
|
|
|
|
|
for input, output in zip(inputs, outputs):
|
|
|
|
if f(*input) != output:
|
|
|
|
return "ERROR: {}!={}".format(f(*input), output)
|
|
|
|
break
|
|
|
|
return "TESTS PASSED"
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print(tests(sum_from_one_to_n))
|