Skip to content

Instantly share code, notes, and snippets.

@me-shaon
Created February 22, 2023 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save me-shaon/af941bba1351f039e6e2d65e35ed4eb0 to your computer and use it in GitHub Desktop.
Save me-shaon/af941bba1351f039e6e2d65e35ed4eb0 to your computer and use it in GitHub Desktop.
<?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