From 53f00cca11df080d954a7e039399b4ff31ff3478 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Mon, 11 Nov 2024 14:38:02 -0500 Subject: [PATCH] Add collatz-conjecture exercise --- config.json | 8 ++++ .../collatz-conjecture/.docs/instructions.md | 29 ++++++++++++ .../collatz-conjecture/.meta/config.json | 19 ++++++++ .../.meta/solutions/lib/CollatzConjecture.pm | 20 +++++++++ .../.meta/solutions/t/collatz-conjecture.t | 1 + .../.meta/template-data.yaml | 39 ++++++++++++++++ .../collatz-conjecture/.meta/tests.toml | 38 ++++++++++++++++ .../lib/CollatzConjecture.pm | 12 +++++ .../collatz-conjecture/t/collatz-conjecture.t | 45 +++++++++++++++++++ 9 files changed, 211 insertions(+) create mode 100644 exercises/practice/collatz-conjecture/.docs/instructions.md create mode 100644 exercises/practice/collatz-conjecture/.meta/config.json create mode 100644 exercises/practice/collatz-conjecture/.meta/solutions/lib/CollatzConjecture.pm create mode 120000 exercises/practice/collatz-conjecture/.meta/solutions/t/collatz-conjecture.t create mode 100644 exercises/practice/collatz-conjecture/.meta/template-data.yaml create mode 100644 exercises/practice/collatz-conjecture/.meta/tests.toml create mode 100644 exercises/practice/collatz-conjecture/lib/CollatzConjecture.pm create mode 100755 exercises/practice/collatz-conjecture/t/collatz-conjecture.t diff --git a/config.json b/config.json index 80564110..1d430552 100644 --- a/config.json +++ b/config.json @@ -235,6 +235,14 @@ "prerequisites": [], "difficulty": 1 }, + { + "slug": "collatz-conjecture", + "name": "Collatz Conjecture", + "uuid": "d952ed69-1598-449b-a28e-7dfd19b53f89", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "crypto-square", "name": "Crypto Square", diff --git a/exercises/practice/collatz-conjecture/.docs/instructions.md b/exercises/practice/collatz-conjecture/.docs/instructions.md new file mode 100644 index 00000000..ba060483 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.docs/instructions.md @@ -0,0 +1,29 @@ +# Instructions + +The Collatz Conjecture or 3x+1 problem can be summarized as follows: + +Take any positive integer n. +If n is even, divide n by 2 to get n / 2. +If n is odd, multiply n by 3 and add 1 to get 3n + 1. +Repeat the process indefinitely. +The conjecture states that no matter which number you start with, you will always reach 1 eventually. + +Given a number n, return the number of steps required to reach 1. + +## Examples + +Starting with n = 12, the steps would be as follows: + +0. 12 +1. 6 +2. 3 +3. 10 +4. 5 +5. 16 +6. 8 +7. 4 +8. 2 +9. 1 + +Resulting in 9 steps. +So for input n = 12, the return value would be 9. diff --git a/exercises/practice/collatz-conjecture/.meta/config.json b/exercises/practice/collatz-conjecture/.meta/config.json new file mode 100644 index 00000000..1a055f7b --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "lib/CollatzConjecture.pm" + ], + "test": [ + "t/collatz-conjecture.t" + ], + "example": [ + ".meta/solutions/lib/CollatzConjecture.pm" + ] + }, + "blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.", + "source": "An unsolved problem in mathematics named after mathematician Lothar Collatz", + "source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem" +} diff --git a/exercises/practice/collatz-conjecture/.meta/solutions/lib/CollatzConjecture.pm b/exercises/practice/collatz-conjecture/.meta/solutions/lib/CollatzConjecture.pm new file mode 100644 index 00000000..673d83c9 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/solutions/lib/CollatzConjecture.pm @@ -0,0 +1,20 @@ +package CollatzConjecture; + +use strict; +use warnings; +use experimental qw; + +use Exporter qw; +our @EXPORT_OK = qw; + +sub steps ($number) { + die 'Only positive integers are allowed' unless $number > 0; + my $count = 0; + while ( $number > 1 ) { + $number = ( $number % 2 == 0 ) ? ( $number / 2 ) : ( 3 * $number + 1 ); + $count++; + } + return $count; +} + +1; diff --git a/exercises/practice/collatz-conjecture/.meta/solutions/t/collatz-conjecture.t b/exercises/practice/collatz-conjecture/.meta/solutions/t/collatz-conjecture.t new file mode 120000 index 00000000..e34bf474 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/solutions/t/collatz-conjecture.t @@ -0,0 +1 @@ +../../../t/collatz-conjecture.t \ No newline at end of file diff --git a/exercises/practice/collatz-conjecture/.meta/template-data.yaml b/exercises/practice/collatz-conjecture/.meta/template-data.yaml new file mode 100644 index 00000000..a337a3f3 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/template-data.yaml @@ -0,0 +1,39 @@ +subs: steps +properties: + steps: + test: |- + use Data::Dmp; + if (ref $case->{expected}) { + sprintf(<<'END', $case->{input}{number}, $case->{expected}{error}, dmp($case->{description})); + like( + dies( sub { steps(%s) } ), + qr/%s/, + %s, + ); + END + } + else { + sprintf(<<'END', $case->{input}{number}, $case->{expected}, dmp($case->{description})); + is( + steps(%s), + %s, + %s, + ); + END + } + +stub: |- + sub steps ($number) { + return undef; + } + +example: |- + sub steps ($number) { + die 'Only positive integers are allowed' unless $number > 0; + my $count = 0; + while ($number > 1) { + $number = ($number % 2 == 0) ? ($number / 2) : (3 * $number + 1); + $count++; + } + return $count; + } diff --git a/exercises/practice/collatz-conjecture/.meta/tests.toml b/exercises/practice/collatz-conjecture/.meta/tests.toml new file mode 100644 index 00000000..cc34e168 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/tests.toml @@ -0,0 +1,38 @@ +# 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. + +[540a3d51-e7a6-47a5-92a3-4ad1838f0bfd] +description = "zero steps for one" + +[3d76a0a6-ea84-444a-821a-f7857c2c1859] +description = "divide if even" + +[754dea81-123c-429e-b8bc-db20b05a87b9] +description = "even and odd steps" + +[ecfd0210-6f85-44f6-8280-f65534892ff6] +description = "large number of even and odd steps" + +[7d4750e6-def9-4b86-aec7-9f7eb44f95a3] +description = "zero is an error" +include = false + +[2187673d-77d6-4543-975e-66df6c50e2da] +description = "zero is an error" +reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3" + +[c6c795bf-a288-45e9-86a1-841359ad426d] +description = "negative value is an error" +include = false + +[ec11f479-56bc-47fd-a434-bcd7a31a7a2e] +description = "negative value is an error" +reimplements = "c6c795bf-a288-45e9-86a1-841359ad426d" diff --git a/exercises/practice/collatz-conjecture/lib/CollatzConjecture.pm b/exercises/practice/collatz-conjecture/lib/CollatzConjecture.pm new file mode 100644 index 00000000..d29db121 --- /dev/null +++ b/exercises/practice/collatz-conjecture/lib/CollatzConjecture.pm @@ -0,0 +1,12 @@ +package CollatzConjecture; + +use v5.40; + +use Exporter qw; +our @EXPORT_OK = qw; + +sub steps ($number) { + return undef; +} + +1; diff --git a/exercises/practice/collatz-conjecture/t/collatz-conjecture.t b/exercises/practice/collatz-conjecture/t/collatz-conjecture.t new file mode 100755 index 00000000..0fe6bcaa --- /dev/null +++ b/exercises/practice/collatz-conjecture/t/collatz-conjecture.t @@ -0,0 +1,45 @@ +#!/usr/bin/env perl +use Test2::V0; + +use FindBin qw<$Bin>; +use lib "$Bin/../lib", "$Bin/../local/lib/perl5"; + +use CollatzConjecture qw; + +is( # begin: 540a3d51-e7a6-47a5-92a3-4ad1838f0bfd + steps(1), + 0, + "zero steps for one", +); # end: 540a3d51-e7a6-47a5-92a3-4ad1838f0bfd + +is( # begin: 3d76a0a6-ea84-444a-821a-f7857c2c1859 + steps(16), + 4, + "divide if even", +); # end: 3d76a0a6-ea84-444a-821a-f7857c2c1859 + +is( # begin: 754dea81-123c-429e-b8bc-db20b05a87b9 + steps(12), + 9, + "even and odd steps", +); # end: 754dea81-123c-429e-b8bc-db20b05a87b9 + +is( # begin: ecfd0210-6f85-44f6-8280-f65534892ff6 + steps(1000000), + 152, + "large number of even and odd steps", +); # end: ecfd0210-6f85-44f6-8280-f65534892ff6 + +like( # begin: 2187673d-77d6-4543-975e-66df6c50e2da + dies( sub { steps(0) } ), + qr/Only positive integers are allowed/, + "zero is an error", +); # end: 2187673d-77d6-4543-975e-66df6c50e2da + +like( # begin: ec11f479-56bc-47fd-a434-bcd7a31a7a2e + dies( sub { steps(-15) } ), + qr/Only positive integers are allowed/, + "negative value is an error", +); # end: ec11f479-56bc-47fd-a434-bcd7a31a7a2e + +done_testing;