Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
edabit php challenges | easy level
<?php
function largestSwap($number) {
if($number < 10 || $number > 99)
return;
$string = str_split($number);
$newNumber = $string[1] . $string[0];
if($number >= $newNumber)
return "true";
else
return "false";
}
echo "largestSwap(27) : " . largestSwap(27) . "<br />";
echo "largestSwap(43) : " . largestSwap(43) . "<br />";
echo "largestSwap(14) : " . largestSwap(14) . "<br />";
echo "largestSwap(53) : " . largestSwap(53) . "<br />";
echo "largestSwap(99) : " . largestSwap(99) . "<br />";
function capMe($arr) {
$output = array();
$counter = 0;
foreach ($arr as $key => $value) {
$temp1 = str_split($value);
$temp2 = strtoupper($temp1[0]);
for($i = 1; $i < strlen($value); $i++)
$temp2[$i] = strtolower($temp1[$i]);
$output[$counter] = json_encode($temp2);
$counter++;
}
return $output;
}
print_r(capMe(["mavis", "senaida", "letty"]));
echo "<br /><br />";
print_r(capMe(["samuel", "MABELLE", "letitia", "meridith"]));
echo "<br /><br />";
print_r(capMe(["Slyvia", "Kristal", "Sharilyn", "Calista"]));
echo "<br /><br />";
function highLow($numberString) {
$numbers = explode(" ", $numberString);
$min = $numbers[0];
$max = $numbers[0];
for ($i=1; $i < count($numbers); $i++) {
if($numbers[$i] > $max)
$max = $numbers[$i];
elseif ($numbers[$i] < $min)
$min = $numbers[$i];
}
return $max . " " . $min;
}
echo 'highLow("1 2 3 4 5") : ' . highLow("1 2 3 4 5") . "<br />";
echo 'highLow("1 2 -3 4 5") : ' . highLow("1 2 -3 4 5") . "<br />";
echo 'highLow("1 9 3 4 -5") : ' . highLow("1 9 3 4 -5") . "<br />";
echo 'highLow("13") : ' . highLow("13") . "<br />";
function sortNumsAscending($arr) {
if(is_null($arr))
return [];
$tempArr = $arr;
$count = count($arr);
for ($i=0; $i < $count ; $i++) {
for ($j=$i; $j < $count; $j++) {
if($tempArr[$i] > $tempArr[$j]) {
$temp = $tempArr[$i];
$tempArr[$i] = $tempArr[$j];
$tempArr[$j] = $temp;
}
}
}
return $tempArr;
}
print_r(sortNumsAscending([1, 2, 10, 50, 5]));
echo "<br /><br />";
print_r(sortNumsAscending([80, 29, 4, -95, -24, 85]));
echo "<br /><br />";
print_r(sortNumsAscending(null));
echo "<br /><br />";
print_r(sortNumsAscending([]));
echo "<br /><br />";
function factorial($number) {
$sum = 1;
for ($i=1; $i <= $number; $i++) {
$sum *= $i;
}
return $sum;
}
echo "factorial(3) : " . factorial(3) . "<br />";
echo "factorial(5) : " . factorial(5) . "<br />";
echo "factorial(13) : " . factorial(13) . "<br />";
function evenOddPartition($arr) {
$output = array();
$output[0] = array();
$output[1] = array();
$oddcounter = 0;
$evencounter = 0;
$count = count($arr);
for ($i=0; $i < $count; $i++) {
if($arr[$i] == 0 || $arr[$i] % 2 == 0) {
$output[0][$evencounter] = $arr[$i];
$evencounter++;
}
else {
$output[1][$oddcounter] = $arr[$i];
$oddcounter++;
}
}
return $output;
}
echo "<pre />";
print_r(evenOddPartition([5, 8, 9, 2, 0]));
echo "<br /><br />";
print_r(evenOddPartition([1, 0, 1, 0, 1, 0]));
echo "<br /><br />";
print_r(evenOddPartition([1, 3, 5, 7, 9]));
echo "<br /><br />";
print_r(evenOddPartition([]));
echo "<br /><br />";
function hasSpaces($string) {
$temp = str_split($string);
for ($i=0; $i < strlen($string); $i++)
if($temp[$i] === " ")
return "true";
return "false";
}
echo 'hasSpaces("hello") : ' . hasSpaces("hello") . "<br/>";
echo 'hasSpaces("hello, world") : ' . hasSpaces("hello, world") . "<br/>";
echo 'hasSpaces(" ") : ' . hasSpaces(" ") . "<br/>";
echo 'hasSpaces("") : ' . hasSpaces("") . "<br/>";
echo 'hasSpaces(",./!@#") : ' . hasSpaces(",./!@#") . "<br/>";
function missingNum($numbers) {
$sorted = sortNumsAscending($numbers);
for ($i=0; $i < count($numbers); $i++)
if($i + 1 != $sorted[$i])
return $i + 1;
return 10;
}
echo "missingNum([1, 2, 3, 4, 6, 7, 8, 9, 10]) : " . missingNum([1, 2, 3, 4, 6, 7, 8, 9, 10]). "<br />";
echo "missingNum([7, 2, 3, 6, 5, 9, 1, 4, 8]) : " . missingNum([7, 2, 3, 6, 5, 9, 1, 4, 8]). "<br />";
echo "missingNum([10, 5, 1, 2, 4, 6, 8, 3, 9]) : " . missingNum([10, 5, 1, 2, 4, 6, 8, 3, 9]). "<br />";
function doubleChar($string) {
$output = "";
$temp = str_split($string);
for ($i=0; $i < strlen($string); $i++)
$output .= $temp[$i] . $temp[$i];
return $output;
}
echo 'doubleChar("String") : ' . doubleChar("String") . "<br />";
echo 'doubleChar("Hello World!") : ' . doubleChar("Hello World!") . "<br />";
echo 'doubleChar("1234!_ ") : ' . doubleChar("1234!_ ") . "<br />";
function societyName($arr) {
$output = "";
foreach ($arr as $key => $value) {
$temp = str_split($value);
$output .= $temp[0];
}
return $output;
}
echo 'societyName(["Adam", "Sarah", "Malcolm"]) : ' . societyName(["Adam", "Sarah", "Malcolm"]) . "<br /><br />";
echo 'societyName(["Harry", "Newt", "Luna", "Cho"]) : ' . societyName(["Harry", "Newt", "Luna", "Cho"]) . "<br /><br />";
echo 'societyName(["Phoebe", "Chandler", "Rachel", "Ross", "Monica", "Joey"]) : ' . societyName(["Phoebe", "Chandler", "Rachel", "Ross", "Monica", "Joey"]) . "<br /><br />";
function removeDups($arr) {
$output = array();
$unique = true;
$counter = 0;
for ($i=0; $i < count($arr); $i++) {
$unique = true;
for ($j=0; $j < $i; $j++) {
if($i == $j)
continue;
if($arr[$i] === $arr[$j])
$unique = false;
}
if($unique) {
$output[$counter] = $arr[$i];
$counter++;
}
}
return $output;
}
print_r(removeDups([1, 0, 1, 0]));
echo "<br /><br />";
print_r(removeDups(["The", "big", "cat"]));
echo "<br /><br />";
print_r(removeDups(["John", "Taylor", "John"]));
echo "<br /><br />";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment