mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-12-21 14:23:10 -05:00
Zig: completed Square Root
This commit is contained in:
parent
40ffc9faa3
commit
d0147655f7
6 changed files with 148 additions and 0 deletions
19
zig/square-root/.exercism/config.json
Normal file
19
zig/square-root/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"authors": [
|
||||
"keiravillekode"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"square_root.zig"
|
||||
],
|
||||
"test": [
|
||||
"test_square_root.zig"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.zig"
|
||||
]
|
||||
},
|
||||
"blurb": "Given a natural radicand, return its square root.",
|
||||
"source": "wolf99",
|
||||
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
|
||||
}
|
1
zig/square-root/.exercism/metadata.json
Normal file
1
zig/square-root/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
|||
{"track":"zig","exercise":"square-root","id":"3f417c127e444442bce118ada67c917d","url":"https://exercism.org/tracks/zig/exercises/square-root","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/square-root/HELP.md
Normal file
53
zig/square-root/HELP.md
Normal file
|
@ -0,0 +1,53 @@
|
|||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
Write your code in `<exercise_name>.zig`.
|
||||
|
||||
To run the tests for an exercise, run:
|
||||
|
||||
```bash
|
||||
zig test test_exercise_name.zig
|
||||
```
|
||||
|
||||
in the exercise's root directory (replacing `exercise_name` with the name of the exercise).
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit square_root.zig` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [Zig track's documentation](https://exercism.org/docs/tracks/zig)
|
||||
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
|
||||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
|
||||
- [Zig Guide][zig-guide] is an excellent primer that explains the language features that Zig has to offer.
|
||||
- [Ziglings][ziglings] is highly recommended.
|
||||
Learn Zig by fixing tiny broken programs.
|
||||
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
|
||||
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
|
||||
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
|
||||
- [/r/Zig][reddit] is the main Zig subreddit.
|
||||
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.
|
||||
|
||||
[discord]: https://discordapp.com
|
||||
[discord-zig]: https://discord.com/invite/gxsFFjE
|
||||
[documentation]: https://ziglang.org/documentation/master
|
||||
[irc]: https://webchat.freenode.net/?channels=%23zig
|
||||
[reddit]: https://www.reddit.com/r/Zig
|
||||
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
|
||||
[zig-guide]: https://zig.guide/
|
||||
[ziglings]: https://codeberg.org/ziglings/exercises
|
28
zig/square-root/README.md
Normal file
28
zig/square-root/README.md
Normal file
|
@ -0,0 +1,28 @@
|
|||
# Square Root
|
||||
|
||||
Welcome to Square Root on Exercism's Zig Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Given a natural radicand, return its square root.
|
||||
|
||||
Note that the term "radicand" refers to the number for which the root is to be determined.
|
||||
That is, it is the number under the root symbol.
|
||||
|
||||
Check out the Wikipedia pages on [square root][square-root] and [methods of computing square roots][computing-square-roots].
|
||||
|
||||
Recall also that natural numbers are positive real whole numbers (i.e. 1, 2, 3 and up).
|
||||
|
||||
[square-root]: https://en.wikipedia.org/wiki/Square_root
|
||||
[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @keiravillekode
|
||||
|
||||
### Based on
|
||||
|
||||
wolf99 - https://github.com/exercism/problem-specifications/pull/1582
|
7
zig/square-root/square_root.zig
Normal file
7
zig/square-root/square_root.zig
Normal file
|
@ -0,0 +1,7 @@
|
|||
pub fn squareRoot(radicand: usize) usize {
|
||||
var guess: usize = 1;
|
||||
|
||||
while (guess * guess < radicand) : (guess += 1) {}
|
||||
|
||||
return guess;
|
||||
}
|
40
zig/square-root/test_square_root.zig
Normal file
40
zig/square-root/test_square_root.zig
Normal file
|
@ -0,0 +1,40 @@
|
|||
const std = @import("std");
|
||||
const testing = std.testing;
|
||||
|
||||
const square_root = @import("square_root.zig");
|
||||
|
||||
test "root of 1" {
|
||||
const expected: usize = 1;
|
||||
const actual = square_root.squareRoot(1);
|
||||
try testing.expectEqual(expected, actual);
|
||||
}
|
||||
|
||||
test "root of 4" {
|
||||
const expected: usize = 2;
|
||||
const actual = square_root.squareRoot(4);
|
||||
try testing.expectEqual(expected, actual);
|
||||
}
|
||||
|
||||
test "root of 25" {
|
||||
const expected: usize = 5;
|
||||
const actual = square_root.squareRoot(25);
|
||||
try testing.expectEqual(expected, actual);
|
||||
}
|
||||
|
||||
test "root of 81" {
|
||||
const expected: usize = 9;
|
||||
const actual = square_root.squareRoot(81);
|
||||
try testing.expectEqual(expected, actual);
|
||||
}
|
||||
|
||||
test "root of 196" {
|
||||
const expected: usize = 14;
|
||||
const actual = square_root.squareRoot(196);
|
||||
try testing.expectEqual(expected, actual);
|
||||
}
|
||||
|
||||
test "root of 65025" {
|
||||
const expected: usize = 255;
|
||||
const actual = square_root.squareRoot(65025);
|
||||
try testing.expectEqual(expected, actual);
|
||||
}
|
Loading…
Reference in a new issue