Created
December 18, 2013 15:30
-
-
Save briedis/8024281 to your computer and use it in GitHub Desktop.
Komanda Acually is dolan
This file contains 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
<? | |
function fibonacci($n, $first = 0, $second = 1){ | |
$fib = [$first, $second]; | |
for($i = 1; $i < $n; $i++){ | |
$fib[] = $fib[$i] + $fib[$i - 1]; | |
} | |
return $fib; | |
} | |
class R{ | |
public $q; | |
static $map = array( | |
'apple' => 'green', | |
'banana' => 'yellow', | |
'carrot' => 'orange', | |
'orange' => 'orange', | |
'beet' => 'red', | |
'tomato' => 'red', | |
'lemon' => 'yellow', | |
'cucumber' => 'green', | |
); | |
function __construct($q){ | |
$this->q = $q; | |
} | |
function answer(){ | |
$q = $this->q; | |
define('Q', $q); | |
if($q == "What is your name?"){ | |
$this->what_is_name(); | |
} | |
if(substr($q, 0, 7) == 'Nice to'){ | |
$this->what_is_my_name(); | |
} | |
if($q == 'Do you remember me? What was my name?'){ | |
$this->remember_me(); | |
} | |
if(strpos(Q, 'this song?') && strpos(Q, 'youtube')){ | |
$this->song(); | |
} | |
if(strpos(Q, 'What color is') !== false){ | |
$this->what_color(); | |
} | |
//Which of "apple,banana,carrot,orange,beet,tomato,lemon,cucumber" are red? (comma separated, no spaces)', | |
if(strpos(Q, 'Which of ') === 0){ | |
$this->which_of_fruit(); | |
} | |
//Which is the biggest number of (371,543,459,683,371,215,678,453,272) | |
if(strpos(Q, 'Which is ') !== false){ | |
$this->which_of_num(); | |
} | |
//Return only even numbers from the set (609,684,10,183,916,242,703,815,795,733,521) | |
if(strpos(Q, 'Return only even numbers') !== false){ | |
$this->return_event(); | |
} | |
//Return only odd numbers from the set (792,355,363,177,499,262,116,934,400,88,448,488,476,957,570) | |
if(strpos(Q, 'Return only odd') !== false){ | |
$this->return_odd(); | |
} | |
//ort+the+set+ascending+%2841%2C299%2C224%2C373%2C975%2C116%2C592%2C195%2C745%2C500%2C546%2C184%2C470%2C527%29 | |
if(strpos(Q, 'Sort the set ascend') !== false){ | |
$this->sort_asc(); | |
} | |
if(strpos(Q, 'Sort the set desc') !== false){ | |
$this->sort_desc(); | |
} | |
//'Calculate the sum of all digits (39437859196647149915173160566430484)', | |
if(strpos(Q, 'Calculate the sum') !== false){ | |
$this->calc_sum_dig(); | |
} | |
// Let\'s do some math. What is the result of (5 + 5) | |
if(strpos(Q, ' do some math') !== false){ | |
$this->calc_math(); | |
} | |
// What is the 14th number in Fibonacci sequence? | |
if(strpos(Q, 'Fibonacci') !== false){ | |
$this->calc_fib(); | |
} | |
//Let%27s+play+anagrams.+Which+of+the+words+%28nomen%2Clomem%2Clonely%2Cmelon%29+is+an+anagram+of+lemon%3F | |
//Let\'s play anagrams. Which of the words (nomen,lomem,lonely,melon) is an anagram of lemon? | |
if(strpos(Q, 'Let\'s play anagrams') !== false){ | |
$this->anag(); | |
} | |
} | |
function anag(){ | |
$l = explode(' ', Q); | |
$words = trim($l[7], '('); | |
$words = trim($words, ')'); | |
$fr = array_pop($l); | |
$fr = trim($fr, '?'); | |
$fr = str_split($fr); | |
arsort($fr); | |
$fr = implode('', $fr);; | |
foreach(explode(',', $words) as $j){ | |
$x = $j; | |
$sorted = str_split($j); | |
arsort($sorted); | |
$j = implode('', $sorted); | |
if($j === $fr){ | |
die($x); | |
} | |
} | |
} | |
private function calc_fib(){ | |
// What is the 14th number in Fibonacci sequence? | |
$num = explode(' ', trim(Q))[3]; | |
$num = trim($num, 'h'); | |
$num = trim($num, 't'); | |
$num = trim($num, 's'); | |
$num = trim($num, 'd'); | |
$num = trim($num, 'n'); | |
$num = trim($num, 'r'); | |
//var_dump($num); | |
$s = number_format(fibonacci($num)[$num], 0, '.', '');; | |
die((string)$s); | |
//die(eval("echo " . $num . ";")); | |
} | |
private function calc_math(){ | |
// Let\'s do some math. What is the result of (5 + 5) | |
$num = explode('(', Q)[1]; | |
$num = trim($num, ')'); | |
die(eval("echo " . $num . ";")); | |
} | |
private function calc_sum_dig(){ | |
////'Calculate the sum of all digits (39437859196647149915173160566430484)', | |
$num = explode('(', Q)[1]; | |
$num = trim($num, ')'); | |
$num = str_split($num); | |
$x = 0; | |
foreach($num as $n){ | |
$x += $n; | |
} | |
echo $x; | |
die(); | |
} | |
private function sort_asc(){ | |
//Return only even numbers from the set (609,684,10,183,916,242,703,815,795,733,521) | |
////Which is the biggest number of (371,543,459,683,371,215,678,453,272) | |
$num = explode('(', Q)[1]; | |
$num = trim($num, ')'); | |
$nums = explode(',', $num); | |
asort($nums); | |
die(implode(',', $nums)); | |
} | |
private function sort_desc(){ | |
//Return only even numbers from the set (609,684,10,183,916,242,703,815,795,733,521) | |
////Which is the biggest number of (371,543,459,683,371,215,678,453,272) | |
$num = explode('(', Q)[1]; | |
$num = trim($num, ')'); | |
$nums = explode(',', $num); | |
arsort($nums); | |
die(implode(',', $nums)); | |
} | |
function what_color(){ | |
$i = substr(Q, 14); | |
$i = trim($i, '?'); | |
die(self::$map[$i]); | |
} | |
function song(){ | |
//What is the title of this song? http://www.youtube.com/watch?v=KRH05Nq_JI0 | |
$key = substr(Q, 63); | |
//die($key); | |
$url = "http://gdata.youtube.com/feeds/api/videos/" . $key; | |
$doc = new DOMDocument; | |
$doc->load($url); | |
$title = $doc->getElementsByTagName("title")->item(0)->nodeValue; | |
//$title = explode(' - ', $title)[1]; | |
//$title = trim($title[1]); | |
die(explode(' - ', $title)[1]); | |
} | |
function remember_me(){ | |
// Do you remember me? What was my name? | |
die(file_get_contents('name.txt')); | |
} | |
private function what_is_name(){ | |
die("acually is dolan"); | |
} | |
function what_is_my_name(){ | |
preg_match("/my name is([A-Za-z]+)\./", Q, $matches); | |
$r = substr(Q, 29); | |
$r = explode('.', $r); | |
file_put_contents('name.txt', $r[0]); | |
die($r[0]); | |
} | |
private function which_of_fruit(){ | |
//Which of "apple,banana,carrot,orange,beet,tomato,lemon,cucumber" are red? (comma separated, no spaces)', | |
$fruit = explode('"', Q); | |
$color = explode(' ', trim($fruit[2]))[1]; | |
$color = trim($color, '?'); | |
$re = []; | |
foreach(explode(',', $fruit[1]) as $f){ | |
if(self::$map[$f] == $color){ | |
$re[] = $f; | |
} | |
} | |
die(implode(',', $re)); | |
} | |
private function which_of_num(){ | |
////Which is the biggest number of (371,543,459,683,371,215,678,453,272) | |
$num = explode('(', Q)[1]; | |
$num = trim($num, ')'); | |
die(max(explode(',', $num))); | |
} | |
private function return_event(){ | |
//Return only even numbers from the set (609,684,10,183,916,242,703,815,795,733,521) | |
////Which is the biggest number of (371,543,459,683,371,215,678,453,272) | |
$num = explode('(', Q)[1]; | |
$num = trim($num, ')'); | |
$re = []; | |
foreach(explode(',', $num) as $n){ | |
if($n % 2 == 0){ | |
$re[] = $n; | |
} | |
} | |
die(implode(',', $re)); | |
} | |
private function return_odd(){ | |
//Return only even numbers from the set (609,684,10,183,916,242,703,815,795,733,521) | |
////Which is the biggest number of (371,543,459,683,371,215,678,453,272) | |
$num = explode('(', Q)[1]; | |
$num = trim($num, ')'); | |
$re = []; | |
foreach(explode(',', $num) as $n){ | |
if($n % 2 == 1){ | |
$re[] = $n; | |
} | |
} | |
die(implode(',', $re)); | |
} | |
} | |
(new R($_GET['q']))->answer(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment