Skip to content

Commit 597d0fa

Browse files
committed
Improve code styling and quality
1 parent 743b9e8 commit 597d0fa

19 files changed

+73
-95
lines changed

5 Kyu/directions-reduction.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
*/
4343

4444
function dirReduc($array)
45-
{
46-
$stack = [];
45+
{
46+
$stack = [];
4747
$opposite = array(
4848
"NORTH" => "SOUTH",
4949
"EAST" => "WEST",
@@ -52,10 +52,10 @@ function dirReduc($array)
5252
);
5353
$array_count = count($array);
5454
$index = 0;
55-
while ($index < $array_count){
56-
if($opposite[$array[$index]] == end($stack)) {
55+
while ($index < $array_count) {
56+
if ($opposite[$array[$index]] == end($stack)) {
5757
array_pop($stack);
58-
}
58+
}
5959
array_push($stack, $array[$index]);
6060
$index++;
6161
}
@@ -71,8 +71,8 @@ function altdirReduc($array)
7171
foreach ($array as $value) {
7272
if (end($stack) == $ops[$value]) {
7373
array_pop($stack);
74-
}
75-
$stack[] = $value;
74+
}
75+
$stack[] = $value;
7676
}
7777
return $stack;
7878
}

6 Kyu/find-the-odd-int.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,19 @@
1313
*/
1414
function findIt(array $arr) : int
1515
{
16-
17-
$count = 0;
16+
$count = 0;
1817
$arrCount = count($arr);
19-
for ($i = 0; $i < $arrCount; $i++)
20-
{
21-
22-
for ($j = 0; $j < $arrCount; $j++)
23-
{
24-
if ($arr[$i] == $arr[$j]) {
18+
for ($i = 0; $i < $arrCount; $i++) {
19+
for ($j = 0; $j < $arrCount; $j++) {
20+
if ($arr[$i] == $arr[$j]) {
2521
$count++;
26-
}
27-
}
28-
if ($count % 2 != 0) {
22+
}
23+
}
24+
if ($count % 2 != 0) {
2925
return $arr[$i];
30-
}
31-
}
32-
return -1;
26+
}
27+
}
28+
return -1;
3329
}
3430

3531
// Alternate solution:
@@ -48,5 +44,5 @@ function altfindIt(array $seq) : mixed
4844
return $key;
4945
}
5046
}
51-
return -1;
47+
return -1;
5248
}

6 Kyu/multiples-of-3-or-5.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
function solution($number)
1111
{
1212
$sum = 0;
13-
for ( $i = 3; $i < $number; $i++) {
14-
15-
if ($i % 3 === 0 || $i % 5 === 0) {
13+
for ($i = 3; $i < $number; $i++) {
14+
if ($i % 3 === 0 || $i % 5 === 0) {
1615
$sum += $i;
1716
}
1817
}
@@ -25,9 +24,10 @@ function altSolution($number)
2524
{
2625
return array_sum(
2726
array_filter(
28-
range(1, $number-1), function ($item) {
27+
range(1, $number-1),
28+
function ($item) {
2929
return $item % 3 == 0 || $item % 5 == 0;
3030
}
3131
)
3232
);
33-
}
33+
}

6 Kyu/persistent-bugger.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ function persistence(int $num): int
2727

2828
if ($numArrayCount > 1) {
2929
for ($i = 0; $i < $numArrayCount; $i++) {
30-
$total *= $numArray[$i];
30+
$total *= $numArray[$i];
3131
}
3232
$count++;
3333
if (strlen($total) > 1) {
3434
return $count + persistence($total);
35-
} return $count;
36-
} return $count;
35+
}
36+
return $count;
37+
}
38+
return $count;
3739
}
3840

3941
// Alternate solution - array_product multiplies all values in the array, so we wont need to use a loop
@@ -55,4 +57,4 @@ function altPersistence(int $num): int
5557
}
5658

5759
return $count;
58-
}
60+
}

7 Kyu/descending-order.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919
function descendingOrder(int $number): int
2020
{
21-
$number = (int)$number;
22-
$arrayNumber = str_split($number);
23-
arsort($arrayNumber);
24-
return (int) implode($arrayNumber);
21+
$number = (int)$number;
22+
$arrayNumber = str_split($number);
23+
arsort($arrayNumber);
24+
return (int) implode($arrayNumber);
2525
}

7 Kyu/get-the-middle-character.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,25 @@
2323

2424
function getMiddle($text)
2525
{
26-
2726
$length = strlen($text);
2827
$middle = $length / 2;
2928

30-
if($length % 2 == 0) {
29+
if ($length % 2 == 0) {
3130
$returnedText = $text[$middle-1];
3231
$returnedText.= $text[$middle];
3332
}
3433
$returnedText.= $text[$middle];
3534
return $returnedText;
36-
3735
}
3836

3937

4038
// Alternative method:
4139

42-
function getMiddle($text)
40+
function altgetMiddle($text)
4341
{
4442
$len = strlen($text);
4543
if ($len % 2 === 0) {
4644
return substr($text, $len / 2 - 1, 2);
4745
}
4846
return substr($text, $len / 2, 1);
4947
}
50-
?>

7 Kyu/growth-of-a-population.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
/*
33
In a small town the population is p0 = 1000 at the beginning of a year. The population regularly increases by 2 percent per year and moreover 50 new inhabitants per year come to live in the town. How many years does the town need to see its population greater or equal to p = 1200 inhabitants?
44
5-
At the end of the first year there will be:
5+
At the end of the first year there will be:
66
1000 + 1000 * 0.02 + 50 => 1070 inhabitants
77
8-
At the end of the 2nd year there will be:
8+
At the end of the 2nd year there will be:
99
1070 + 1070 * 0.02 + 50 => 1141 inhabitants (number of inhabitants is an integer)
1010
1111
At the end of the 3rd year there will be:
@@ -32,14 +32,13 @@ function nbYear($startpeople, $percent, $aug, $endpeople)
3232
{
3333
$counter = 0;
3434
for ($counter; $startpeople < $endpeople; $counter++) {
35-
$startpeople = $startpeople * (1 + $percent / 100) + $aug ;
35+
$startpeople = $startpeople * (1 + $percent / 100) + $aug ;
3636
}
37-
return $counter;
37+
return $counter;
3838
}
3939

4040
// Alternate solution with ternary:
41-
function nbYear($startpeople, $percent, $aug, $endpeople)
41+
function altnbYear($startpeople, $percent, $aug, $endpeople)
4242
{
43-
return $startpeople >= $endpeople ? 0 : 1 + nbYear((int)$startpeople * (1+$percent/100) + $aug, $percent, $aug, $endpeople);
43+
return $startpeople >= $endpeople ? 0 : 1 + nbYear((int)$startpeople * (1+$percent/100) + $aug, $percent, $aug, $endpeople);
4444
}
45-
?>

7 Kyu/highest-and-lowest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616
*/
1717

1818
function highAndLow($numbers)
19-
{
19+
{
2020
$splitNumbers = explode(" ", $numbers);
2121
$highestNumber = max($splitNumbers);
2222
$lowestNumber = min($splitNumbers);
23-
return "$highestNumber $lowestNumber";
23+
return "$highestNumber $lowestNumber";
2424
}
2525

2626
// Alternate solution
2727

28-
function highAndLow($numbers)
29-
{
30-
$splitNumbers = explode(" ", $numbers);
31-
return max($splitNumbers) . " " . min($splitNumbers);
28+
function althighAndLow($numbers)
29+
{
30+
$splitNumbers = explode(" ", $numbers);
31+
return max($splitNumbers) . " " . min($splitNumbers);
3232
}
33-
?>

7 Kyu/two-to-one.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@ function longest($a, $b)
2020
$arrayString = str_split($combinedString);
2121
$uniqueString = array_unique($arrayString);
2222
sort($uniqueString);
23-
return implode($uniqueString);
23+
return implode($uniqueString);
2424
}
2525

2626
// Alternate solution
2727

28-
function longest($a, $b)
28+
function altlongest($a, $b)
2929
{
3030
$chars = array_unique(str_split($a . $b));
3131
sort($chars);
3232
return implode('', $chars);
3333
}
34-
35-
?>

7 Kyu/vowel-count.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
function getCount($str)
1111
{
12-
// Use regex to get the count
12+
// Use regex to get the count
1313
//return preg_match_all('/[aeiou]/i', $str, $matches);
1414
return preg_match_all('/[aeiou]/i', $str);
1515
}
16-
?>

0 commit comments

Comments
 (0)