Tuesday, January 12, 2016

Limbajul PHP. Guessing game. Tablouri multidimensionale

Urmatorul enunt de pe www.programmr.com este o problema simpla cu sase scenarii posibile, raspunsurile de selectie fiind introduse de la tastatura.

Se cere sa se identifice prin doua interogari un obiect intr-o matrice 2 x 3 (screen-ul urmator).

Din pacate, softul de pe programmr.com valideaza si topica si compozitia stringului de iesire, dupa urmatorul pattern.

1) "alive" & "outside" sau "alive" & "inside":  Obviously the living thing on your mind is a ...

2) "not alive" & "outside" sau "not alive" & "inside" : Obviously the nonliving thing on your mind is a ...
3) ("alive" sau "not alive") && "both":   Obviously the nonliving thing inside/outside on your mind is a ...

Altminteri, problema e triviala iar varianta comoda este stocarea entitatilor living/not living etc intr-un tablou bidimensional cu cheile indicate mai jos.






Codul:

<?php

echo"TWO MORE QUESTIONS, BABY!:";

echo"\nThink of something and I'll try to guess it!:";

echo"\nQuestion 1: Does it belong inside or outside or both?:";

$a=trim(fgets(STDIN));

echo"Question 2: Is it alive?:";

$b=trim(fgets(STDIN));

    $matrix = array("alive"=>array("inside"=>"houseplant","outside"=>"bison", "both"=>"dog"),
"not alive"=>array("inside"=>"shower curtain", "outside"=>"billboard", "both"=>"cell phone"));

if($b == "yes")
{
    $c = "alive";
    if($a == "both")
    {
        $d = "inside/outside";
        echo "Obviously the living thing ".$d." on your mind is a ".$matrix[$c][$a]."!";
    }
    else echo "Obviously the living thing on your mind is a ".$matrix[$c][$a]."!";
}
else if($b = "no")
{
    $c = "not alive";
    $e = "living";
    if($a == "both")
    {
        $d = "inside/outside";
        echo "Obviously the nonliving thing ".$d." on your mind is a ".$matrix[$c][$a]."!";
    }
    else echo "Obviously the nonliving thing on your mind is a ".$matrix[$c][$a]."!";
}


?>

Printscreen-urile de validare:



Link-ul catre aplicatia pe site-ul programmr.

No comments:

Post a Comment