-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add math utility functions #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,36 @@ | ||||||||||||||||||||||||||
| """Math utility functions.""" | ||||||||||||||||||||||||||
| import math | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def fibonacci(n: int) -> list[int]: | ||||||||||||||||||||||||||
| if n <= 0: | ||||||||||||||||||||||||||
| return [] | ||||||||||||||||||||||||||
| if n == 1: | ||||||||||||||||||||||||||
| return [0] | ||||||||||||||||||||||||||
| seq = [0, 1] | ||||||||||||||||||||||||||
| for _ in range(2, n): | ||||||||||||||||||||||||||
| seq.append(seq[-1] + seq[-2]) | ||||||||||||||||||||||||||
| return seq | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def is_prime(n: int) -> bool: | ||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docstring: Public function lacks a docstring (org guideline).
Suggested change
|
||||||||||||||||||||||||||
| if n < 2: | ||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||
| for i in range(2, int(math.sqrt(n)) + 1): | ||||||||||||||||||||||||||
| if n % i == 0: | ||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docstring: Public function lacks a docstring (org guideline).
Suggested change
|
||||||||||||||||||||||||||
| def gcd(a: int, b: int) -> int: | ||||||||||||||||||||||||||
| while b: | ||||||||||||||||||||||||||
| a, b = b, a % b | ||||||||||||||||||||||||||
| return a | ||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docstring: Public function lacks a docstring (org guideline).
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Also, even for non-zero inputs, multiplying first (
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test coverage: The
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def lcm(a: int, b: int) -> int: | ||||||||||||||||||||||||||
| return abs(a * b) // gcd(a, b) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def clamp(value: float, min_val: float, max_val: float) -> float: | ||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docstring: Public function lacks a docstring (org guideline).
Suggested change
|
||||||||||||||||||||||||||
| return max(min_val, min(value, max_val)) | ||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing docstring: All public functions must have a docstring describing what the function does, its parameters, and its return value (org guideline).