Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions exercises/Reverse/Complete/ReverseComplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public static function int2(int $number): int
{
$revers = 0;

$sing = $number <=> 0;
$sign = $number <=> 0;
$number = abs($number);

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

return $revers * $sing;
return $revers * $sign;
}

public static function int3(int $number): int
{
$reverse = 0;

$sign = $number <=> 0;
$number = abs($number);

while ($number > 0) {
$lastDigit = $number % 10;
$reverse = ($reverse * 10) + $lastDigit;
$number = (int) ($number / 10);
}
return $reverse * $sign;
}
}
30 changes: 30 additions & 0 deletions tests/Reverse/Complete/ReverseCompleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,36 @@ public function testCanReverseIntNegativeWithZeros2(): void
self::assertSame(-21, ReverseComplete::int2(-120));
}

public function testCanReverseIntZero3(): void
{
self::assertSame(0, ReverseComplete::int3(0));
}

public function testCanReverseInt3(): void
{
self::assertSame(21, ReverseComplete::int3(12));
}

public function testCanReverseIntWithThreeDigits3(): void
{
self::assertSame(321, ReverseComplete::int3(123));
}

public function testCanReverseIntZeros3(): void
{
self::assertSame(3, ReverseComplete::int3(300));
}

public function testCanReverseIntNegative3(): void
{
self::assertSame(-21, ReverseComplete::int3(-12));
}

public function testCanReverseIntNegativeWithZeros3(): void
{
self::assertSame(-21, ReverseComplete::int3(-120));
}

public function testCanReverseString1(): void
{
self::assertSame('ytrewq', ReverseComplete::string1('qwerty'));
Expand Down