Created
May 25, 2015 13:09
-
-
Save gnysek/c23b5bca21118546aaa0 to your computer and use it in GitHub Desktop.
scrabble array simplified
This file contains hidden or 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
<?php | |
header('Content-type: text/html; charset=utf-8'); | |
mb_internal_encoding("UTF-8"); | |
ini_set('memory_limit', '1024M'); | |
ini_set('max_execution_time', 500); | |
?> | |
<style> | |
table, td, th { | |
border: 1px solid black; | |
} | |
th { | |
background: yellowgreen; | |
} | |
</style> | |
<?php | |
echo '<code>'; | |
$str = '-aąbcćdeęfghijklłmnńoópqrsśtuwvxyzżź'; | |
$perLine = mb_strlen($str); | |
/*class bst | |
{ | |
public $left = NULL; | |
public $right = NULL; | |
public $value = false; | |
public function __construct($value) | |
{ | |
$this->value = $value; | |
} | |
public function insert($value) | |
{ | |
var_dump($value); | |
} | |
}*/ | |
#$header = new bst('a'); | |
$words = array(); | |
$file = fopen('slowa-win.txt', 'r'); | |
$lines = 0; | |
$totalLenght = 0; | |
while ($word = fgets($file)) { | |
$word = str_replace("\r\n", '', $word); | |
#echo $word . '<br/>' . PHP_EOL; | |
$reference = & $words; | |
$totalLenght += mb_strlen($word); | |
for ($i = 0; $i < mb_strlen($word); $i++) { | |
$letter = mb_substr($word, $i, 1); | |
if (array_key_exists($letter, $reference)) { | |
#echo 'F'; | |
} else { | |
$reference[$letter] = array(); | |
} | |
$reference = & $reference[$letter]; | |
} | |
$lines++; | |
/*if ($lines >= 500000) { | |
break; | |
}*/ | |
} | |
fclose($file); | |
echo $word . '<br/>'; | |
function tab($array, $level = 0) | |
{ | |
#echo '<br/>'; | |
$cnt = count($array); | |
foreach ($array as $k => $v) { | |
#echo str_repeat('_', $level * 1) . $k; | |
if (is_array($v)) { | |
$cnt += tab($v, $level + 1); | |
} | |
} | |
return $cnt; | |
} | |
$totalSaved = tab($words); | |
echo $totalLenght . '<br/>'; | |
echo $totalSaved . '<br/>'; | |
echo round($totalSaved / $totalLenght * 100, 2) . '%<br/>'; | |
echo round($totalLenght / $totalSaved, 2) . 'x<br/>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment