From 08444e366162df51fb13dcc482ad504b42d6a3a8 Mon Sep 17 00:00:00 2001 From: Daniel Mita Date: Tue, 19 Dec 2023 12:20:55 +0000 Subject: [PATCH] Reimplement prime-factors --- config.json | 3 +- .../practice/prime-factors/.meta/config.json | 6 +- .../prime-factors/.meta/solutions/Prime.pm | 26 ----- .../.meta/solutions/lib/PrimeFactors.pm | 25 +++++ .../prime-factors/.meta/solutions/prime.t | 1 - .../.meta/solutions/t/prime-factors.t | 1 + .../prime-factors/.meta/template-data.yaml | 34 +++++++ .../practice/prime-factors/.meta/tests.toml | 28 +++++- exercises/practice/prime-factors/Prime.pm | 12 --- .../prime-factors/lib/PrimeFactors.pm | 10 ++ exercises/practice/prime-factors/prime.t | 99 ------------------- .../practice/prime-factors/t/prime-factors.t | 81 +++++++++++++++ 12 files changed, 180 insertions(+), 146 deletions(-) delete mode 100644 exercises/practice/prime-factors/.meta/solutions/Prime.pm create mode 100644 exercises/practice/prime-factors/.meta/solutions/lib/PrimeFactors.pm delete mode 120000 exercises/practice/prime-factors/.meta/solutions/prime.t create mode 120000 exercises/practice/prime-factors/.meta/solutions/t/prime-factors.t create mode 100644 exercises/practice/prime-factors/.meta/template-data.yaml delete mode 100644 exercises/practice/prime-factors/Prime.pm create mode 100644 exercises/practice/prime-factors/lib/PrimeFactors.pm delete mode 100644 exercises/practice/prime-factors/prime.t create mode 100755 exercises/practice/prime-factors/t/prime-factors.t diff --git a/config.json b/config.json index 120cfc1f..7e44a0c7 100644 --- a/config.json +++ b/config.json @@ -432,8 +432,7 @@ "uuid": "bcfc5046-ec4e-4b6e-a5c6-811883f021a0", "practices": [], "prerequisites": [], - "difficulty": 1, - "status": "wip" + "difficulty": 1 }, { "slug": "proverb", diff --git a/exercises/practice/prime-factors/.meta/config.json b/exercises/practice/prime-factors/.meta/config.json index 0c77552d..8e5a9402 100644 --- a/exercises/practice/prime-factors/.meta/config.json +++ b/exercises/practice/prime-factors/.meta/config.json @@ -9,13 +9,13 @@ ], "files": { "solution": [ - "Prime.pm" + "lib/PrimeFactors.pm" ], "test": [ - "prime.t" + "t/prime-factors.t" ], "example": [ - ".meta/solutions/Prime.pm" + ".meta/solutions/lib/PrimeFactors.pm" ] }, "blurb": "Compute the prime factors of a given natural number.", diff --git a/exercises/practice/prime-factors/.meta/solutions/Prime.pm b/exercises/practice/prime-factors/.meta/solutions/Prime.pm deleted file mode 100644 index d1eb96e9..00000000 --- a/exercises/practice/prime-factors/.meta/solutions/Prime.pm +++ /dev/null @@ -1,26 +0,0 @@ -package Prime; -use strict; -use warnings; - -sub factors { - my ($num) = @_; - - return [] if $num == 1; - my @factors; - my $i = 2; - while ( $i * $i <= $num ) { - my $div = $num / $i; - if ( $div == int $div ) { - push @factors, $i; - $num = $div; - next; - } - $i++; - } - push @factors, $num; - - return \@factors; -} - -1; - diff --git a/exercises/practice/prime-factors/.meta/solutions/lib/PrimeFactors.pm b/exercises/practice/prime-factors/.meta/solutions/lib/PrimeFactors.pm new file mode 100644 index 00000000..0c7aed7b --- /dev/null +++ b/exercises/practice/prime-factors/.meta/solutions/lib/PrimeFactors.pm @@ -0,0 +1,25 @@ +package PrimeFactors; + +use strict; +use warnings; +use experimental qw; + +use Exporter qw; +our @EXPORT_OK = qw; + +sub factors ($number) { + my @factors; + + for ( my $i = 2; $i * $i <= $number; $i++ ) { + while ( $number % $i == 0 ) { + push @factors, $i; + $number /= $i; + } + } + + push @factors, $number if $number > 1; + + return \@factors; +} + +1; diff --git a/exercises/practice/prime-factors/.meta/solutions/prime.t b/exercises/practice/prime-factors/.meta/solutions/prime.t deleted file mode 120000 index 2320fd0f..00000000 --- a/exercises/practice/prime-factors/.meta/solutions/prime.t +++ /dev/null @@ -1 +0,0 @@ -../../prime.t \ No newline at end of file diff --git a/exercises/practice/prime-factors/.meta/solutions/t/prime-factors.t b/exercises/practice/prime-factors/.meta/solutions/t/prime-factors.t new file mode 120000 index 00000000..41b7e843 --- /dev/null +++ b/exercises/practice/prime-factors/.meta/solutions/t/prime-factors.t @@ -0,0 +1 @@ +../../../t/prime-factors.t \ No newline at end of file diff --git a/exercises/practice/prime-factors/.meta/template-data.yaml b/exercises/practice/prime-factors/.meta/template-data.yaml new file mode 100644 index 00000000..f29a7b21 --- /dev/null +++ b/exercises/practice/prime-factors/.meta/template-data.yaml @@ -0,0 +1,34 @@ +subs: factors + +properties: + factors: + test: |- + use Data::Dmp; + sprintf(<<'END', $case->{input}{value}, map {dmp($_)} @{$case}{qw}); + is( + factors( %s ), + %s, + %s, + ); + END + +example: |- + sub factors ($number) { + my @factors; + + for (my $i = 2; $i * $i <= $number; $i++) { + while ($number % $i == 0) { + push @factors, $i; + $number /= $i; + } + } + + push @factors, $number if $number > 1; + + return \@factors; + } + +stub: |- + sub factors ($number) { + return undef; + } diff --git a/exercises/practice/prime-factors/.meta/tests.toml b/exercises/practice/prime-factors/.meta/tests.toml index f3f05a3e..6f9cc8ce 100644 --- a/exercises/practice/prime-factors/.meta/tests.toml +++ b/exercises/practice/prime-factors/.meta/tests.toml @@ -1,6 +1,13 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. [924fc966-a8f5-4288-82f2-6b9224819ccd] description = "no factors" @@ -8,12 +15,27 @@ description = "no factors" [17e30670-b105-4305-af53-ddde182cb6ad] description = "prime number" +[238d57c8-4c12-42ef-af34-ae4929f94789] +description = "another prime number" + [f59b8350-a180-495a-8fb1-1712fbee1158] description = "square of a prime" +[756949d3-3158-4e3d-91f2-c4f9f043ee70] +description = "product of first prime" + [bc8c113f-9580-4516-8669-c5fc29512ceb] description = "cube of a prime" +[7d6a3300-a4cb-4065-bd33-0ced1de6cb44] +description = "product of second prime" + +[073ac0b2-c915-4362-929d-fc45f7b9a9e4] +description = "product of third prime" + +[6e0e4912-7fb6-47f3-a9ad-dbcd79340c75] +description = "product of first and second prime" + [00485cd3-a3fe-4fbe-a64a-a4308fc1f870] description = "product of primes and non-primes" diff --git a/exercises/practice/prime-factors/Prime.pm b/exercises/practice/prime-factors/Prime.pm deleted file mode 100644 index 39eeadda..00000000 --- a/exercises/practice/prime-factors/Prime.pm +++ /dev/null @@ -1,12 +0,0 @@ -package Prime; -use strict; -use warnings; - -sub factors { - my ($num) = @_; - - return undef; -} - -1; - diff --git a/exercises/practice/prime-factors/lib/PrimeFactors.pm b/exercises/practice/prime-factors/lib/PrimeFactors.pm new file mode 100644 index 00000000..45b77a2b --- /dev/null +++ b/exercises/practice/prime-factors/lib/PrimeFactors.pm @@ -0,0 +1,10 @@ +package PrimeFactors; + +use v5.38; + +use Exporter qw; +our @EXPORT_OK = qw; + +sub factors ($number) { + return undef; +} diff --git a/exercises/practice/prime-factors/prime.t b/exercises/practice/prime-factors/prime.t deleted file mode 100644 index 7b301a1c..00000000 --- a/exercises/practice/prime-factors/prime.t +++ /dev/null @@ -1,99 +0,0 @@ -#!/usr/bin/env perl -use strict; -use warnings; - -my $module = 'Prime'; - -use Test2::Bundle::More; -use JSON::PP qw(decode_json); -use FindBin qw($Bin); -use lib $Bin, "$Bin/local/lib/perl5"; - -my $cases; -{ - local $/ = undef; - $cases = decode_json scalar ; -} - -#plan 3 + @$cases; -#diag explain $cases; - -ok -e "$Bin/$module.pm", "missing $module.pm" - or BAIL_OUT( - "You need to create a class called $module.pm with a constructor called factors." - ); - -eval "use $module"; -ok !$@, "Cannot load $module.pm" - or BAIL_OUT("Does $module.pm compile? Does it end with 1; ? ($@)"); - -can_ok( $module, 'factors' ) - or BAIL_OUT("Missing package $module; or missing sub factors()"); - -my $sub = $module . '::factors'; - -foreach my $c (@$cases) { - no strict 'refs'; - is_deeply $sub->( $c->{input} ), $c->{expected}, $c->{name}; -} - -done_testing(); - -__DATA__ -[ - { - "input" : 1, - "expected" : [], - "name" : "test_1" - }, - { - "input" : 2, - "expected" : [2], - "name" : "test_2" - }, - { - "input" : 3, - "expected" : [3], - "name" : "test_3" - }, - { - "input" : 4, - "expected" : [2, 2], - "name" : "test_4" - }, - { - "input" : 6, - "expected" : [2, 3], - "name" : "test_6" - }, - { - "input" : 8, - "expected" : [2, 2, 2], - "name" : "test_8" - }, - { - "input" : 9, - "expected" : [3, 3], - "name" : "test_9" - }, - { - "input" : 27, - "expected" : [3, 3, 3], - "name" : "test_27" - }, - { - "input" : 625, - "expected" : [5, 5, 5, 5], - "name" : "test_625" - }, - { - "input" : 901255, - "expected" : [5, 17, 23, 461], - "name" : "test_901255" - }, - { - "input" : 93819012551, - "expected" : [11, 9539, 894119], - "name" : "test_93819012551" - } -] diff --git a/exercises/practice/prime-factors/t/prime-factors.t b/exercises/practice/prime-factors/t/prime-factors.t new file mode 100755 index 00000000..8364fc2c --- /dev/null +++ b/exercises/practice/prime-factors/t/prime-factors.t @@ -0,0 +1,81 @@ +#!/usr/bin/env perl +use Test2::V0; + +use FindBin qw<$Bin>; +use lib "$Bin/../lib", "$Bin/../local/lib/perl5"; + +use PrimeFactors qw; + +is( # begin: 924fc966-a8f5-4288-82f2-6b9224819ccd + factors(1), + [], + "no factors", +); # end: 924fc966-a8f5-4288-82f2-6b9224819ccd + +is( # begin: 17e30670-b105-4305-af53-ddde182cb6ad + factors(2), + [2], + "prime number", +); # end: 17e30670-b105-4305-af53-ddde182cb6ad + +is( # begin: 238d57c8-4c12-42ef-af34-ae4929f94789 + factors(3), + [3], + "another prime number", +); # end: 238d57c8-4c12-42ef-af34-ae4929f94789 + +is( # begin: f59b8350-a180-495a-8fb1-1712fbee1158 + factors(9), + [ 3, 3 ], + "square of a prime", +); # end: f59b8350-a180-495a-8fb1-1712fbee1158 + +is( # begin: 756949d3-3158-4e3d-91f2-c4f9f043ee70 + factors(4), + [ 2, 2 ], + "product of first prime", +); # end: 756949d3-3158-4e3d-91f2-c4f9f043ee70 + +is( # begin: bc8c113f-9580-4516-8669-c5fc29512ceb + factors(8), + [ 2, 2, 2 ], + "cube of a prime", +); # end: bc8c113f-9580-4516-8669-c5fc29512ceb + +is( # begin: 7d6a3300-a4cb-4065-bd33-0ced1de6cb44 + factors(27), + [ 3, 3, 3 ], + "product of second prime", +); # end: 7d6a3300-a4cb-4065-bd33-0ced1de6cb44 + +is( # begin: 073ac0b2-c915-4362-929d-fc45f7b9a9e4 + factors(625), + [ 5, 5, 5, 5 ], + "product of third prime", +); # end: 073ac0b2-c915-4362-929d-fc45f7b9a9e4 + +is( # begin: 6e0e4912-7fb6-47f3-a9ad-dbcd79340c75 + factors(6), + [ 2, 3 ], + "product of first and second prime", +); # end: 6e0e4912-7fb6-47f3-a9ad-dbcd79340c75 + +is( # begin: 00485cd3-a3fe-4fbe-a64a-a4308fc1f870 + factors(12), + [ 2, 2, 3 ], + "product of primes and non-primes", +); # end: 00485cd3-a3fe-4fbe-a64a-a4308fc1f870 + +is( # begin: 02251d54-3ca1-4a9b-85e1-b38f4b0ccb91 + factors(901255), + [ 5, 17, 23, 461 ], + "product of primes", +); # end: 02251d54-3ca1-4a9b-85e1-b38f4b0ccb91 + +is( # begin: 070cf8dc-e202-4285-aa37-8d775c9cd473 + factors(93819012551), + [ 11, 9539, 894119 ], + "factors include a large prime", +); # end: 070cf8dc-e202-4285-aa37-8d775c9cd473 + +done_testing;