diff --git a/maths/area.py b/maths/area.py new file mode 100644 index 0000000..bc271db --- /dev/null +++ b/maths/area.py @@ -0,0 +1,16 @@ +def circle_area(radius: float) -> float: + """ + >>> circle_area(10) + 314.1592653589793 + >>> circle_area(0) + 0.0 + """ + import math + + return math.pi * radius * radius + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/maths/leap_year.py b/maths/leap_year.py new file mode 100644 index 0000000..99acff7 --- /dev/null +++ b/maths/leap_year.py @@ -0,0 +1,19 @@ +""" +https://en.wikipedia.org/wiki/Leap_year +""" + + +def is_leap_year(year: int) -> bool: + """ + >>> all(is_leap_year(year) for year in [1600, 2000, 24000]) + True + >>> all(is_leap_year(year) for year in [1999, 2001, 2002]) + False + """ + return year % 4 == 0 and year % 100 != 0 or year % 400 == 0 + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/maths/sum_to_n.py b/maths/sum_to_n.py new file mode 100644 index 0000000..78f6cf9 --- /dev/null +++ b/maths/sum_to_n.py @@ -0,0 +1,14 @@ +def sum_to_n(n: int) -> int: + """ + >>> sum_to_n(100) + 5050 + >>> sum_to_n(10) + 55 + """ + return sum(i for i in range(1, n + 1)) + + +if __name__ == "__main__": + from doctest import testmod + + testmod()