Entry 3795

This Is My Code

   

Submitted by xlazex@mail.ru on May 7, 2010 at 5:27 p.m.
Language: PHP. Code size: 3.6 KB.

<?php

        $A["a"] = array("spelling"=>"a","code"=>"2", "digits"=>"2");
        $A["b"] = array("spelling"=>"b", "code"=>"22", "digits"=>"2");
        $A["c"] = array("spelling"=>"c", "code"=>"222", "digits"=>"2");
	$A["d"] = array("spelling"=>"d", "code"=>"3", "digits"=>"3");
	$A["e"] = array("spelling"=>"e", "code"=>"33", "digits"=>"3");
	$A["f"] = array("spelling"=>"f", "code"=>"333", "digits"=>"3");
        $A["g"] = array("spelling"=>"g", "code"=>"4", "digits"=>"4");
	$A["h"] = array("spelling"=>"g", "code"=>"44", "digits"=>"4");
	$A["i"] = array("spelling"=>"i", "code"=>"444", "digits"=>"4");
	$A["j"] = array("spelling"=>"j", "code"=>"5", "digits"=>"5");
	$A["k"] = array("spelling"=>"k", "code"=>"55", "digits"=>"5");
	$A["l"] = array("spelling"=>"l", "code"=>"555", "digits"=>"5");
	$A["m"] = array("spelling"=>"m", "code"=>"6", "digits"=>"6");
	$A["n"] = array("spelling"=>"n", "code"=>"66", "digits"=>"6");
	$A["o"] = array("spelling"=>"o", "code"=>"666", "digits"=>"6");
        $A["p"] = array("spelling"=>"p", "code"=>"7", "digits"=>"7");
        $A["q"] = array("spelling"=>"q", "code"=>"77", "digits"=>"7");
	$A["r"] = array("spelling"=>"r", "code"=>"777", "digits"=>"7");
	$A["s"] = array("spelling"=>"s", "code"=>"7777", "digits"=>"7");
	$A["t"] = array("spelling"=>"t", "code"=>"8", "digits"=>"8");
	$A["u"] = array("spelling"=>"u", "code"=>"88", "digits"=>"8");
	$A["v"] = array("spelling"=>"v", "code"=>"888", "digits"=>"8");
	$A["w"] = array("spelling"=>"w", "code"=>"9", "digits"=>"9");
	$A["x"] = array("spelling"=>"x", "code"=>"99", "digits"=>"9");
	$A["y"] = array("spelling"=>"y", "code"=>"999", "digits"=>"9");
	$A["z"] = array("spelling"=>"z", "code"=>"9999", "digits"=>"9");

    $arrayofwords = array('bacon','hello');

class Word
{
	public $spelling = '';
	public $digits = NULL;
	public $code = '';


	public function __construct() {}



	}
        function getSymbCode($key, $arr){

   if (array_key_exists($key, $arr))
        return $arr[$key]["code"];
   else {
        foreach ($arr as $val) {
            if (gettype($val) == 'array'){
                $res = getSymbCode($key, $val);
                if ($res != NULL)
                    return $res;
            }
        }
        return NULL;
    }
}

function getSymbDigits($key, $arr){
   if (array_key_exists($key, $arr))
        return $arr[$key]["digits"];
   else {
        foreach ($arr as $val) {
            if (gettype($val) == 'array'){
                $res = getSymbDigits($key, $val);
                if ($res != NULL)
                    return $res;
            }
        }
        return NULL;
    }
}




for ($i = 0; $i <= sizeof($arrayofwords); $i++){
	$word[$i] =  new Word();
	$word[$i]->spelling = $arrayofwords[$i];
	for ($q = 0; $q <= strlen($arrayofwords[$i]) ; $q++){
  			$word[$i]->digits .= getSymbDigits($arrayofwords[$i][$q], $A);
			$word[$i]->code .= /*' '.*/getSymbCode($arrayofwords[$i][$q], $A);

	}
			}


#	echo $word[3]->code;

function binarySearch($arr, $what) {
    $n = sizeof($arr);
    $y = -1;
    $x = $n;
    $z = 0;
    $w = 0;
    $result = array();
    $whatlen = strlen($what);

	while ($y + 1 != $x) {
        $w = ceil(($y + $x) / 2);

        if (substr($arr[$w]->digits, 0, $whatlen) <  $what) {
            $y = $w;
        } else if (substr($arr[$w]->digits, 0, $whatlen) >  $what){
            $x = $w;
        }
		else { return $arr[$w]->spelling;
  }
    }

    $z = $x;
    if ($z >= $n || $arr[$z]!= $what) {
        $z = -1;
    }
    return $arr[$z]->spelling;
}


    echo binarySearch($word,'2');


?>

This snippet took 0.04 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).