From 8849d2ecdc3cd839d7ef6d793a7cbe5657fd103c Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 14 Oct 2021 12:30:21 -0300 Subject: [PATCH] Grains --- racket/grains/.exercism/config.json | 24 ++++++++++++++ racket/grains/.exercism/metadata.json | 1 + racket/grains/HELP.md | 39 ++++++++++++++++++++++ racket/grains/README.md | 48 +++++++++++++++++++++++++++ racket/grains/grains-test.rkt | 21 ++++++++++++ racket/grains/grains.rkt | 11 ++++++ 6 files changed, 144 insertions(+) create mode 100644 racket/grains/.exercism/config.json create mode 100644 racket/grains/.exercism/metadata.json create mode 100644 racket/grains/HELP.md create mode 100644 racket/grains/README.md create mode 100644 racket/grains/grains-test.rkt create mode 100644 racket/grains/grains.rkt diff --git a/racket/grains/.exercism/config.json b/racket/grains/.exercism/config.json new file mode 100644 index 0000000..7098c48 --- /dev/null +++ b/racket/grains/.exercism/config.json @@ -0,0 +1,24 @@ +{ + "blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.", + "authors": [ + "arguello" + ], + "contributors": [ + "mbertheau", + "PurityControl", + "yurrriq" + ], + "files": { + "solution": [ + "grains.rkt" + ], + "test": [ + "grains-test.rkt" + ], + "example": [ + ".meta/example.rkt" + ] + }, + "source": "JavaRanch Cattle Drive, exercise 6", + "source_url": "http://www.javaranch.com/grains.jsp" +} diff --git a/racket/grains/.exercism/metadata.json b/racket/grains/.exercism/metadata.json new file mode 100644 index 0000000..5eab962 --- /dev/null +++ b/racket/grains/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"racket","exercise":"grains","id":"7dfea06643424904aa9efa5a44544efc","url":"https://exercism.org/tracks/racket/exercises/grains","handle":"JBiason","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/racket/grains/HELP.md b/racket/grains/HELP.md new file mode 100644 index 0000000..2713e14 --- /dev/null +++ b/racket/grains/HELP.md @@ -0,0 +1,39 @@ +# Help + +## Running the tests + +To run the test through DrRacket, simply open the test file and click the 'Run' button in the upper right. + +To run the test from the command line, run the test from the exercise directory with the following command: + +``` +raco test -test.rkt +``` + +where `` should be replaced with the exercise's slug. + +## Submitting your solution + +You can submit your solution using the `exercism submit grains.rkt` 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 [Racket track's documentation](https://exercism.org/docs/tracks/racket) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +To get help if you're having trouble, you can use one of the following resources: + +- [The Racket Reference](http://docs.racket-lang.org/reference/index.html) +- [/r/racket](https://www.reddit.com/r/racket) is the Racket subreddit. +- [StackOverflow](http://stackoverflow.com/questions/tagged/racket) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions. \ No newline at end of file diff --git a/racket/grains/README.md b/racket/grains/README.md new file mode 100644 index 0000000..aac13f4 --- /dev/null +++ b/racket/grains/README.md @@ -0,0 +1,48 @@ +# Grains + +Welcome to Grains on Exercism's Racket 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 + +## For bonus points + +Did you get the tests passing and the code clean? If you want to, these +are some additional things you could try: + +- Optimize for speed. +- Optimize for readability. + +Then please share your thoughts in a comment on the submission. Did this +experiment make the code better? Worse? Did you learn anything from it? + +## Source + +### Created by + +- @arguello + +### Contributed to by + +- @mbertheau +- @PurityControl +- @yurrriq + +### Based on + +JavaRanch Cattle Drive, exercise 6 - http://www.javaranch.com/grains.jsp \ No newline at end of file diff --git a/racket/grains/grains-test.rkt b/racket/grains/grains-test.rkt new file mode 100644 index 0000000..3a6a528 --- /dev/null +++ b/racket/grains/grains-test.rkt @@ -0,0 +1,21 @@ +#lang racket/base + +(require "grains.rkt") + +(module+ test + (require rackunit rackunit/text-ui) + + (define suite + (test-suite + "grains tests" + + (test-eqv? "square 1" (square 1) 1) + (test-eqv? "square 2" (square 2) 2) + (test-eqv? "square 3" (square 3) 4) + (test-eqv? "square 4" (square 4) 8) + (test-eqv? "square 16" (square 16) 32768) + (test-eqv? "square 32" (square 32) 2147483648) + (test-eqv? "square 64" (square 64) 9223372036854775808) + (test-eqv? "total grains" (total) 18446744073709551615))) + + (run-tests suite)) diff --git a/racket/grains/grains.rkt b/racket/grains/grains.rkt new file mode 100644 index 0000000..2a326b6 --- /dev/null +++ b/racket/grains/grains.rkt @@ -0,0 +1,11 @@ +#lang racket + +(provide square total) + +(define (square val) + (if (= val 1) + 1 + (* 2 (square (- val 1))))) + +(define (total) + 18446744073709551615)