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 grains #232

Merged
merged 2 commits into from
Sep 3, 2023
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 @@ -25,3 +25,4 @@
- [raindrops](./raindrops/README.md)
- [leap](./leap/README.md)
- [armstrong-numbers](./armstrong-numbers/README.md)
- [grains](./grains/README.md)
19 changes: 19 additions & 0 deletions awk/grains/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"grains.awk"
],
"test": [
"test-grains.bats"
],
"example": [
".meta/example.awk"
]
},
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.",
"source": "The CodeRanch Cattle Drive, Assignment 6",
"source_url": "https://coderanch.com/wiki/718824/Grains"
}
1 change: 1 addition & 0 deletions awk/grains/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"awk","exercise":"grains","id":"1a3045dfcd1642978c12499b7080a7f3","url":"https://exercism.org/tracks/awk/exercises/grains","handle":"vpayno","is_requester":true,"auto_approve":false}
1 change: 1 addition & 0 deletions awk/grains/.lint_default_vars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-M
99 changes: 99 additions & 0 deletions awk/grains/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# 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 grains.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)
- [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
95 changes: 95 additions & 0 deletions awk/grains/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Grains

Welcome to Grains on Exercism's AWK Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.

There once was a wise servant who saved the life of a prince.
The king promised to pay whatever the servant could dream up.
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat.
One grain on the first square of a chess board, with the number of grains doubling on each successive square.

There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).

Write code that shows:

- how many grains were on a given square, and
- the total number of grains on the chessboard

## GNU awk and Arbitrary Precision Arithmetic

By default, numbers in `awk` are represented as double-precision floating
point numbers. This provides 53 bits of precision (see [Table
16.3][table-16.3] in the manual). Unfortunately, this is insufficient to
complete this exercise (spoiler alert, one test requires 63 bits of
precision).

"Insufficient precision" looks like this:
```sh
$ gawk 'BEGIN {
n = 2 ^ 54
if (n == n + 1)
print "not enough precision"
else
print "OK"
}'
```
```none
not enough precision
```

GNU `awk` has the capability to use arbitrary-precision numbers, if the
relevant libraries have been compiled into the `awk` executable. Check
your awk version, and look for "GNU MPFR":
```sh
$ gawk --version
GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.2.0)
...
```

If it's present, enable the capability using the `-M` option:
```sh
$ gawk -M 'BEGIN {
n = 2 ^ 54
if (n == n + 1)
print "not enough precision"
else
print "OK"
}'
```
```none
OK
```

What if my `awk` doesn't have `-M`? You'll have to call out to an external
program that can do arbitrary precision arithmetic, like `bc`. See the
section [Using getline into a Variable from a Pipe][getline-pipe] for an
example how to do this.


Further reading: [Arbitrary Precision Arithmetic][arbitrary]


[table-16.3]: https://www.gnu.org/software/gawk/manual/html_node/Math-Definitions.html#table_002dieee_002dformats
[arbitrary]: https://www.gnu.org/software/gawk/manual/html_node/Arbitrary-Precision-Arithmetic.html
[getline-pipe]: https://www.gnu.org/software/gawk/manual/html_node/Getline_002fVariable_002fPipe.html

## Source

### Created by

- @glennj

### Based on

The CodeRanch Cattle Drive, Assignment 6 - https://coderanch.com/wiki/718824/Grains

### My Solution

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