Created
November 26, 2018 21:24
-
-
Save jetonr/d00584ed05f19ef24aa3ab2738dc53a4 to your computer and use it in GitHub Desktop.
PHP Simple Algorithms - Coderbyte.com
This file contains hidden or 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 | |
function NumberEncoding( $str ) { | |
$albphabet = range('a','z'); | |
$arrStr = str_split( strtolower( $str ) ); | |
$newArr = array(); | |
foreach( $arrStr as $key => $value ) { | |
if ( in_array( $value, $albphabet ) ) { | |
$newArr[] = array_search( $value, $albphabet ) + 1; | |
} else { | |
$newArr[] = $value; | |
} | |
} | |
return implode($newArr); | |
} | |
echo NumberEncoding('jaj-a'); | |
function SimpleMode($arr) { | |
$shown_number = array(); | |
foreach ( $arr as $number ) { | |
if ( isset($shown_number[$number] ) ) { | |
$shown_number[$number]++; | |
} else { | |
$shown_number[$number] = 1; | |
} | |
} | |
if ( max($shown_number) <= 1 ) { | |
return -1; | |
} else { | |
return array_keys( $shown_number, max($shown_number) )[0]; | |
} | |
} | |
function CountingMinutesI($str) { | |
$array = explode( '-', $str ); | |
$time1 = minute_of_the_day( $array[0] ); | |
$time2 = minute_of_the_day( $array[1] ); | |
if ( $time2 < $time1 ) { | |
$min_span = $time2 - $time1 + 1440; | |
} else { | |
$min_span = $time2 - $time1; | |
} | |
return $min_span; | |
} | |
function minute_of_the_day( $time ) { | |
$am_pm = strtolower(substr( $time, -2 )); | |
$hour = substr( $time, 0, strpos($time, ':') ); | |
$mins = substr( $time, strpos( $time, ':' ) + 1, 2 ); | |
$min_of_day = ''; | |
if ( $am_pm == 'am' && $hour == 12 && $mins == 00 ) { | |
$min_of_day = 1440; | |
} | |
if ( $am_pm == 'am' && $hour < 12 ) { | |
$min_of_day = ( $hour * 60 ) + $mins; | |
} | |
if ( $am_pm == 'pm' ) { | |
if ( $hour < 12 ) { | |
$min_of_day = ( ( $hour + 12 ) * 60 ) + $mins; | |
} else { | |
$min_of_day = ( $hour * 60 ) + $mins; | |
} | |
} | |
return $min_of_day; | |
} | |
function VowelCount( $str ) { | |
$words_array = str_split($str); | |
$vovels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); | |
$counter = 0; | |
foreach ( $words_array as $letter ) { | |
if ( in_array( $letter, $vovels ) ) { | |
$counter++; | |
} | |
} | |
return $counter; | |
} | |
function ExOh( $str ) { | |
$x = substr_count($str, 'x'); | |
$o = substr_count($str, 'o'); | |
return ( $x != $o ) ? 'false' : 'true'; | |
} | |
function SwapCase($str) { | |
$words_array = str_split( $str ); | |
$swapedcase = ''; | |
foreach ( $words_array as $word ) { | |
if ( true == ctype_upper($word) ) { | |
$swapedcase .= strtolower($word); | |
} else { | |
$swapedcase .= strtoupper($word); | |
} | |
} | |
return $swapedcase; | |
} | |
function DashInsert($num) { | |
$numbers = str_split( $num ); | |
$last_odd = array(); | |
$hyphend = ''; | |
for ( $i=0; $i < count($numbers); $i++ ) { | |
if ( $numbers[$i] % 2 != 0 ) { | |
if ( isset( $last_odd[$i-1] ) && $last_odd[$i-1] == 'odd' ) { | |
$numbers[$i] = '-' . $numbers[$i]; | |
} | |
$last_odd[$i] = 'odd'; | |
} | |
$hyphend .= $numbers[$i]; | |
} | |
return $hyphend; | |
} | |
function ArithGeo( $arr ) { | |
$result = array(); | |
$last_number = ''; | |
for ( $i=0; $i < count($arr); $i++ ) { | |
if ( $arr[$i] == $arr[0] ) { | |
$result['a'] = 1; | |
$result['g'] = 1; | |
$last_number = $arr[$i]; | |
continue; | |
} | |
if ( $last_number + ( $arr[1] - $arr[0] ) == $arr[$i] ) { | |
$result['a']++; | |
} | |
if ( $last_number * ( $arr[1] / $arr[0] ) == $arr[$i] ) { | |
$result['g']++; | |
} | |
$last_number = $arr[$i]; | |
} | |
if ( $result['g'] == count($arr) ) { | |
return 'Geometric'; | |
} elseif ( $result['a'] == count($arr) ) { | |
return 'Arithmetic'; | |
} else { | |
return -1; | |
} | |
} | |
function ArrayAdditionI($arr) { | |
$highest_value = max($arr); | |
$high_key = array_keys( $arr, $highest_value ); | |
unset( $arr[$high_key[0]] ); | |
$total_left = array_sum($arr); | |
$arr = array_values($arr); | |
if ( $highest_value > $total_left ) { | |
return 'false'; | |
} | |
$new_num_array = array(); | |
$i = 0; | |
foreach ( $arr as $num ) { | |
$v = $arr[$i]; | |
unset($arr[$i]); | |
$arr[$i] = $v; | |
$new_num_array[$num] = $arr; | |
$i++; | |
} | |
foreach ( $new_num_array as $variations ) { | |
$total_value = 0; | |
foreach ( $variations as $variation ) { | |
if ( $total_value + $variation > $highest_value ) { | |
continue; | |
} | |
$total_value += $variation; | |
if ( $total_value == $highest_value ) { | |
return 'true'; | |
} | |
} | |
} | |
return 'false'; | |
} | |
function ThirdGreatest_not( $strArr ) { | |
$sorter = array(); | |
foreach ($strArr as $key => $row) { | |
$sorter[$key] = strlen($row); | |
} | |
array_multisort( array_values($sorter), SORT_DESC, array_keys($sorter), SORT_ASC, $strArr ); | |
return $strArr[2]; | |
} | |
function PrimeTime($num) { | |
for($i = 2; $i < $num; $i++) { | |
if ( $num % $i == 0 ) { | |
return 'false'; | |
} | |
} | |
return 'true'; | |
} | |
function PrimeChecker($num) { | |
$numArr = AllPermutations(str_split($num)); | |
for( $i=0; $i < count($numArr); $i++ ){ | |
if ( isPrime( implode($numArr[$i]) ) ) { | |
return 1; | |
} | |
} | |
return 0; | |
} | |
function AllPermutations($InArray, $InProcessedArray = array()) { | |
$ReturnArray = array(); | |
foreach($InArray as $Key=>$value) { | |
$CopyArray = $InProcessedArray; | |
$CopyArray[$Key] = $value; | |
$TempArray = array_diff_key($InArray, $CopyArray); | |
if (count($TempArray) == 0) { | |
$ReturnArray[] = $CopyArray; | |
} else { | |
$ReturnArray = array_merge($ReturnArray, AllPermutations($TempArray, $CopyArray)); | |
} | |
} | |
return $ReturnArray; | |
} | |
function isPrime($num) { | |
for( $i = 2; $i < $num; $i++ ) { | |
if ( $num % $i == 0 ) { | |
return false; | |
} | |
} | |
return true; | |
} | |
function PrimeRange($num) { | |
$range = range( 1, pow( 10, 4 ) ); | |
$primes = array(); | |
foreach ( $range as $nr ) { | |
if ( isPrime( $nr ) ) { | |
$primes[] = $nr; | |
} | |
} | |
return $primes[$num]; | |
} | |
function AlphabetSoup($str) { | |
$alphabetLow = range('a','z'); | |
$letterMap = array(); | |
foreach ( str_split($str) as $letter ) { | |
$arr_key = array_search( $letter, $alphabetLow ); | |
if ( isset($arr_key) && false !== $arr_key ) { | |
if ( isset( $letterMap[$arr_key] ) ) { | |
$letterMap[$arr_key] = $letterMap[$arr_key].$letter; | |
} else { | |
$letterMap[$arr_key] = $letter; | |
} | |
} | |
} | |
$newString = ''; | |
while ( $letterMap ) { | |
$min = min(array_keys($letterMap)); | |
$newString .= $letterMap[$min]; | |
unset($letterMap[$min]); | |
} | |
return $newString; | |
} | |
function BinaryConverter( $str ) { | |
$strArr = array_reverse(str_split($str)); | |
$binarytoDec = ''; | |
for ( $i=0; $i < count($strArr); $i++ ) { | |
$binarytoDec += $strArr[$i] * pow(2, $i); | |
} | |
return $binarytoDec; | |
} | |
function ThirdGreatest($strArr) { | |
$sorter = array(); | |
foreach ( $strArr as $word ) { | |
$length = strlen($word); | |
$sorter[$length][] = $word; | |
} | |
$max = 0; | |
$count = 0; | |
for ( $i=0; $i < count($strArr); $i++ ) { | |
$max = max(array_keys($sorter)); | |
foreach ($sorter[$max] as $word) { | |
$count++; | |
if ($count == 3) { | |
return $word; | |
} | |
} | |
unset($sorter[$max]); | |
} | |
} | |
function DivisionStringified($num1,$num2) { | |
$number = round( ( $num1/$num2 ), 0 ); | |
if ( strlen($number) > 3 ) { | |
$number = number_format($number, 0, '', ','); | |
return "$number"; | |
} | |
return "$number"; | |
} | |
function ThreeFiveMultiples($num) { | |
$numbers = array(); | |
for ( $i=3; $i < $num; $i++ ) { | |
if ( $i % 3 == 0 ) { | |
$numbers[] = $i; | |
} elseif ( $i % 5 == 0 ) { | |
$numbers[] = $i; | |
} | |
} | |
return array_sum($numbers); | |
} | |
function AdditivePersistence($num) { | |
$counter = 0; | |
while ( $num > 9 ) { | |
$num = array_sum(str_split($num)); | |
$counter++; | |
} | |
return $counter; | |
} | |
function MultiplicativePersistence($num) { | |
$counter = 0; | |
while ( $num > 9 ) { | |
$num = array_product(str_split($num)); | |
$counter++; | |
} | |
return $counter; | |
} | |
function StringScramble($str1,$str2) { | |
$str1 = str_split($str1); | |
$str2 = str_split($str2); | |
$counter = 0; | |
foreach ( $str2 as $letter ) { | |
if ( in_array($letter, $str1) ){ | |
$counter++; | |
$checked_letter = array_search($letter, $str1); | |
unset($str1[$checked_letter]); | |
} | |
} | |
if ( $counter == count($str2) ) { | |
return 'true'; | |
} | |
return 'false'; | |
} | |
function OverlappingRectangles($strArr) { | |
$strArr = preg_split('/(?<=\)),/', $strArr[0] ); | |
$strArr = preg_replace('/\(|\)/', '', $strArr); | |
$strArrA = array_slice($strArr, 0, 4); | |
$strArrB = array_slice($strArr, 4, 4); | |
$rectA = array(); | |
$rectB = array(); | |
for ( $i = 0; $i < 4; $i++ ) { | |
$rectA[] = explode( ',' , $strArrA[$i] ); | |
$rectB[] = explode( ',' , $strArrB[$i] ); | |
} | |
for ( $i = 0; $i < 4; $i++ ) { | |
$rectA['x'][] = $rectA[$i][0]; | |
$rectA['y'][] = $rectA[$i][1]; | |
$rectB['x'][] = $rectB[$i][0]; | |
$rectB['y'][] = $rectB[$i][1]; | |
unset($rectA[$i]); | |
unset($rectB[$i]); | |
} | |
//Rectangle A min-max xy | |
$minRectAx = min($rectA['x']); | |
$maxRectAx = max($rectA['x']); | |
$minRectAy = min($rectA['y'] ); | |
$maxRectAy = max($rectA['y'] ); | |
//Rectangle B min-max xy | |
$minRectBx = min($rectB['x']); | |
$maxRectBx = max($rectB['x']); | |
$minRectBy = min($rectB['y']); | |
$maxRectBy = max($rectB['y']); | |
// Get min max values for both rectangles | |
$maxABx = max( $minRectAx, $minRectBx ); | |
$maxABy = max( $minRectAy, $minRectBy ); | |
$minABx = min( $maxRectAx, $maxRectBx ); | |
$minABy = min( $maxRectAy, $maxRectBy ); | |
if ( $maxABx - $minABx == 0 || $maxABy - $minABy == 0 ) { | |
return 0; | |
} | |
$RectAwidth = abs($maxRectAx - $minRectAx); | |
$RectAheight = abs($maxRectAy - $minRectAy); | |
$rectAtotal = $RectAwidth * $RectAheight; | |
$overlapWith = abs($minABx - $maxABx); | |
$overlapHeight = abs($minABy - $maxABy); | |
$overlapTotal = $overlapWith * $overlapHeight; | |
if( ( $overlapWith / 2) < $overlapHeight || ($RectAheight) < $overlapHeight) { | |
$overlapTotal+1; | |
} | |
return round( $rectAtotal/$overlapTotal ); | |
} | |
function TripleDouble( $num1, $num2 ) { | |
$num1 = array_count_values(str_split($num1)); | |
$num2 = array_count_values(str_split($num2)); | |
$num1_key = array_keys( $num1, max($num1) ); | |
for ( $i=0; $i < count($num1); $i++) { | |
if ( isset( $num1[$num1_key[$i]] ) && ( $num1[$num1_key[$i]] >= 3 ) ) { | |
if ( isset($num2[$num1_key[$i]] ) && $num2[$num1_key[$i]] >= 2 ) { | |
return 1; | |
} | |
} else { | |
unset($num1[$num1_key[$i]]); | |
} | |
} | |
return 0; | |
} | |
function RunLength($str) { | |
$strArr = str_split($str); | |
$last_letter = ''; | |
$new_letter = ''; | |
$old_key = ''; | |
$newArr = array(); | |
$counter = 0; | |
for ( $i= 0; $i < count($strArr); $i++ ) { | |
$new_letter = $strArr[$i]; | |
if ( isset( $newArr[$old_key] ) && $last_letter == $new_letter ) { | |
$counter++; | |
$newArr[$old_key][$new_letter] = $counter; | |
} elseif ( $last_letter != $new_letter ) { | |
$counter = 1; | |
$newArr[$i][$new_letter] = $counter; | |
$old_key = $i; | |
} | |
$last_letter = $new_letter; | |
} | |
$encrypted = ''; | |
foreach ( $newArr as $repeat ) { | |
foreach ($repeat as $letter => $count ) { | |
$encrypted .= $count.$letter; | |
} | |
} | |
return $encrypted; | |
} | |
function Consecutive($arr) { | |
$max = max($arr); | |
$min = min($arr); | |
$range = range($min, $max); | |
return count($range) - count($arr); | |
} | |
function FormattedDivision($num1,$num2) { | |
$number = round( ( $num1/$num2 ), 4 ); | |
$number = number_format($number, 4, '.', ','); | |
return "$number"; | |
} | |
function DashInsertII($num) { | |
$numbers = str_split( $num ); | |
$last_odd = array(); | |
$last_even = array(); | |
$hyphend = ''; | |
for ( $i=0; $i < count($numbers); $i++ ) { | |
if ( $numbers[$i] % 2 != 0 && $numbers[$i] != 0 ) { | |
if ( isset( $last_odd[$i-1] ) && $last_odd[$i-1] == 'odd' ) { | |
$numbers[$i] = '-' . $numbers[$i]; | |
} | |
$last_odd[$i] = 'odd'; | |
} | |
if ( $numbers[$i] % 2 == 0 && $numbers[$i] != 0 ) { | |
if ( isset( $last_even[$i-1] ) && $last_even[$i-1] == 'even' ) { | |
$numbers[$i] = '*' . $numbers[$i]; | |
} | |
$last_even[$i] = 'even'; | |
} | |
$hyphend .= $numbers[$i]; | |
} | |
return $hyphend; | |
} | |
function MeanMode($arr) { | |
$mean = array_sum($arr) / count($arr); | |
$mode = array_count_values($arr); | |
$mode = array_search(max($mode), $mode); | |
if ( $mean == $mode ) { | |
return '1'; | |
} | |
return '0'; | |
} | |
function ABCheck($str) { | |
if ( preg_match( '/a.{3,3}b/', $str) ) { | |
return 'true'; | |
} | |
return 'false'; | |
} | |
function LetterCountI($str) { | |
$str = explode( ' ', $str); | |
$LetterCount = array(); | |
for ( $i=0; $i < count($str); $i++) { | |
$letters = str_split($str[$i]); | |
$LetterCount[$str[$i]] = array_count_values($letters); | |
} | |
foreach ( $LetterCount as $word => $letters ) { | |
foreach (array_keys($letters, 1, true ) as $key) { | |
unset($LetterCount[$word][$key]); | |
} | |
} | |
$LetterCount = array_filter($LetterCount); | |
if ( $LetterCount ) { | |
$word = array_keys( $LetterCount, max($LetterCount) ); | |
return $word[0]; | |
} else { | |
return -1; | |
} | |
} | |
function factor_array($number) { | |
$factors = array(); | |
for ( $i=1; $i <= $number; $i++ ) { | |
if ( $number % $i == 0 ) { | |
$factors[] = $i; | |
} | |
} | |
return $factors; | |
} | |
function Division($num1,$num2) { | |
$num1 = factor_array($num1); | |
$num2 = factor_array($num2); | |
$num1 = array_reverse($num1); | |
$num2 = array_reverse($num2); | |
foreach ( $num1 as $number ) { | |
if ( in_array( $number, $num2 ) ) { | |
return $number; | |
} | |
} | |
} | |
function SecondGreatLow($arr) { | |
$arr = array_unique($arr); | |
$max = $arr; | |
$min = $arr; | |
$max_val = max($max); | |
$min_val = min($min); | |
if ( 1 == count($arr) ) { | |
return $min_val . ' ' . $max_val; | |
} | |
$max_key = array_search($max_val, $max); | |
unset($max[$max_key]); | |
$min_key = array_search($min_val, $min); | |
unset($min[$min_key]); | |
$max_val = max($max); | |
$min_val = min($min); | |
return $min_val . ' ' . $max_val; | |
} | |
function DistinctList($arr) { | |
return count($arr) - count(array_unique($arr) ); | |
} | |
function LongestWord($sen) { | |
$sen = preg_replace('/[^a-z|A-Z1-9\s]/', '', $sen); | |
$sen = explode( ' ' , $sen ); | |
$wordCounter = array(); | |
foreach ( $sen as $word ) { | |
$wordCounter[$word] = strlen($word); | |
} | |
return array_search( max($wordCounter), $wordCounter ); | |
} | |
function Palindrome($str) { | |
$str = preg_replace('/[^a-z|A-Z|1-9]/', '', $str); | |
$reverse = strrev($str); | |
if ($reverse === $str) { | |
return 'true'; | |
} | |
return 'false'; | |
} | |
function PalindromeTwo($str) { | |
$str = preg_replace('/[^a-z|A-Z|1-9]/', '', strtolower($str)); | |
$reverse = strrev(strtolower($str)); | |
if ($reverse == $str) { | |
return 'true'; | |
} | |
return 'false'; | |
} | |
function TimeConvert($num) { | |
$hours = floor($num / 60); | |
$minutes = ($num % 60); | |
return $hours.':'. $minutes; | |
} | |
function SimpleSymbols($str) { | |
$letters = preg_replace("/[^a-z|A-Z]/", "", $str); | |
$letters = str_split($letters); | |
$counter = 0; | |
foreach ( $letters as $letter ) { | |
if ( preg_match( '/\+'.$letter.'\+/', $str ) ) { | |
$counter++; | |
} | |
} | |
if ( $counter != count($letters) ) { | |
return 'false'; | |
} | |
return 'true'; | |
} | |
function FibonacciChecker($num) { | |
$x = 0; | |
$y = 1; | |
$z = 0; | |
$fibonaci = array(); | |
while( $z < $num ) { | |
$z = $x + $y; | |
$x = $y; | |
$y = $z; | |
$fibonaci[] = $z; | |
} | |
if (in_array( $num, $fibonaci )) { | |
return 'yes'; | |
} | |
return 'no'; | |
} | |
function NumberSearch( $str ) { | |
$numbers = preg_split( '/[^\d]/', $str ); // Use PREG_SPLIT_NO_EMPTY flag if supported | |
$numbers = array_sum(array_filter( $numbers )); // Unnecessary if flags are supported for preg_split() | |
$letters = strlen( preg_replace( '/[^a-z|A-Z]/', '', $str ) ); | |
return (int) round( $numbers / $letters, 0 ); | |
} | |
function MostFreeTime($strArr) { | |
$event_times = array(); | |
for ( $i=0; $i < count($strArr); $i++ ) { | |
$times = explode( '-', $strArr[$i] ); | |
$time1 = minute_of_the_day( $times[0] ); | |
$time2 = minute_of_the_day( $times[1] ); | |
$event_times[$i]['start'] = $time1; | |
$event_times[$i]['end'] = $time2; | |
$freeTimes = array(); | |
$events_count = count($event_times); | |
for ( $i=0; $i < $events_count-1; $i++ ) { | |
$arr_key = array_search(min($event_times), $event_times); | |
$end_time = $event_times[$arr_key]['end']; | |
unset($event_times[$arr_key]); | |
$arr_key = array_search(min($event_times), $event_times); | |
$start_time = $event_times[$arr_key]['start']; | |
$freeTimes[] = $start_time - $end_time; | |
} | |
return date('H:i', mktime(0, max($freeTimes))); | |
} | |
} | |
function LetterChanges($str) { | |
$alphabetLow = range('a','z'); | |
$vovels = array("a", "e", "i", "o", "u"); | |
$letterChanges = array(); | |
foreach ( str_split($str) as $letter ) { | |
$arr_key = array_search( $letter, $alphabetLow ); | |
if ( isset($arr_key) && false !== $arr_key ) { | |
$letter = ( $letter == 'z' ) ? $alphabetLow[$arr_key-25] : $alphabetLow[$arr_key+1]; | |
$letter = ( in_array( $letter, $vovels) ? strtoupper($letter) : $letter ); | |
} | |
$letterChanges[] = $letter; | |
} | |
return implode($letterChanges); | |
} | |
function BracketMatcher($str) { | |
$bracketClose = strlen( preg_replace( '#[^)]#', '', $str ) ); | |
$bracketOpen = strlen( preg_replace( '#[^(]#', '', $str ) ); | |
return $bracketOpen - $bracketClose == 0 ? 1 : 0; | |
} | |
function PermutationStep($num) { | |
$numArr = str_split($num); | |
$arrCount = count($numArr); | |
$highestFound = false; | |
$numArr = array_reverse($numArr); | |
for ( $i=0; $i < $arrCount; $i++ ) { | |
if( isset($numArr[$i+1]) && $numArr[$i] > $numArr[$i+1] && $highestFound == false ) { | |
$highest_found = $numArr[$i+1]; | |
$numArr[$i+1] = $numArr[$i]; | |
$numArr[$i] = $highest_found; | |
$highestFound = true; | |
$left_side = array_slice( array_reverse($numArr), 0, $i+2 ); | |
$right_side = array_slice($numArr, 0, $arrCount - count($left_side) ); | |
if ($i != 0 ) { | |
for ( $i = 0; $i < count( $right_side ); $i++ ) { | |
if( isset($right_side[$i+1]) && $right_side[$i] < $right_side[$i+1] ) { | |
$lowest = $right_side[$i+1]; | |
$right_side[$i+1] = $right_side[$i]; | |
$right_side[$i] = $lowest; | |
} | |
} | |
} | |
$left_side = array_reverse($left_side); | |
} | |
} | |
if ( isset($right_side) && isset($left_side ) ) { | |
$numArr = array_reverse(array_merge( $right_side, $left_side )); | |
if ( is_array($numArr) && !empty($numArr ) ) { | |
return implode($numArr); | |
} | |
} | |
return -1; | |
} | |
function OffLineMinimum($strArr) { | |
$newArr = array(); | |
$lowest = array(); | |
foreach ( $strArr as $number ) { | |
if ( $number === 'E' && !empty( $newArr ) ) { | |
$min_value = min($newArr); | |
$lowest[] = $min_value; | |
$min_key = array_keys( $newArr, $min_value ); | |
unset( $newArr[$min_key[0]] ); | |
} | |
if ( $number !== 'E' ) { | |
$newArr[] = $number; | |
} | |
} | |
return implode( ',', $lowest ); | |
} | |
function SwapII($str) { | |
$strArr = str_split($str); | |
$upstr = ''; | |
foreach ( $strArr as $character ) { | |
if ( ctype_upper( $character ) ) { | |
$upstr .= strtolower($character); | |
} elseif ( ctype_lower($character ) ) { | |
$upstr .= strtoupper($character); | |
} else { | |
$upstr .= strtoupper($character); | |
} | |
} | |
$result = array(); | |
$newArr = explode( ' ', $upstr ); | |
foreach ( $newArr as $word ) { | |
if( preg_match( "/\d+[a-zA-Z]+\d+/", $word ) == true ){ | |
$wordArr = str_split($word); | |
$firstNumber = false; | |
for ( $i=0; $i < count($wordArr); $i++ ) { | |
if ( ctype_digit( $wordArr[$i] ) ) { | |
if ( $firstNumber === false ){ | |
$number1 = $wordArr[$i]; | |
$firstNumber = true; | |
$arr_key = $i; | |
} | |
if ( $firstNumber !== false ) { | |
$number2 = $wordArr[$i]; | |
$wordArr[$i] = $number1; | |
$wordArr[$arr_key] = $number2; | |
} | |
} | |
} | |
$result[] = implode($wordArr); | |
} else { | |
$result[] = $word; | |
} | |
} | |
return implode( ' ', $result); | |
} | |
function CaesarCipher($str,$num) { | |
$alphabetLow = range('a', 'z'); | |
$alphabetHigh = range('A', 'Z'); | |
$strArr = str_split($str); | |
$letterMap = array(); | |
for ( $i = 0; $i < count($strArr); $i++ ) { | |
if ( in_array( $strArr[$i], $alphabetLow ) ) { | |
$arr_key = array_search( $strArr[$i], $alphabetLow ); | |
$arr_key = $arr_key + $num > 25 ? $arr_key + $num - 26 : $arr_key + $num; | |
$letterMap[] = $alphabetLow[$arr_key]; | |
} elseif ( in_array( $strArr[$i], $alphabetHigh ) ) { | |
$arr_key = array_search( $strArr[$i], $alphabetHigh ); | |
$arr_key = ( $arr_key + $num > 25 ) ? $arr_key + $num - 26 : $arr_key + $num; | |
$letterMap[] = $alphabetHigh[$arr_key]; | |
} else { | |
$letterMap[] = $strArr[$i]; | |
} | |
} | |
return implode($letterMap); | |
} | |
function CoinDeterminer( $num ) { | |
if ( in_array( $num, array( 11, 9, 7, 5, 1 ) ) ) { | |
return 1; | |
} | |
$counter = 0; | |
while ( $num > 0 ) { | |
switch ( $num ) { | |
case $num == 15: | |
$num -= 5; | |
break; | |
case $num == 14: | |
$num -= 9; | |
break; | |
case $num >= 11: | |
$num -= 11; | |
break; | |
case $num >= 9: | |
$num -= 9; | |
break; | |
case $num >= 7: | |
$num -= 7; | |
break; | |
case $num >= 5: | |
$num -= 5; | |
break; | |
case $num >= 1: | |
$num -= 1; | |
break; | |
} | |
$counter++; | |
} | |
return $counter; | |
} | |
function StringReduction($str) { | |
$strArr = str_split($str); | |
$count = array_count_values($strArr); | |
$count['a'] = isset($count['a']) ? $count['a'] : $count['a'] = 0; | |
$count['b'] = isset($count['b']) ? $count['b'] : $count['b'] = 0; | |
$count['c'] = isset($count['c']) ? $count['c'] : $count['c'] = 0; | |
$nullCount = 0; | |
$evenCount = 0; | |
$oddCount = 0; | |
foreach ($count as $val) { | |
if ( $val == 0 ) { | |
$nullCount++; | |
} | |
if ( odd_even($val) == 'even' ){ | |
$evenCount++; | |
} | |
if ( odd_even($val) == 'odd' ){ | |
$oddCount++; | |
} | |
} | |
if ( $nullCount > 2 ) { | |
return strlen($str); | |
} | |
if ( $evenCount === 3 || $oddCount === 3 ) { | |
return 2; | |
} | |
return 1; | |
} | |
function odd_even( $num ) { | |
if( ($num & 1 ) == 0) { | |
return 'even'; | |
} else { | |
return 'odd'; | |
} | |
} | |
function GasStation($strArr){ | |
$gasStations = $strArr[0]; | |
unset($strArr[0]); | |
$new_num_array = array(); | |
$i = 1; | |
foreach ( $strArr as $station ){ | |
$station = explode(':', $station); | |
$station = $station[0] - $station[1]; | |
$new_num_array[$i] = $station; | |
$i++; | |
} | |
$allStations = array(); | |
$i = 2; | |
foreach ( $new_num_array as $key => $val ) { | |
if ( $i > $gasStations ) { | |
$i -= $gasStations; | |
} | |
unset($new_num_array[$key]); | |
$new_num_array[$key] = $val; | |
$allStations[$i] = $new_num_array; | |
$i++; | |
} | |
$i = 1; | |
for ( $i = 1; $i < $gasStations+1; $i++) { | |
if ( $allStations[$i][$i] <= 0 ) { | |
continue; | |
} | |
$total = 0; | |
foreach ( $allStations[$i] as $station ) { | |
$total += $station; | |
} | |
if ( $total >= 0 ) { | |
return $i; | |
} | |
} | |
return 'impossible'; | |
} | |
function BlackjackHighest($strArr){ | |
$result = ''; | |
$values = array( | |
'two' => 2, | |
'three' => 3, | |
'four' => 4, | |
'five' => 5, | |
'six' => 6, | |
'seven' => 7, | |
'eight' => 8, | |
'nine' => 9, | |
'ten' => 10, | |
'jack' => 10, | |
'queen' => 10, | |
'king' => 10, | |
'ace' => 11 | |
); | |
$rank = array( | |
'two' => 0, | |
'three' => 1, | |
'four' => 2, | |
'five' => 3, | |
'six' => 4, | |
'seven' => 5, | |
'eight' => 6, | |
'nine' => 7, | |
'ten' => 8, | |
'jack' => 9, | |
'queen' => 10, | |
'king' => 11, | |
'ace' => 12 | |
); | |
$total = array(); | |
foreach ( $strArr as $card ) { | |
if ( isset( $total[$card] ) ) { | |
$total[$card] += $values[$card]; | |
} else { | |
$total[$card] = $values[$card]; | |
} | |
} | |
if ( array_sum($total) > 21 && array_key_exists( 'ace', $total ) ) { | |
$total['ace'] = 1; | |
unset($rank['ace']); | |
} | |
$highCard = array(); | |
foreach ( $total as $card => $val ) { | |
if (isset($rank[$card]) ) { | |
$highCard[$card] = $rank[$card]; | |
} | |
} | |
$highCard = array_keys( $highCard, max($highCard)); | |
switch ( $total ) { | |
case array_sum( $total ) < 21 : | |
$result = 'below ' . $highCard[0]; | |
break; | |
case array_sum( $total ) > 21 : | |
$result = 'above ' . $highCard[0]; | |
break; | |
case array_sum( $total ) == 21 : | |
$result = 'blackjack ' . $highCard[0]; | |
break; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment