bg-social

STONES
algorithme : stones get min stones from an array for stones
  • 2 stones having same number can fusioned in next level
  • when no dupliacted we get the min numbres return by fusioning from first array
										
function magic($table){
	
	$duplica = dupliacted($table);
	if (!count($duplica)){
		return count($table);
	}else{
		
		$removedDuplicates = array_diff($table, $duplica);
		$duplica2 = array_unique($duplica);
		$newtable = array_merge($removedDuplicates,array_map(function($a){return $a*2;},$duplica2));		
		return getminstones($newtable);
				
	}
}

function dupliacted($array){												
	return array_diff_assoc($array, array_unique($array));
}

echo magic([1,5,1]);
											
									
END STONES algorithme
check string

l'algorithme retourne si une chaine et correct ou pas une chaine est dite correct si:

  • elle est vide
  • contient un nombre '{' et '}' egal dont chaque '{' ferme '}'
  • contient un nombre '[' et ']' egal dont chaque '[' ferme ']'

										
											
function checkStr($str){
  
    if (trim($str)===''|| is_null($str) || strlen($str)===0 || !isset($str)){
        print 'OK';
        return 1;
    }
    if (strlen($str)%2){
        print  'KO';
        return 0;
    }

    $chars = str_split($str);

    $values = array_count_values($chars);

    if ($values['{'] !== $values['}']){
        print  'KO';
        return 0;
    }
    if ($values[']'] !== $values['[']){
        print  'KO';
        return 0;    }
    $reduced = false;
    for ($i=0; $i < count($chars); $i++) { 
            if (isset($chars[$i+1]) && $chars[$i+1]===mynext($chars[$i])){
                unset($chars[$i+1]);
                unset($chars[$i]);
                $reduced = true;
            }
    }
    $newstr = implode('',$chars);
    print $newstr.'
'; if ($reduced) {checkStr($newstr);}else{ if (strlen($newstr)){ print 'KO'; return 0 ; } } } function mynext($car){ $tab =[ '{'=>'}', '['=>']', ]; return $tab[$car]?$tab[$car]:'#'; } echo checkStr('{[{{[[]]}}{}{}[[]]][[]][{{{[{}]}}}]}'); echo checkStr('{{{{][]]}}}}'); echo checkStr('[[{{]}}]]'); echo checkStr('{[{{[[]]}}{}[{}]}]');
Days
/
Hours
/
Minutes
/
Seconds
Copyright © 2017     bg-social