Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions maths/factors.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
from doctest import testmod
from math import sqrt


def factors_of_a_number(num: int) -> list:
"""
>>> factors_of_a_number(1)
[1]
>>> factors_of_a_number(5)
[1, 5]
>>> factors_of_a_number(24)
[1, 2, 3, 4, 6, 8, 12, 24]
>>> factors_of_a_number(-24)
[]
"""
facs: list[int] = []
def factors_of_a_number(num: int) -> list[int]:
"""Return all factors of a positive integer in sorted order."""

Check failure on line 5 in maths/factors.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W291)

maths/factors.py:5:67: W291 Trailing whitespace help: Remove trailing whitespace

Check failure on line 6 in maths/factors.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W293)

maths/factors.py:6:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
if num < 1:

Check failure on line 7 in maths/factors.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

maths/factors.py:7:1: invalid-syntax: Unexpected indentation
return facs
raise ValueError("num must be a positive integer")
facs: list[int] = []

Check failure on line 9 in maths/factors.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W291)

maths/factors.py:9:25: W291 Trailing whitespace help: Remove trailing whitespace
facs.append(1)
if num == 1:
return facs
facs.append(num)
facs.append(num) #num is always a factor of itself

Check failure on line 13 in maths/factors.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W291)

maths/factors.py:13:56: W291 Trailing whitespace help: Remove trailing whitespace
for i in range(2, int(sqrt(num)) + 1):
if num % i == 0: # If i is a factor of num
facs.append(i)
Expand All @@ -30,5 +21,5 @@
return facs


if __name__ == "__main__":

Check failure on line 24 in maths/factors.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (invalid-syntax)

maths/factors.py:24:1: invalid-syntax: Expected a statement
testmod(name="factors_of_a_number", verbose=True)
Loading