commit cd4add2cfa09915a1361eee27241a50b07239347
parent 4bcfc8a56e1b423dc44fec1f03747e45775b7304
Author: Samir Parikh <noreply@samirparikh.com>
Date: Thu, 6 Jan 2022 00:37:20 +0000
solve all tests
Diffstat:
4 files changed, 212 insertions(+), 0 deletions(-)
diff --git a/difference-of-squares/DifferenceOfSquares.pm b/difference-of-squares/DifferenceOfSquares.pm
@@ -0,0 +1,27 @@
+package DifferenceOfSquares;
+use strict;
+use warnings;
+use Exporter qw<import>;
+our @EXPORT_OK
+ = qw<square_of_sum sum_of_squares difference_of_squares>;
+
+sub square_of_sum {
+ my ($number) = @_;
+ my $sum;
+ $sum += $_ foreach (1 .. $number);
+ return $sum * $sum;
+}
+
+sub sum_of_squares {
+ my ($number) = @_;
+ my $sum;
+ $sum += $_ * $_ foreach (1 .. $number);
+ return $sum;
+}
+
+sub difference_of_squares {
+ my ($number) = @_;
+ return square_of_sum($number) - sum_of_squares($number);
+}
+
+1;
diff --git a/difference-of-squares/HELP.md b/difference-of-squares/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 DifferenceOfSquares.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/difference-of-squares/README.md b/difference-of-squares/README.md
@@ -0,0 +1,39 @@
+# Difference Of Squares
+
+Welcome to Difference Of Squares on Exercism's Perl 5 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
+
+- @bistik
+
+### Contributed to by
+
+- @kytrinyx
+- @m-dango
+- @rfilipo
+- @Scientifica96
+
+### Based on
+
+Problem 6 at Project Euler - http://projecteuler.net/problem=6
+\ No newline at end of file
diff --git a/difference-of-squares/difference-of-squares.t b/difference-of-squares/difference-of-squares.t
@@ -0,0 +1,109 @@
+#!/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 DifferenceOfSquares
+ qw<square_of_sum sum_of_squares difference_of_squares>;
+
+my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; };
+
+imported_ok qw<square_of_sum sum_of_squares difference_of_squares>
+ or bail_out;
+
+for my $case (@test_cases) {
+ my $func;
+ if ( $case->{property} eq 'squareOfSum' ) {
+ $func = \&square_of_sum;
+ }
+ elsif ( $case->{property} eq 'sumOfSquares' ) {
+ $func = \&sum_of_squares;
+ }
+ elsif ( $case->{property} eq 'differenceOfSquares' ) {
+ $func = \&difference_of_squares;
+ }
+
+ is( $func->( $case->{input}{number} ),
+ $case->{expected}, $case->{description}, );
+}
+
+done_testing;
+
+__DATA__
+[
+ {
+ "description": "Square the sum of the numbers up to the given number: square of sum 1",
+ "expected": 1,
+ "input": {
+ "number": 1
+ },
+ "property": "squareOfSum"
+ },
+ {
+ "description": "Square the sum of the numbers up to the given number: square of sum 5",
+ "expected": 225,
+ "input": {
+ "number": 5
+ },
+ "property": "squareOfSum"
+ },
+ {
+ "description": "Square the sum of the numbers up to the given number: square of sum 100",
+ "expected": 25502500,
+ "input": {
+ "number": 100
+ },
+ "property": "squareOfSum"
+ },
+ {
+ "description": "Sum the squares of the numbers up to the given number: sum of squares 1",
+ "expected": 1,
+ "input": {
+ "number": 1
+ },
+ "property": "sumOfSquares"
+ },
+ {
+ "description": "Sum the squares of the numbers up to the given number: sum of squares 5",
+ "expected": 55,
+ "input": {
+ "number": 5
+ },
+ "property": "sumOfSquares"
+ },
+ {
+ "description": "Sum the squares of the numbers up to the given number: sum of squares 100",
+ "expected": 338350,
+ "input": {
+ "number": 100
+ },
+ "property": "sumOfSquares"
+ },
+ {
+ "description": "Subtract sum of squares from square of sums: difference of squares 1",
+ "expected": 0,
+ "input": {
+ "number": 1
+ },
+ "property": "differenceOfSquares"
+ },
+ {
+ "description": "Subtract sum of squares from square of sums: difference of squares 5",
+ "expected": 170,
+ "input": {
+ "number": 5
+ },
+ "property": "differenceOfSquares"
+ },
+ {
+ "description": "Subtract sum of squares from square of sums: difference of squares 100",
+ "expected": 25164150,
+ "input": {
+ "number": 100
+ },
+ "property": "differenceOfSquares"
+ }
+]