Skip to content

Commit b6ed9e5

Browse files
authored
Sync collatz-conjecture (#829)
[no important files changed]
1 parent e93178d commit b6ed9e5

File tree

5 files changed

+60
-57
lines changed

5 files changed

+60
-57
lines changed

exercises/practice/collatz-conjecture/.docs/instructions.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
The Collatz Conjecture or 3x+1 problem can be summarized as follows:
44

5-
Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is
6-
odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely.
7-
The conjecture states that no matter which number you start with, you will
8-
always reach 1 eventually.
5+
Take any positive integer n.
6+
If n is even, divide n by 2 to get n / 2.
7+
If n is odd, multiply n by 3 and add 1 to get 3n + 1.
8+
Repeat the process indefinitely.
9+
The conjecture states that no matter which number you start with, you will always reach 1 eventually.
910

1011
Given a number n, return the number of steps required to reach 1.
1112

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

27-
Resulting in 9 steps. So for input n = 12, the return value would be 9.
28+
Resulting in 9 steps.
29+
So for input n = 12, the return value would be 9.

exercises/practice/collatz-conjecture/.meta/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
".meta/example.php"
2121
]
2222
},
23-
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture",
23+
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
2424
"source": "An unsolved problem in mathematics named after mathematician Lothar Collatz",
2525
"source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem"
2626
}

exercises/practice/collatz-conjecture/.meta/example.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,12 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
function steps($number)
286
{
297
$stepCount = 0;
308
if ($number < 1) {
31-
throw new InvalidArgumentException('Only positive numbers are allowed');
9+
throw new InvalidArgumentException('Only positive integers are allowed');
3210
}
3311
do {
3412
if ($number === 1) {

exercises/practice/collatz-conjecture/.meta/tests.toml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[540a3d51-e7a6-47a5-92a3-4ad1838f0bfd]
613
description = "zero steps for one"
@@ -16,6 +23,16 @@ description = "large number of even and odd steps"
1623

1724
[7d4750e6-def9-4b86-aec7-9f7eb44f95a3]
1825
description = "zero is an error"
26+
include = false
27+
28+
[2187673d-77d6-4543-975e-66df6c50e2da]
29+
description = "zero is an error"
30+
reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3"
1931

2032
[c6c795bf-a288-45e9-86a1-841359ad426d]
2133
description = "negative value is an error"
34+
include = false
35+
36+
[ec11f479-56bc-47fd-a434-bcd7a31a7a2e]
37+
description = "negative value is an error"
38+
reimplements = "c6c795bf-a288-45e9-86a1-841359ad426d"
Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
class CollatzConjectureTest extends PHPUnit\Framework\TestCase
@@ -31,39 +9,67 @@ public static function setUpBeforeClass(): void
319
require_once 'CollatzConjecture.php';
3210
}
3311

12+
/**
13+
* uuid: 540a3d51-e7a6-47a5-92a3-4ad1838f0bfd
14+
* @testdox zero steps for one
15+
*/
3416
public function testZeroStepsForOne(): void
3517
{
3618
$this->assertEquals(0, steps(1));
3719
}
3820

21+
/**
22+
* uuid: 3d76a0a6-ea84-444a-821a-f7857c2c1859
23+
* @testdox divide if even
24+
*/
3925
public function testDivideIfEven(): void
4026
{
4127
$this->assertEquals(4, steps(16));
4228
}
4329

30+
/**
31+
* uuid: 754dea81-123c-429e-b8bc-db20b05a87b9
32+
* @testdox even and odd steps
33+
*/
4434
public function testEvenAndOddSteps(): void
4535
{
4636
$this->assertEquals(9, steps(12));
4737
}
4838

39+
/**
40+
* uuid: ecfd0210-6f85-44f6-8280-f65534892ff6
41+
* @testdox large number of even and odd steps
42+
*/
4943
public function testLargeNumberOfEvenAndOddSteps(): void
5044
{
5145
$this->assertEquals(152, steps(1000000));
5246
}
5347

48+
/**
49+
* uuid: 2187673d-77d6-4543-975e-66df6c50e2da
50+
* @testdox zero is an error
51+
*/
5452
public function testZeroIsAnError(): void
5553
{
5654
$this->expectException(InvalidArgumentException::class);
57-
$this->expectExceptionMessage('Only positive numbers are allowed');
55+
// Matches 'Only positive numbers are allowed'
56+
// or 'Only positive integers are allowed'
57+
$this->expectExceptionMessageMatches('/Only positive (numbers|integers) are allowed/');
5858

5959
steps(0);
6060
}
6161

62+
/**
63+
* uuid: ec11f479-56bc-47fd-a434-bcd7a31a7a2e
64+
* @testdox negative value is an error
65+
*/
6266
public function testNegativeValueIsAnError(): void
6367
{
6468
$this->expectException(InvalidArgumentException::class);
65-
$this->expectExceptionMessage('Only positive numbers are allowed');
69+
// Matches 'Only positive numbers are allowed'
70+
// or 'Only positive integers are allowed'
71+
$this->expectExceptionMessageMatches('/Only positive (numbers|integers) are allowed/');
6672

67-
steps(-1);
73+
steps(-15);
6874
}
6975
}

0 commit comments

Comments
 (0)