-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exercises: use usize, not isize, when guaranteed non-negative (#207)
Rationale: - This makes it self-documenting when the output cannot be negative. - This makes it self-documenting when a negative input is not meaningful/useful, and when the user doesn't need to return an error for a negative input. - There isn't much benefit for the user to change `isize` to `usize` themselves.
- Loading branch information
Showing
9 changed files
with
15 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
pub fn squareOfSum(number: isize) isize { | ||
pub fn squareOfSum(number: usize) usize { | ||
const result = @divExact(number * (number + 1), 2); | ||
return result * result; | ||
} | ||
|
||
pub fn sumOfSquares(number: isize) isize { | ||
pub fn sumOfSquares(number: usize) usize { | ||
return @divExact(number * (number + 1) * (2 * number + 1), 6); | ||
} | ||
|
||
pub fn differenceOfSquares(number: isize) isize { | ||
pub fn differenceOfSquares(number: usize) usize { | ||
return squareOfSum(number) - sumOfSquares(number); | ||
} |
6 changes: 3 additions & 3 deletions
6
exercises/practice/difference-of-squares/difference_of_squares.zig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
pub fn squareOfSum(number: isize) isize { | ||
pub fn squareOfSum(number: usize) usize { | ||
_ = number; | ||
@compileError("compute the sum of i from 0 to n then square it"); | ||
} | ||
|
||
pub fn sumOfSquares(number: isize) isize { | ||
pub fn sumOfSquares(number: usize) usize { | ||
_ = number; | ||
@compileError("compute the sum of i^2 from 0 to n"); | ||
} | ||
|
||
pub fn differenceOfSquares(number: isize) isize { | ||
pub fn differenceOfSquares(number: usize) usize { | ||
_ = number; | ||
@compileError("compute the difference between the square of sum and sum of squares"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
pub fn colorCode(colors: [2]ColorBand) isize { | ||
pub fn colorCode(colors: [2]ColorBand) usize { | ||
_ = colors; | ||
@compileError("please implement the colorCode function"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters