Skip to content

Commit

Permalink
Reimplement prime-factors
Browse files Browse the repository at this point in the history
  • Loading branch information
m-dango committed Dec 19, 2023
1 parent 2e47eee commit 08444e3
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 146 deletions.
3 changes: 1 addition & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@
"uuid": "bcfc5046-ec4e-4b6e-a5c6-811883f021a0",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"status": "wip"
"difficulty": 1
},
{
"slug": "proverb",
Expand Down
6 changes: 3 additions & 3 deletions exercises/practice/prime-factors/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
26 changes: 0 additions & 26 deletions exercises/practice/prime-factors/.meta/solutions/Prime.pm

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package PrimeFactors;

use strict;
use warnings;
use experimental qw<signatures postderef postderef_qq>;

use Exporter qw<import>;
our @EXPORT_OK = qw<factors>;

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;
1 change: 0 additions & 1 deletion exercises/practice/prime-factors/.meta/solutions/prime.t

This file was deleted.

34 changes: 34 additions & 0 deletions exercises/practice/prime-factors/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
subs: factors

properties:
factors:
test: |-
use Data::Dmp;
sprintf(<<'END', $case->{input}{value}, map {dmp($_)} @{$case}{qw<expected description>});
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;
}
28 changes: 25 additions & 3 deletions exercises/practice/prime-factors/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
# 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"

[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"

Expand Down
12 changes: 0 additions & 12 deletions exercises/practice/prime-factors/Prime.pm

This file was deleted.

10 changes: 10 additions & 0 deletions exercises/practice/prime-factors/lib/PrimeFactors.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package PrimeFactors;

use v5.38;

use Exporter qw<import>;
our @EXPORT_OK = qw<factors>;

sub factors ($number) {
return undef;
}
99 changes: 0 additions & 99 deletions exercises/practice/prime-factors/prime.t

This file was deleted.

81 changes: 81 additions & 0 deletions exercises/practice/prime-factors/t/prime-factors.t
Original file line number Diff line number Diff line change
@@ -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<factors>;

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;

0 comments on commit 08444e3

Please sign in to comment.