var_dump(isValidHexCode("#CD5C5C")); // ➞ true
var_dump(isValidHexCode("#EAECEE")); // ➞ true
var_dump(isValidHexCode("#eaecee")); // ➞ true
var_dump(isValidHexCode("#CD5C58C")); // ➞ false
// Length exceeds 6
var_dump(isValidHexCode("#CD5C5Z")); // ➞ false
// Not all alphabetic characters in A-F
var_dump(isValidHexCode("#CD5C&C")); // ➞ false
// Contains unacceptable character
var_dump(isValidHexCode("CD5C5C")); // ➞ false
// Missing #
function isValidHexCode($code) {
$allowed = '0123456789abcdef';
$len = strlen($code);
if ($len!=7 || $code[0]!='#') {
return false;
}
for ($i=1; $i<$len; $i++) {
if (!str_contains($allowed, strtolower($code[$i]))) {
return false;
}
}
return true;
}
echo correctSpacing_1(" my name is Slim Shady ")."\n";
echo correctSpacing_2(" my name is Giovani Giorgio ")."\n";
//Using explode function
function correctSpacing_1($sentence) {
$array = explode(' ', $sentence);
$count = count($array);
for ($i = 0; $i < $count; $i++) {
if (!$array[$i]) {
unset ($array[$i]);
}
}
return implode(' ', $array);
}
//Using while loop
function correctSpacing_2($sentence) {
$sentence = trim($sentence);
while (str_contains($sentence, ' ')) {
$sentence = str_replace(' ', ' ', $sentence);
}
return $sentence;
}
//An array is special if every even index contains an even number and every odd index contains an odd number.
var_dump(isSpecialArray([2, 7, 4, 9, 6, 1, 6, 3]));//➞ true
var_dump(isSpecialArray([2, 7, 9, 1, 6, 1, 6, 3]));//➞ false
var_dump(isSpecialArray([2, 7, 8, 8, 6, 1, 6, 3]));//➞ false
function isSpecialArray($array){
foreach($array as $key=>$value){
if($key%2+$value%2==1){
return "Array is not special";
}
}
return "Array is special";
}
print_r(splitCode("TEWA8392")); // ➞ ["TEWA", 8392]
print_r(splitCode("MMU778")); // ➞ ["MMU", 778]
print_r(splitCode("SRPE5532")); // ➞ ["SRPE", 5532]
function splitCode($code) {
$alpha = $numbers = '';
$len = strlen($code);
for ($i=0; $i<$len; $i++) {
if(!(47 < ord($code[$i])&&ord($code[$i]) < 58)) {
// if(!is_numeric($code[$i])) {
$alpha.=$code[$i];
} else {
$numbers = substr($code, $i);
break;
}
}
return [$alpha, $numbers];
}
echo rps("Rock", "Paper")."\n";
echo rps("Scissor", "Paper")."\n";
echo rps("Paper", "Paper")."\n";
function rps($p1, $p2){
$res= "";
if ($p1==$p2){
$res="It's a draw";
}else if (
($p1=="Paper" && $p2=="Rock")||
($p1=="Rock" && $p2=="Scissor")||
($p1=="Scissor" && $p2=="Paper")
){
$res="Player 1 wins";
}else {
$res="Player 2 wins";
}
return $res;
}
/*
// a number "n" is automorphic if n^2 ends with "n"
e.g. 5 is automorphic number, because 5^2=25 ends with 5, 6^2=36 and is automorphic as well;
*/
echo "5 - ".isAutomorphic_2(5)."\n"; // 25
echo "8 - ".isAutomorphic_2(8)."\n"; // 64
echo "76 - ".isAutomorphic_2(76)."\n"; // 5,776
function isAutomorphic($num){
$square=$num*$num;
$result=str_ends_with($square, $num);
return $result;
}
//using substring function
function isAutomorphic_2($num){
return substr($num*$num, -1*strlen($num)) === strval($num);
}
echo validatePIN("1234")."\n";
echo validatePIN("12345")."\n";
echo validatePIN("123456")."\n";
echo validatePIN("")."\n";
function validatePIN($pin){
$len=strlen($pin);
if (strlen($pin)!=4 && strlen($pin)!=6){
return "PIN is invalid";
}
for ($i=0; $i<$len; $i++)
$ASCII=ord($pin[$i]);
if (!(47<$ASCII && $ASCII<58)){
return "PIN is invalid";
}
return "PIN is valid";
}