Skip to content

Commit be2f121

Browse files
authored
Merge pull request azdanov#14 from fathom/master
added int3 to Reverse and test cases
2 parents f2a9853 + db8ceb7 commit be2f121

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

exercises/Reverse/Complete/ReverseComplete.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function int2(int $number): int
7070
{
7171
$revers = 0;
7272

73-
$sing = $number <=> 0;
73+
$sign = $number <=> 0;
7474
$number = abs($number);
7575

7676
$n = (int) log10($number) + 1;
@@ -82,6 +82,21 @@ public static function int2(int $number): int
8282
$revers += $numeral * pow(10, $i - 1);
8383
}
8484

85-
return $revers * $sing;
85+
return $revers * $sign;
86+
}
87+
88+
public static function int3(int $number): int
89+
{
90+
$reverse = 0;
91+
92+
$sign = $number <=> 0;
93+
$number = abs($number);
94+
95+
while ($number > 0) {
96+
$lastDigit = $number % 10;
97+
$reverse = ($reverse * 10) + $lastDigit;
98+
$number = (int) ($number / 10);
99+
}
100+
return $reverse * $sign;
86101
}
87102
}

tests/Reverse/Complete/ReverseCompleteTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,36 @@ public function testCanReverseIntNegativeWithZeros2(): void
6969
self::assertSame(-21, ReverseComplete::int2(-120));
7070
}
7171

72+
public function testCanReverseIntZero3(): void
73+
{
74+
self::assertSame(0, ReverseComplete::int3(0));
75+
}
76+
77+
public function testCanReverseInt3(): void
78+
{
79+
self::assertSame(21, ReverseComplete::int3(12));
80+
}
81+
82+
public function testCanReverseIntWithThreeDigits3(): void
83+
{
84+
self::assertSame(321, ReverseComplete::int3(123));
85+
}
86+
87+
public function testCanReverseIntZeros3(): void
88+
{
89+
self::assertSame(3, ReverseComplete::int3(300));
90+
}
91+
92+
public function testCanReverseIntNegative3(): void
93+
{
94+
self::assertSame(-21, ReverseComplete::int3(-12));
95+
}
96+
97+
public function testCanReverseIntNegativeWithZeros3(): void
98+
{
99+
self::assertSame(-21, ReverseComplete::int3(-120));
100+
}
101+
72102
public function testCanReverseString1(): void
73103
{
74104
self::assertSame('ytrewq', ReverseComplete::string1('qwerty'));

0 commit comments

Comments
 (0)