exercism-perl5

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

commit 590f19f5edbdfe7d4b45827bc659329f707e3c61
parent d6eeb08ce21dc686446be15bd3967bf464d4f136
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Mon, 27 Dec 2021 14:59:22 +0000

initial commit for wordy

Diffstat:
Awordy/HELP.md | 36++++++++++++++++++++++++++++++++++++
Awordy/README.md | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Awordy/Wordy.pm | 12++++++++++++
Awordy/wordy.t | 230+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 368 insertions(+), 0 deletions(-)

diff --git a/wordy/HELP.md b/wordy/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 Wordy.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/wordy/README.md b/wordy/README.md @@ -0,0 +1,89 @@ +# Wordy + +Welcome to Wordy on Exercism's Perl 5 Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Parse and evaluate simple math word problems returning the answer as an integer. + +## Iteration 0 — Numbers + +Problems with no operations simply evaluate to the number given. + +> What is 5? + +Evaluates to 5. + +## Iteration 1 — Addition + +Add two numbers together. + +> What is 5 plus 13? + +Evaluates to 18. + +Handle large numbers and negative numbers. + +## Iteration 2 — Subtraction, Multiplication and Division + +Now, perform the other three operations. + +> What is 7 minus 5? + +2 + +> What is 6 multiplied by 4? + +24 + +> What is 25 divided by 5? + +5 + +## Iteration 3 — Multiple Operations + +Handle a set of operations, in sequence. + +Since these are verbal word problems, evaluate the expression from +left-to-right, _ignoring the typical order of operations._ + +> What is 5 plus 13 plus 6? + +24 + +> What is 3 plus 2 multiplied by 3? + +15 (i.e. not 9) + +## Iteration 4 — Errors + +The parser should reject: + +* Unsupported operations ("What is 52 cubed?") +* Non-math questions ("Who is the President of the United States") +* Word problems with invalid syntax ("What is 1 plus plus 2?") + +## Bonus — Exponentials + +If you'd like, handle exponentials. + +> What is 2 raised to the 5th power? + +32 + +## Source + +### Created by + +- @szabgab + +### Contributed to by + +- @kytrinyx +- @m-dango +- @rfilipo + +### Based on + +Inspired by one of the generated questions in the Extreme Startup game. - https://github.com/rchatley/extreme_startup +\ No newline at end of file diff --git a/wordy/Wordy.pm b/wordy/Wordy.pm @@ -0,0 +1,12 @@ +package Wordy; +use strict; +use warnings; +use Exporter qw<import>; +our @EXPORT_OK = qw<answer>; + +sub answer { + my ($question) = @_; + return undef; +} + +1; diff --git a/wordy/wordy.t b/wordy/wordy.t @@ -0,0 +1,230 @@ +#!/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 Wordy qw<answer>; + +my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; + +imported_ok qw<answer> or bail_out; + +for my $case (@test_cases) { + if ( !ref $case->{expected} ) { + is( answer( $case->{input}{question} ), + $case->{expected}, $case->{description}, ); + } + else { + like dies( sub { answer( $case->{input}{question} ) } ), + qr/$case->{expected}{error}/, $case->{description}; + } +} + +done_testing; + +__DATA__ +[ + { + "description": "just a number", + "expected": 5, + "input": { + "question": "What is 5?" + }, + "property": "answer" + }, + { + "description": "addition", + "expected": 2, + "input": { + "question": "What is 1 plus 1?" + }, + "property": "answer" + }, + { + "description": "more addition", + "expected": 55, + "input": { + "question": "What is 53 plus 2?" + }, + "property": "answer" + }, + { + "description": "addition with negative numbers", + "expected": -11, + "input": { + "question": "What is -1 plus -10?" + }, + "property": "answer" + }, + { + "description": "large addition", + "expected": 45801, + "input": { + "question": "What is 123 plus 45678?" + }, + "property": "answer" + }, + { + "description": "subtraction", + "expected": 16, + "input": { + "question": "What is 4 minus -12?" + }, + "property": "answer" + }, + { + "description": "multiplication", + "expected": -75, + "input": { + "question": "What is -3 multiplied by 25?" + }, + "property": "answer" + }, + { + "description": "division", + "expected": -11, + "input": { + "question": "What is 33 divided by -3?" + }, + "property": "answer" + }, + { + "description": "multiple additions", + "expected": 3, + "input": { + "question": "What is 1 plus 1 plus 1?" + }, + "property": "answer" + }, + { + "description": "addition and subtraction", + "expected": 8, + "input": { + "question": "What is 1 plus 5 minus -2?" + }, + "property": "answer" + }, + { + "description": "multiple subtraction", + "expected": 3, + "input": { + "question": "What is 20 minus 4 minus 13?" + }, + "property": "answer" + }, + { + "description": "subtraction then addition", + "expected": 14, + "input": { + "question": "What is 17 minus 6 plus 3?" + }, + "property": "answer" + }, + { + "description": "multiple multiplication", + "expected": -12, + "input": { + "question": "What is 2 multiplied by -2 multiplied by 3?" + }, + "property": "answer" + }, + { + "description": "addition and multiplication", + "expected": -8, + "input": { + "question": "What is -3 plus 7 multiplied by -2?" + }, + "property": "answer" + }, + { + "description": "multiple division", + "expected": 2, + "input": { + "question": "What is -12 divided by 2 divided by -3?" + }, + "property": "answer" + }, + { + "description": "unknown operation", + "expected": { + "error": "unknown operation" + }, + "input": { + "question": "What is 52 cubed?" + }, + "property": "answer" + }, + { + "description": "Non math question", + "expected": { + "error": "unknown operation" + }, + "input": { + "question": "Who is the President of the United States?" + }, + "property": "answer" + }, + { + "description": "reject problem missing an operand", + "expected": { + "error": "syntax error" + }, + "input": { + "question": "What is 1 plus?" + }, + "property": "answer" + }, + { + "description": "reject problem with no operands or operators", + "expected": { + "error": "syntax error" + }, + "input": { + "question": "What is?" + }, + "property": "answer" + }, + { + "description": "reject two operations in a row", + "expected": { + "error": "syntax error" + }, + "input": { + "question": "What is 1 plus plus 2?" + }, + "property": "answer" + }, + { + "description": "reject two numbers in a row", + "expected": { + "error": "syntax error" + }, + "input": { + "question": "What is 1 plus 2 1?" + }, + "property": "answer" + }, + { + "description": "reject postfix notation", + "expected": { + "error": "syntax error" + }, + "input": { + "question": "What is 1 2 plus?" + }, + "property": "answer" + }, + { + "description": "reject prefix notation", + "expected": { + "error": "syntax error" + }, + "input": { + "question": "What is plus 1 2?" + }, + "property": "answer" + } +]