From ddebf2f85b7d8aa1fd833b8fafb7ddfbb5d34128 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 1 Aug 2024 10:04:02 -0400 Subject: [PATCH] Zig: completed Leap and Difference of Squares --- .../.exercism/config.json | 19 ++++++ .../.exercism/metadata.json | 1 + zig/difference-of-squares/HELP.md | 53 +++++++++++++++++ zig/difference-of-squares/README.md | 29 ++++++++++ .../difference_of_squares.zig | 19 ++++++ .../test_difference_of_squares.zig | 58 +++++++++++++++++++ zig/leap/.exercism/config.json | 19 ++++++ zig/leap/.exercism/metadata.json | 1 + zig/leap/HELP.md | 53 +++++++++++++++++ zig/leap/README.md | 37 ++++++++++++ zig/leap/leap.zig | 12 ++++ zig/leap/test_leap.zig | 40 +++++++++++++ 12 files changed, 341 insertions(+) create mode 100644 zig/difference-of-squares/.exercism/config.json create mode 100644 zig/difference-of-squares/.exercism/metadata.json create mode 100644 zig/difference-of-squares/HELP.md create mode 100644 zig/difference-of-squares/README.md create mode 100644 zig/difference-of-squares/difference_of_squares.zig create mode 100644 zig/difference-of-squares/test_difference_of_squares.zig create mode 100644 zig/leap/.exercism/config.json create mode 100644 zig/leap/.exercism/metadata.json create mode 100644 zig/leap/HELP.md create mode 100644 zig/leap/README.md create mode 100644 zig/leap/leap.zig create mode 100644 zig/leap/test_leap.zig diff --git a/zig/difference-of-squares/.exercism/config.json b/zig/difference-of-squares/.exercism/config.json new file mode 100644 index 0000000..77deda4 --- /dev/null +++ b/zig/difference-of-squares/.exercism/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "massivelivefun" + ], + "files": { + "solution": [ + "difference_of_squares.zig" + ], + "test": [ + "test_difference_of_squares.zig" + ], + "example": [ + ".meta/example.zig" + ] + }, + "blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.", + "source": "Problem 6 at Project Euler", + "source_url": "https://projecteuler.net/problem=6" +} diff --git a/zig/difference-of-squares/.exercism/metadata.json b/zig/difference-of-squares/.exercism/metadata.json new file mode 100644 index 0000000..fc12a7a --- /dev/null +++ b/zig/difference-of-squares/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"zig","exercise":"difference-of-squares","id":"05fde6d09bb749e38afa56a478926999","url":"https://exercism.org/tracks/zig/exercises/difference-of-squares","handle":"Chomp1295","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/zig/difference-of-squares/HELP.md b/zig/difference-of-squares/HELP.md new file mode 100644 index 0000000..44f19cb --- /dev/null +++ b/zig/difference-of-squares/HELP.md @@ -0,0 +1,53 @@ +# Help + +## Running the tests + +Write your code in `.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 difference_of_squares.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 \ No newline at end of file diff --git a/zig/difference-of-squares/README.md b/zig/difference-of-squares/README.md new file mode 100644 index 0000000..d013f9b --- /dev/null +++ b/zig/difference-of-squares/README.md @@ -0,0 +1,29 @@ +# Difference of Squares + +Welcome to Difference of Squares on Exercism's Zig Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Find the difference between the square of the sum and the sum of the squares of the first N natural numbers. + +The square of the sum of the first ten natural numbers is +(1 + 2 + ... + 10)² = 55² = 3025. + +The sum of the squares of the first ten natural numbers is +1² + 2² + ... + 10² = 385. + +Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640. + +You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged. +Finding the best algorithm for the problem is a key skill in software engineering. + +## Source + +### Created by + +- @massivelivefun + +### Based on + +Problem 6 at Project Euler - https://projecteuler.net/problem=6 \ No newline at end of file diff --git a/zig/difference-of-squares/difference_of_squares.zig b/zig/difference-of-squares/difference_of_squares.zig new file mode 100644 index 0000000..f4aa1f2 --- /dev/null +++ b/zig/difference-of-squares/difference_of_squares.zig @@ -0,0 +1,19 @@ +pub fn squareOfSum(number: usize) usize { + var sum: usize = 0; + for (0..number + 1) |i| { + sum += i; + } + return sum * sum; +} + +pub fn sumOfSquares(number: usize) usize { + var sum: usize = 0; + for (1..number + 1) |i| { + sum += i * i; + } + return sum; +} + +pub fn differenceOfSquares(number: usize) usize { + return squareOfSum(number) - sumOfSquares(number); +} diff --git a/zig/difference-of-squares/test_difference_of_squares.zig b/zig/difference-of-squares/test_difference_of_squares.zig new file mode 100644 index 0000000..4138571 --- /dev/null +++ b/zig/difference-of-squares/test_difference_of_squares.zig @@ -0,0 +1,58 @@ +const std = @import("std"); +const testing = std.testing; + +const difference_of_squares = @import("difference_of_squares.zig"); + +test "square of sum up to 1" { + const expected: usize = 1; + const actual = difference_of_squares.squareOfSum(1); + try testing.expectEqual(expected, actual); +} + +test "square of sum up to 5" { + const expected: usize = 225; + const actual = difference_of_squares.squareOfSum(5); + try testing.expectEqual(expected, actual); +} + +test "square of sum up to 100" { + const expected: usize = 25_502_500; + const actual = difference_of_squares.squareOfSum(100); + try testing.expectEqual(expected, actual); +} + +test "sum of squares up to 1" { + const expected: usize = 1; + const actual = difference_of_squares.sumOfSquares(1); + try testing.expectEqual(expected, actual); +} + +test "sum of squares up to 5" { + const expected: usize = 55; + const actual = difference_of_squares.sumOfSquares(5); + try testing.expectEqual(expected, actual); +} + +test "sum of squares up to 100" { + const expected: usize = 338_350; + const actual = difference_of_squares.sumOfSquares(100); + try testing.expectEqual(expected, actual); +} + +test "difference of squares up to 1" { + const expected: usize = 0; + const actual = difference_of_squares.differenceOfSquares(1); + try testing.expectEqual(expected, actual); +} + +test "difference of squares up to 5" { + const expected: usize = 170; + const actual = difference_of_squares.differenceOfSquares(5); + try testing.expectEqual(expected, actual); +} + +test "difference of squares up to 100" { + const expected: usize = 25_164_150; + const actual = difference_of_squares.differenceOfSquares(100); + try testing.expectEqual(expected, actual); +} diff --git a/zig/leap/.exercism/config.json b/zig/leap/.exercism/config.json new file mode 100644 index 0000000..44d4f04 --- /dev/null +++ b/zig/leap/.exercism/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "massivelivefun" + ], + "files": { + "solution": [ + "leap.zig" + ], + "test": [ + "test_leap.zig" + ], + "example": [ + ".meta/example.zig" + ] + }, + "blurb": "Given a year, report if it is a leap year.", + "source": "CodeRanch Cattle Drive, Assignment 3", + "source_url": "https://coderanch.com/t/718816/Leap" +} diff --git a/zig/leap/.exercism/metadata.json b/zig/leap/.exercism/metadata.json new file mode 100644 index 0000000..99281d0 --- /dev/null +++ b/zig/leap/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"zig","exercise":"leap","id":"c311a440c14b4ef084adf8482af46d12","url":"https://exercism.org/tracks/zig/exercises/leap","handle":"Chomp1295","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/zig/leap/HELP.md b/zig/leap/HELP.md new file mode 100644 index 0000000..e8698b5 --- /dev/null +++ b/zig/leap/HELP.md @@ -0,0 +1,53 @@ +# Help + +## Running the tests + +Write your code in `.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 leap.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 Learn][zig-learn] 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-learn]: https://ziglearn.org/ +[ziglings]: https://github.com/ratfactor/ziglings \ No newline at end of file diff --git a/zig/leap/README.md b/zig/leap/README.md new file mode 100644 index 0000000..504196c --- /dev/null +++ b/zig/leap/README.md @@ -0,0 +1,37 @@ +# Leap + +Welcome to Leap on Exercism's Zig Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Given a year, report if it is a leap year. + +The tricky thing here is that a leap year in the Gregorian calendar occurs: + +```text +on every year that is evenly divisible by 4 + except every year that is evenly divisible by 100 + unless the year is also evenly divisible by 400 +``` + +For example, 1997 is not a leap year, but 1996 is. +1900 is not a leap year, but 2000 is. + +## Notes + +Though our exercise adopts some very simple rules, there is more to learn! + +For a delightful, four minute explanation of the whole leap year phenomenon, go watch [this youtube video][video]. + +[video]: https://www.youtube.com/watch?v=xX96xng7sAE + +## Source + +### Created by + +- @massivelivefun + +### Based on + +CodeRanch Cattle Drive, Assignment 3 - https://coderanch.com/t/718816/Leap \ No newline at end of file diff --git a/zig/leap/leap.zig b/zig/leap/leap.zig new file mode 100644 index 0000000..0838961 --- /dev/null +++ b/zig/leap/leap.zig @@ -0,0 +1,12 @@ +pub fn isLeapYear(year: u32) bool { + if (year % 4 == 0) { + if (year % 100 == 0) { + if (year % 400 == 0) { + return true; + } + return false; + } + return true; + } + return false; +} diff --git a/zig/leap/test_leap.zig b/zig/leap/test_leap.zig new file mode 100644 index 0000000..3ac91d3 --- /dev/null +++ b/zig/leap/test_leap.zig @@ -0,0 +1,40 @@ +const std = @import("std"); +const testing = std.testing; + +const leap = @import("leap.zig"); + +test "year not divisible by 4 in common year" { + try testing.expect(!leap.isLeapYear(2015)); +} + +test "year divisible by 2, not divisible by 4 in common year" { + try testing.expect(!leap.isLeapYear(1970)); +} + +test "year divisible by 4, not divisible by 100 in leap year" { + try testing.expect(leap.isLeapYear(1996)); +} + +test "year divisible by 4 and 5 is still a leap year" { + try testing.expect(leap.isLeapYear(1960)); +} + +test "year divisible by 100, not divisible by 400 in common year" { + try testing.expect(!leap.isLeapYear(2100)); +} + +test "year divisible by 100 but not by 3 is still not a leap year" { + try testing.expect(!leap.isLeapYear(1900)); +} + +test "year divisible by 400 is leap year" { + try testing.expect(leap.isLeapYear(2000)); +} + +test "year divisible by 400 but not by 125 is still a leap year" { + try testing.expect(leap.isLeapYear(2400)); +} + +test "year divisible by 200, not divisible by 400 in common year" { + try testing.expect(!leap.isLeapYear(1800)); +}