Skip to content

Commit

Permalink
sync collatz conjecture
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasnorre committed Oct 27, 2024
1 parent 32b62f8 commit c065109
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 56 deletions.
12 changes: 7 additions & 5 deletions exercises/practice/collatz-conjecture/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

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.
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.

Expand All @@ -24,4 +25,5 @@ Starting with n = 12, the steps would be as follows:
8. 2
9. 1

Resulting in 9 steps. So for input n = 12, the return value would be 9.
Resulting in 9 steps.
So for input n = 12, the return value would be 9.
2 changes: 1 addition & 1 deletion exercises/practice/collatz-conjecture/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
".meta/example.php"
]
},
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture",
"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"
}
24 changes: 1 addition & 23 deletions exercises/practice/collatz-conjecture/.meta/example.php
Original file line number Diff line number Diff line change
@@ -1,34 +1,12 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

function steps($number)
{
$stepCount = 0;
if ($number < 1) {
throw new InvalidArgumentException('Only positive numbers are allowed');
throw new InvalidArgumentException('Only positive integers are allowed');
}
do {
if ($number === 1) {
Expand Down
23 changes: 20 additions & 3 deletions exercises/practice/collatz-conjecture/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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.

[540a3d51-e7a6-47a5-92a3-4ad1838f0bfd]
description = "zero steps for one"
Expand All @@ -16,6 +23,16 @@ 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"
50 changes: 26 additions & 24 deletions exercises/practice/collatz-conjecture/CollatzConjectureTest.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

class CollatzConjectureTest extends PHPUnit\Framework\TestCase
Expand All @@ -31,38 +9,62 @@ public static function setUpBeforeClass(): void
require_once 'CollatzConjecture.php';
}

/**
* uuid: 540a3d51-e7a6-47a5-92a3-4ad1838f0bfd
* @testdox zero steps for one
*/
public function testZeroStepsForOne(): void
{
$this->assertEquals(0, steps(1));
}

/**
* uuid: 3d76a0a6-ea84-444a-821a-f7857c2c1859
* @testdox divide if even
*/
public function testDivideIfEven(): void
{
$this->assertEquals(4, steps(16));
}

/**
* uuid: 754dea81-123c-429e-b8bc-db20b05a87b9
* @testdox even and odd steps
*/
public function testEvenAndOddSteps(): void
{
$this->assertEquals(9, steps(12));
}

/**
* uuid: ecfd0210-6f85-44f6-8280-f65534892ff6
* @testdox large number of even and odd steps
*/
public function testLargeNumberOfEvenAndOddSteps(): void
{
$this->assertEquals(152, steps(1000000));
}

/**
* uuid: 2187673d-77d6-4543-975e-66df6c50e2da
* @testdox zero is an error
*/
public function testZeroIsAnError(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Only positive numbers are allowed');
$this->expectExceptionMessageMatches('/Only positive (numbers|integers) are allowed/');

steps(0);
}

/**
* uuid: ec11f479-56bc-47fd-a434-bcd7a31a7a2e
* @testdox negative value is an error
*/
public function testNegativeValueIsAnError(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Only positive numbers are allowed');
$this->expectExceptionMessageMatches('/Only positive (numbers|integers) are allowed/');

steps(-1);
}
Expand Down

0 comments on commit c065109

Please sign in to comment.