Skip to content

Instantly share code, notes, and snippets.

@MohamedFawzy
Last active August 29, 2015 14:00
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 MohamedFawzy/023a67559fcad6d2e389 to your computer and use it in GitHub Desktop.
Save MohamedFawzy/023a67559fcad6d2e389 to your computer and use it in GitHub Desktop.
php algorithm
<?php
/**
* 100 doors problem set
*/
// toggle state for doors
$toggleState = array('open' => 'closed', 'closed' => 'open');
// init all doors as closed
$doors = array_fill(1, 100, 'closed');
for($pass=1; $pass<=100; ++$pass){
for($nr =1; $nr <=100; ++$nr){
if($nr % $pass == 0){
$doors[$nr] = $toggleState[$doors[$nr]];
}
}
}
for($nr=1; $nr<=100; ++$nr){
//printf("Door %d is %s\n", $nr, $doors[$nr]);
}
// optimized version
for($i=1; $i<=100; $i++){
$root = sqrt($i);
$state = ($root == ceil($root)) ? 'open' : 'closed';
echo "Door {$i}: {$state}\n";
}
<?php
while (true) {
$numbers = make_numbers();
for ($iteration_num = 1; ; $iteration_num++) {
echo "Expresion $iteration_num: ";
$entry = rtrim(fgets(STDIN));
if ($entry === '!') break;
if ($entry === 'q') exit;
$result = play($numbers, $entry);
if ($result === null) {
echo "That's not valid\n";
continue;
}
elseif ($result != 24) {
echo "Sorry, that's $result\n";
continue;
}
else {
echo "That's right! 24!!\n";
exit;
}
}
}
function make_numbers() {
$numbers = array();
echo "Your four digits: ";
for ($i = 0; $i < 4; $i++) {
$number = rand(1, 9);
// The check is needed to avoid E_NOTICE from PHP
if (!isset($numbers[$number])) {
$numbers[$number] = 0;
}
$numbers[$number]++;
print "$number ";
}
print "\n";
return $numbers;
}
function play($numbers, $expression) {
$operator = true;
for ($i = 0, $length = strlen($expression); $i < $length; $i++) {
$character = $expression[$i];
if (in_array($character, array('(', ')', ' ', "\t"))) continue;
$operator = !$operator;
if (!$operator) {
if (!empty($numbers[$character])) {
$numbers[$character]--;
continue;
}
return;
}
elseif (!in_array($character, array('+', '-', '*', '/'))) {
return;
}
}
foreach ($numbers as $remaining) {
if ($remaining > 0) {
return;
}
}
return eval("return $expression;");
}
<?php
/**
* 99 bootles of beer problem set
*/
$plural = 's';
foreach(range(1, 99) as $i){
echo "$i bootle$plural of beer on the wall \n";
echo "$i bottle$plural of beer!\n";
echo "Take one down, pass it around!\n";
if ($i - 1 == 1)
$plural = '';
if ($i > 1)
echo ($i - 1) . " bottle$plural of beer on the wall!\n\n";
else
echo "No more bottles of beer on the wall!\n";
}
<?php
fscanf(STDIN, "%d %d\n", $a, $b); //Reads 2 numbers from STDIN
echo ($a + $b) . "\n";
<?php
/**
* abc problem set
*
*/
$words = array("A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "Confuse");
function canMakeWord($word) {
$word = strtoupper($word);
$blocks = array(
"BO", "XK", "DQ", "CP", "NA",
"GT", "RE", "TG", "QD", "FS",
"JW", "HU", "VI", "AN", "OB",
"ER", "FS", "LY", "PC", "ZM",
);
foreach (str_split($word) as $char) {
foreach ($blocks as $k => $block) {
if (strpos($block, $char) !== FALSE) {
unset($blocks[$k]);
continue(2);
}
}
return false;
}
return true;
}
foreach ($words as $word) {
echo $word.': ';
echo canMakeWord($word) ? "True" : "False";
echo "\r\n";
}
<?php
/**
* Anagram finder function
* Check if word is an anagram for any word in provided dictionary
*/
function anagram_finder($anagram, $dictionary)
{
// the array for successful words
$match_words = array();
// split anagram word by ASCII codes and how many times occured
$anagram_letters = count_chars($anagram, 1);
// for each word in dictinary...
foreach($dictionary as $word){
// split word by chars
$letters = count_chars($word, 1);
// for each char...
foreach($letters as $key => $amount){
// skip the word if any char not exist in anagram word or its number is bigger than in anagram word
if(!array_key_exists($key, $anagram_letters) || $amount > $anagram_letters[$key]){
continue 2;
}
}
// if was not skipped - put word to successful words array
$match_words[] = $word;
}
return $match_words;
}
$dictionary = array("bomb", "auto", "rome", "plane", "elephant");
$results = anagram_finder("pbaluromateobom", $dictionary);
print_r($results);
<?php
/**
* Symmetric_difference between two arrays problem
*/
// input arrays
$array1 = array(1, 7, 8, 2, 4, 5);
$array2 = array(3, 5, 1, 7, 6, 9);
// output arrays
$result1 = array();
$result2 = array();
$total_result = array();
// remove any duplicates before running to be faster in comparing arrays for processor
$array1 = array_unique($array1);
$array2 = array_unique($array2);
// get differences from array1 against array2
$result1 = array_diff($array1, $array2);
// get differences from array2 against array1
$result2 = array_diff($array2, $array1);
// simply merge together both result set into one array
$total_result = array_merge($result1,$result2);
var_dump($total_result);
Hello this will be place for solve and test some algorithms and tricks i can handle them using php programming language
<?php
/**
* find possible paragraphs based on those words
* given array
* result paragraph
*/
$words = array('php','love','technology','hackers','travel','thinking');
// first of all i will use simple computing math rule 2^n can give you possible combinations such as binary 2^5=32 etc ..
// use power function
$total = count($words);
// this will give us possible combination 2^6=64
$possible = pow(2,$total);
/***
* repersent them in binary like this
* 0 0 0
* 0 0 1
* 0 1 0
* 0 1 1
* etc ...
* */
// loop throw each column
for($i=0; $i<$possible; $i++){
// foreach combination check if each bit is set
for($j=0; $j < $possible; $j++){
if(pow(2,$j) & $i) echo $words[$j].' ';
}
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment