Created
February 22, 2023 16:44
-
-
Save me-shaon/af941bba1351f039e6e2d65e35ed4eb0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Problem 1 | |
function reverse($x) { | |
$x = intval($x); | |
if ($x < 0) { | |
return false; | |
} | |
$reversedInt = 0; | |
$copyX = $x; | |
while ($x > 0) { | |
$remainder = $x % 10; | |
$x = (int)($x/10); | |
$reversedInt = ($reversedInt * 10) + $remainder; | |
} | |
return $copyX === $reversedInt; | |
} | |
// Problem 2 | |
function findTheNum($arr) { | |
$map = []; | |
foreach($arr as $item) { | |
if (!array_key_exists($item, $map)) { | |
$map[$item] = 0; | |
} | |
$map[$item]++; | |
} | |
foreach($map as $key => $value) { | |
if ($value === 1) { | |
return $key; | |
} | |
} | |
} | |
// Problem 3 | |
function exchange($bills) { | |
$notes = [ | |
5 => 0, | |
10 => 0, | |
20 => 0 | |
]; | |
foreach($bills as $bill) { | |
if ($bill === 5) { | |
$notes[5]++; | |
} elseif($bill === 10) { | |
if ($notes[5] >= 1) { | |
$notes[5]--; | |
$notes[10]++; | |
} else { | |
return false; | |
} | |
} elseif($bill === 20) { | |
if ($notes[5] >= 1 && $notes[10] >= 1) { | |
$notes[5]--; | |
$notes[10]--; | |
$notes[20]++; | |
} else { | |
return false; | |
} | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment