Moderator |
|
Gelukt .
<?php
// naar 26-cijferig stelsel
function to26($chars) {
$number_array = array();
$chars = strrev($chars);
for($i=0; $i < strlen($chars); $i++) {
$number_array[$i] = ord(strtolower($chars{$i})) - 97; // a=0, b=1 etc
}
return $number_array;
}
// 26 cijferig -> decimaal
function todec($number_array) {
$return = 0;
for($i=0; $i < sizeof($number_array); $i++) {
$return += $number_array[$i] * pow(26, $i);
}
return $return;
}
// decimaal -> 26 cijferig
function decto26($number, $length) {
$number_array = array();
for($i = $length-1; $i > -1; $i--) {
$i_aantal = (int) floor($number/pow(26, $i));
$number_array[$i] = $i_aantal;
$number = $number - ($i_aantal * pow(26, $i));
}
ksort($number_array);
return $number_array;
}
function printnumberarray($arr) {
$ret = "";
for($i=sizeof($arr)-1; $i > -1; $i--) {
$ret .= chr($arr[$i]+97);
}
return $ret;
}
function dumparray($arr) {
echo "<pre>";
print_r($arr);
echo "</pre>";
}
$test = "abc";
echo "test: ".$test."<br />";
$test26 = to26($test);
dumparray($test26);
$testnr = todec($test26);
echo "decimaal: ".$testnr."<br />";
$test26 = decto26($testnr, 3);
echo "terug: ";
dumparray($test26);
echo "test: ".printnumberarray($test26)."<br />";
echo "start: ".todec(to26("ghi"))."<br />";
echo "eind: ".todec(to26("zzz"))."<br />";
$start = todec(to26("ghi"));
$eind = todec(to26("zzz"));
echo "<pre>";
for($i=$start; $i <= $eind; $i++) {
echo printnumberarray(decto26($i, 3))."\n";
}
echo "</pre>";
?>
<?php // naar 26-cijferig stelsel function to26($chars) { for($i=0; $i < strlen($chars); $i++) { $number_array[$i] = ord(strtolower($chars{$i})) - 97; // a=0, b=1 etc } return $number_array; } // 26 cijferig -> decimaal function todec($number_array) { $return = 0; for($i=0; $i < sizeof($number_array); $i++) { $return += $number_array[$i] * pow(26, $i); } return $return; } // decimaal -> 26 cijferig function decto26($number, $length) { for($i = $length-1; $i > -1; $i--) { $i_aantal = (int ) floor($number/pow(26, $i)); $number_array[$i] = $i_aantal; $number = $number - ($i_aantal * pow(26, $i)); } return $number_array; } function printnumberarray($arr) { $ret = ""; for($i=sizeof($arr)-1; $i > -1; $i--) { $ret .= chr($arr[$i]+97); } return $ret; } function dumparray($arr) { } $test = "abc"; echo "test: ".$test."<br />"; $test26 = to26($test); dumparray($test26); $testnr = todec($test26); echo "decimaal: ".$testnr."<br />"; $test26 = decto26($testnr, 3); dumparray($test26); echo "test: ".printnumberarray ($test26)."<br />"; echo "start: ".todec (to26 ("ghi"))."<br />"; echo "eind: ".todec (to26 ("zzz"))."<br />"; $start = todec(to26("ghi")); $eind = todec(to26("zzz")); for($i=$start; $i <= $eind; $i++) { echo printnumberarray (decto26 ($i, 3))."\n"; } ?>
Het aantal combinaties neemt natuurlijk enorm toe als je een string ter lengte 4 of 5 wilt hebben, uiteraard... |