Skip to content

Commit b48c2c5

Browse files
authored
Merge pull request azdanov#12 from fathom/master
added int2 and string5 functions to Reverse and test cases
2 parents 2086a1c + fa2788b commit b48c2c5

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

exercises/Reverse/Complete/ReverseComplete.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,41 @@ public static function string4(string $string): string
4242
});
4343
}
4444

45+
public static function string5(string $string): string
46+
{
47+
$revers = '';
48+
49+
for ($i = mb_strlen($string); $i >= 0; $i--) {
50+
$revers .= mb_substr($string, $i, 1);
51+
}
52+
53+
return $revers;
54+
}
55+
4556
public static function int(int $number): int
4657
{
4758
/** @see https://wiki.php.net/rfc/combined-comparison-operator */
4859
$sign = $number <=> 0;
4960

5061
return $sign * (int) strrev((string) $number);
5162
}
63+
64+
public static function int2(int $number): int
65+
{
66+
$revers = 0;
67+
68+
$sing = $number <=> 0;
69+
$number = abs($number);
70+
71+
$n = (int) log10($number) + 1;
72+
73+
for ($i = 1; $i <= $n; $i++) {
74+
$pow = pow(10, $n - $i);
75+
$numeral = (int) ($number / $pow);
76+
$number -= $numeral * $pow;
77+
$revers += $numeral * pow(10, $i - 1);
78+
}
79+
80+
return $revers * $sing;
81+
}
5282
}

tests/Reverse/Complete/ReverseCompleteTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,36 @@ public function testCanReverseIntNegativeWithZeros(): void
3939
self::assertSame(-21, ReverseComplete::int(-120));
4040
}
4141

42+
public function testCanReverseIntZero2(): void
43+
{
44+
self::assertSame(0, ReverseComplete::int2(0));
45+
}
46+
47+
public function testCanReverseInt2(): void
48+
{
49+
self::assertSame(21, ReverseComplete::int2(12));
50+
}
51+
52+
public function testCanReverseIntWithThreeDigits2(): void
53+
{
54+
self::assertSame(321, ReverseComplete::int2(123));
55+
}
56+
57+
public function testCanReverseIntZeros2(): void
58+
{
59+
self::assertSame(3, ReverseComplete::int2(300));
60+
}
61+
62+
public function testCanReverseIntNegative2(): void
63+
{
64+
self::assertSame(-21, ReverseComplete::int2(-12));
65+
}
66+
67+
public function testCanReverseIntNegativeWithZeros2(): void
68+
{
69+
self::assertSame(-21, ReverseComplete::int2(-120));
70+
}
71+
4272
public function testCanReverseString1(): void
4373
{
4474
self::assertSame('ytrewq', ReverseComplete::string1('qwerty'));
@@ -78,4 +108,14 @@ public function testCanReverseStringWithWhitespace4(): void
78108
{
79109
self::assertSame('ytr ewq ', ReverseComplete::string4(' qwe rty'));
80110
}
111+
112+
public function testCanReverseString5(): void
113+
{
114+
self::assertSame('ytrewq', ReverseComplete::string5('qwerty'));
115+
}
116+
117+
public function testCanReverseStringWithWhitespace5(): void
118+
{
119+
self::assertSame('ytr ewq ', ReverseComplete::string5(' qwe rty'));
120+
}
81121
}

0 commit comments

Comments
 (0)