Skip to content
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

Awk diff of squares #366

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions awk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
- [space-age](./space-age/README.md)
- [all-your-base](./all-your-base/README.md)
- [high-scores](./high-scores/README.md)
- [difference-of-squares](./difference-of-squares/README.md)
19 changes: 19 additions & 0 deletions awk/difference-of-squares/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"difference-of-squares.awk"
],
"test": [
"test-difference-of-squares.bats"
],
"example": [
".meta/example.awk"
]
},
"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"
}
1 change: 1 addition & 0 deletions awk/difference-of-squares/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"awk","exercise":"difference-of-squares","id":"488e8fafe396433c9d3455749663cac9","url":"https://exercism.org/tracks/awk/exercises/difference-of-squares","handle":"vpayno","is_requester":true,"auto_approve":false}
Empty file.
100 changes: 100 additions & 0 deletions awk/difference-of-squares/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Help

## Running the tests

Each exercise contains a test file.
Run the tests using the `bats` program.
```bash
bats test-hello-world.bats
```

`bats` will need to be installed.
See the [Testing on the Bash track][bash] page for instructions to install `bats` for your system.

### bats is implemented in bash

The bats file is a bash script, with some special functions recognized by the `bats` command.
You'll see some tests that look like
```sh
gawk -f some-exercise.awk <<< "some,input,here"
```
That `<<<` syntax is a bash [Here String][here-string].
It sends the string on the right-hand side into the standard input of the program on the left-hand side.
It is ([approximately][so]) the same as
```sh
echo "some,input,here" | gawk -f some-exercise.awk
```

## Help for assert functions

The tests use functions from the [bats-assert][bats-assert] library.
Help for the various `assert*` functions can be found there.

## Skipped tests

Solving an exercise means making all its tests pass.
By default, only one test (the first one) is executed when you run the tests.
This is intentional, as it allows you to focus on just making that one test pass.
Once it passes, you can enable the next test by commenting out or removing the

[[ $BATS_RUN_SKIPPED == true ]] || skip

annotations prepending other tests.

## Overriding skips

To run all tests, including the ones with `skip` annotations, you can run:
```bash
BATS_RUN_SKIPPED=true bats test-some-exercise.bats
```

It can be convenient to use a wrapper function to save on typing: in `bash` you can do:
```bash
bats() {
BATS_RUN_SKIPPED=true command bats *.bats
}
```
Then run tests with just:
```bash
bats
```

[bash]: https://exercism.org/docs/tracks/bash/tests
[bats-assert]: https://github.com/bats-core/bats-assert
[here-string]: https://www.gnu.org/software/bash/manual/bash.html#Here-Strings
[so]: https://unix.stackexchange.com/a/80372/4667

## Submitting your solution

You can submit your solution using the `exercism submit difference-of-squares.awk` 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 [AWK track's documentation](https://exercism.org/docs/tracks/awk)
- The [AWK track's programming category on the forum](https://forum.exercism.org/c/programming/awk)
- [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.

Places to look for help for AWK questions:

* [Stack Overflow `awk` tag][so].
* check the Resources section of the [Stack Overflow `awk` tag into page][so-info].
* raise an issue at the [exercism/awk][github] Github repository.
* IRC: `irc://irc.liberachat.net/#awk`, `irc://irc.liberachat.net/#exercism`
* see [Libera.chat][libera] if you're unfamiliar with IRC.


[so]: https://stackoverflow.com/tags/awk
[so-info]: https://stackoverflow.com/tags/awk/info
[github]: https://github.com/exercism/awk
[libera]: https://libera.chat
36 changes: 36 additions & 0 deletions awk/difference-of-squares/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Difference of Squares

Welcome to Difference of Squares on Exercism's AWK 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

- @glennj

### Based on

Problem 6 at Project Euler - https://projecteuler.net/problem=6

### My Solution

- [my solution](./difference-of-squares.awk)
- [awkunit tests](./difference-of-squares_test.awk)
- [test cases](./test-cases.awk)
- [run-tests](./run-tests-awk.txt)
1 change: 1 addition & 0 deletions awk/difference-of-squares/awkunit.awk
Loading
Loading