exercism-perl5

Repository for my Perl 5 Exercism exercises
git clone git://git.samirparikh.com/exercism-perl5
Log | Files | Refs | README

commit 0a89058f5b9dc715ed2a47c26b141dba451bb653
parent 0ca298743e1fe7c1adae915e4a284531699f3f2f
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Fri,  1 Jul 2022 19:34:08 +0000

initial commit for grains

Diffstat:
Agrains/Grains.pm | 19+++++++++++++++++++
Agrains/HELP.md | 36++++++++++++++++++++++++++++++++++++
Agrains/README.md | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Agrains/grains.t | 125+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 235 insertions(+), 0 deletions(-)

diff --git a/grains/Grains.pm b/grains/Grains.pm @@ -0,0 +1,19 @@ +package Grains; + +use strict; +use warnings; +use feature qw<say>; + +use Exporter qw<import>; +our @EXPORT_OK = qw<grains_on_square total_grains>; + +sub grains_on_square { + my ($square) = @_; + return undef; +} + +sub total_grains { + return undef; +} + +1; diff --git a/grains/HELP.md b/grains/HELP.md @@ -0,0 +1,35 @@ +# Help + +## Running the tests + +There is a Perl 5 script with the extension `.t`, which will be used to test +your solution. You can run through the tests by using the command: + +```bash +`prove .` +``` + +## Submitting your solution + +You can submit your solution using the `exercism submit Grains.pm` 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 [Perl 5 track's documentation](https://exercism.org/docs/tracks/perl5) +- [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: + +- [/r/perl5](https://www.reddit.com/r/perl5) is the perl5 subreddit. +- [StackOverflow](http://stackoverflow.com/questions/tagged/perl5) 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/grains/README.md b/grains/README.md @@ -0,0 +1,54 @@ +# Grains + +Welcome to Grains on Exercism's Perl 5 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 + +- @bistik + +### Contributed to by + +- @darksuji +- @dnmfarrell +- @kappa +- @kentfredric +- @kytrinyx +- @m-dango +- @rfilipo +- @sshine + +### Based on + +JavaRanch Cattle Drive, exercise 6 - http://www.javaranch.com/grains.jsp +\ No newline at end of file diff --git a/grains/grains.t b/grains/grains.t @@ -0,0 +1,125 @@ +#!/usr/bin/env perl +use Test2::V0; +use JSON::PP; +use constant JSON => JSON::PP->new; + +use FindBin qw<$Bin>; +use lib $Bin, "$Bin/local/lib/perl5"; + +use Grains qw<grains_on_square total_grains>; + +my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; +plan 12; + +imported_ok qw<grains_on_square total_grains> or bail_out; + +for my $case (@test_cases) { + if ( ref $case->{expected} ) { + like dies( sub { grains_on_square( $case->{input}{square} ) } ), + qr/$case->{expected}{error}/, + $case->{description}; + } + elsif ( $case->{property} eq 'square' ) { + is( grains_on_square( $case->{input}{square} ), + $case->{expected}, 'square no. ' . $case->{description} ); + } + elsif ( $case->{property} eq 'total' ) { + is( total_grains(), $case->{expected}, $case->{description} ); + } +} + +__DATA__ +[ + { + "description": "returns the number of grains on the square: grains on square 1", + "expected": 1, + "input": { + "square": 1 + }, + "property": "square" + }, + { + "description": "returns the number of grains on the square: grains on square 2", + "expected": 2, + "input": { + "square": 2 + }, + "property": "square" + }, + { + "description": "returns the number of grains on the square: grains on square 3", + "expected": 4, + "input": { + "square": 3 + }, + "property": "square" + }, + { + "description": "returns the number of grains on the square: grains on square 4", + "expected": 8, + "input": { + "square": 4 + }, + "property": "square" + }, + { + "description": "returns the number of grains on the square: grains on square 16", + "expected": 32768, + "input": { + "square": 16 + }, + "property": "square" + }, + { + "description": "returns the number of grains on the square: grains on square 32", + "expected": 2147483648, + "input": { + "square": 32 + }, + "property": "square" + }, + { + "description": "returns the number of grains on the square: grains on square 64", + "expected": 9223372036854775808, + "input": { + "square": 64 + }, + "property": "square" + }, + { + "description": "returns the number of grains on the square: square 0 raises an exception", + "expected": { + "error": "square must be between 1 and 64" + }, + "input": { + "square": 0 + }, + "property": "square" + }, + { + "description": "returns the number of grains on the square: negative square raises an exception", + "expected": { + "error": "square must be between 1 and 64" + }, + "input": { + "square": -1 + }, + "property": "square" + }, + { + "description": "returns the number of grains on the square: square greater than 64 raises an exception", + "expected": { + "error": "square must be between 1 and 64" + }, + "input": { + "square": 65 + }, + "property": "square" + }, + { + "description": "returns the total number of grains on the board", + "expected": 18446744073709551615, + "input": {}, + "property": "total" + } +]