Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
edabit.com challenges php | Hard Level
<?php
function missingLetter($arr)
{
for ($i=ord($arr['0']); $i <= ord($arr[count($arr)-1]); $i++)
if(ord($arr[$i - ord($arr['0'])]) != $i)
return chr($i);
}
echo 'missingLetter(["a", "b", "c", "e", "f", "g"]) : ' . missingLetter(["a", "b", "c", "e", "f", "g"]) . "<br /><br />";
echo 'missingLetter(["O", "Q", "R", "S"]) : ' . missingLetter(["O", "Q", "R", "S"]) . "<br /><br />";
echo 'missingLetter(["t", "u", "v", "w", "x", "z"]) : ' . missingLetter(["t", "u", "v", "w", "x", "z"]) . "<br /><br />";
echo 'missingLetter(["m", "o"]) : ' . missingLetter(["m", "o"]) . "<br /><br />";
function makeBox($number) {
if($number < 0)
return;
elseif($number == 1) {
echo "#";
return;
}
for ($i=0; $i < $number; $i++)
echo "#";
echo "<br />";
if($number > 2) {
for ($i=0; $i < $number - 2; $i++) {
echo "#";
for ($j=0; $j < ($number - 2) * 2; $j++)
echo "&nbsp;";
echo "#" . "<br />";
}
}
for ($i=0; $i < $number; $i++)
echo "#";
}
makeBox(10);
echo "<br /><br />";
makeBox(5);
echo "<br /><br />";
makeBox(3);
echo "<br /><br />";
makeBox(2);
echo "<br /><br />";
makeBox(1);
function unique($arraryNumbers) {
$numbers = array("", 0, "", 0);
$numbers[0] = $arraryNumbers[0];
$numbers[1] = 1;
for ($i=1; $i < count($arraryNumbers); $i++) {
if(empty($numbers[2]) && $numbers[0] != $arraryNumbers[$i]) {
$numbers[2] = $arraryNumbers[$i];
$numbers[3] = 1;
}
if($numbers[0] == $arraryNumbers[$i])
$numbers[1] += 1;
elseif($numbers[2] == $arraryNumbers[$i])
$numbers[3] += 1;
}
if($numbers[1] == 1)
return $numbers[0];
else
return $numbers[2];
}
echo "unique([3, 3, 3, 7, 3, 3]) : " . unique([3, 3, 3, 7, 3, 3]) . "<br /><br />";
echo "unique([0, 0, 0.77, 0, 0]) : " . unique([0, 0, 0.77, 0, 0]) . "<br /><br />";
echo "unique([0, 1, 1, 1, 1, 1, 1, 1]) : " . unique([0, 1, 1, 1, 1, 1, 1, 1]) . "<br /><br />";
function getPrices($stringArray) {
$output = array();
for ($i=0; $i < count($stringArray); $i++) {
$stringArraySeprate = str_split($stringArray[$i]);
$fetchFlag = false;
$output[$i] = "";
for ($j=0; $j < strlen($stringArray[$i]); $j++) {
if($fetchFlag == true && $stringArraySeprate[$j] != ")")
$output[$i] .= $stringArraySeprate[$j];
elseif($fetchFlag == true && $stringArraySeprate[$j] == ")")
break;
if($stringArraySeprate[$j] == "$")
$fetchFlag = true;
}
}
return $output;
}
print_r(getPrices(["ice cream ($5.99)", "banana ($0.20)", "sandwich ($8.50)", "soup ($1.99)"]));
echo "<br /><br />";
print_r(getPrices(["salad ($4.99)"]));
function longestWord($myString) {
$arrayMyString = explode(" ", $myString);
$maxString = "";
for ($i=0; $i < count($arrayMyString); $i++)
if(strlen($maxString) < strlen($arrayMyString[$i]))
$maxString = $arrayMyString[$i];
return $maxString;
}
echo 'longestWord("Hello darkness my old friend") : ' . longestWord("Hello darkness my old friend") . "<br /></br />";
echo 'longestWord("Hello there mate") : ' . longestWord("Hello there mate") . "<br /></br />";
echo 'longestWord("Kayla!s toy is plastic") : ' . longestWord("Kayla's toy is plastic") . "<br /></br />";
function sumDigitWithString($number) {
$stringNumber = str_split($number);
$sum = 0;
for ($i=0; $i < count($stringNumber); $i++)
$sum += $stringNumber[$i];
return $sum;
}
function sumDigit($number) {
$sum = 0;
while ($number > 9) {
$temp = $number % 10;
$sum += $temp;
$number -= $temp;
$number /= 10;
}
$sum += $number;
return $sum;
}
echo "sumDigit(111) : " . sumDigit(111) . "<br />";
echo "sumDigit(222) : " . sumDigit(222) . "<br />";
echo "sumDigit(333) : " . sumDigit(333) . "<br />";
function primeCount($lowBound, $highBound) {
if($lowBound < 2)
$lowBound = 2;
$count = 0;
for ($i=$lowBound; $i <= $highBound; $i++) {
$counter = 0;
for ($j=2; $j < $i - 1; $j++) {
if($i % $j == 0)
$counter++;
}
if($counter == 0)
$count++;
}
return $count;
}
echo "primeCount(1, 10) : " . primeCount(1, 10) . "<br /><br />";
echo "primeCount(1, 100) : " . primeCount(1, 100) . "<br /><br />";
echo "primeCount(1, 1000) : " . primeCount(1, 1000) . "<br /><br />";
function primesBelowNum($highBound) {
$lowBound = 2;
$prIndex = 0;
$output = array();
for ($i=$lowBound; $i <= $highBound; $i++) {
$counter = 0;
for ($j=2; $j < $i - 1; $j++) {
if($i % $j == 0)
$counter++;
}
if($counter == 0) {
$output[$prIndex] = $i;
$prIndex += 1;
}
}
return $output;
}
echo "primesBelowNum(5) : ";
print_r(primesBelowNum(5));
echo "<br /><br />";
echo "primesBelowNum(10) : ";
print_r(primesBelowNum(10));
echo "<br /><br />";
echo "primesBelowNum(20) : ";
print_r(primesBelowNum(20));
echo "<br /><br />";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment