Okay boys and girls (?). Here it is, I worked on the assumption that a combination lock will always have the same numbers available through all the sets.
class combo_lock{
var $max_number; // numbers available on the combination
var $number_sets; // number of sets containing the max_numbers
var $filename; // filename to output to
function combo_lock(){
$this->max_number = 0; // defualt assignments
$this->number_sets = 0;
$this->filename = "";
}
function start_unlock($echo){
if (($this->max_number != 0) && ($this->number_sets != 0) && ($this->filename != "")) {
$power_of = pow($this->max_number,$this->number_sets); // get possible number of combinations
$fh = fopen($this->filename,"w"); // open file for writting
$line_num = 0;
for ($i = 0; $i < $power_of; $i++){
$v1 = $i % $this->max_number;
for ($j = 1; $j <= $this->number_sets; $j++){
$output[$j] = ($i / (pow($this->max_number,$j))) % $this->max_number;
} // get combinations available at point i
$output_str = $v1;
for ($j = 1; $j < count($output);$j++){
$output_str .= "-" . $output[$j];
} // build output string
$output_str .= "\n";
$line_num++;
if ($echo == 1) {
echo "<b>" . $line_num . "</b>:" . $output_str . "<br />";
}
fwrite($fh,$output_str);
} // for loop though number of combinations
fclose($fh);
echo "Completed!" ;
}else{
die ("Please set the properties for the class");
}
}
}
// usage-----------------------------------------------------------------
$oLock = new combo_lock();
$oLock->max_number = 10;
$oLock->number_sets = 5;
$oLock->filename = "combo_lock.txt";
$oLock->start_unlock(1); // 1 to echo the values (true)
-
- class combo_lock{
-
- var $max_number; // numbers available on the combination
-
- var $number_sets; // number of sets containing the max_numbers
-
- var $filename; // filename to output to
-
-
-
- function combo_lock(){
-
- $this->max_number = 0; // defualt assignments
-
- $this->number_sets = 0;
-
- $this->filename = "";
-
- }
-
-
-
- function start_unlock($echo){
-
- if (($this->max_number != 0) && ($this->number_sets != 0) && ($this->filename != "")) {
-
- $power_of = pow($this->max_number,$this->number_sets); // get possible number of combinations
-
- $fh = fopen($this->filename,"w"); // open file for writting
-
- $line_num = 0;
-
- for ($i = 0; $i < $power_of; $i++){
-
- $v1 = $i % $this->max_number;
-
- for ($j = 1; $j <= $this->number_sets; $j++){
-
- $output[$j] = ($i / (pow($this->max_number,$j))) % $this->max_number;
-
- } // get combinations available at point i
-
- $output_str = $v1;
-
- for ($j = 1; $j < count($output);$j++){
-
- $output_str .= "-" . $output[$j];
-
- } // build output string
-
- $output_str .= "\n";
-
- $line_num++;
-
- if ($echo == 1) {
-
- echo "<b>" . $line_num . "</b>:" . $output_str . "<br />";
-
- }
-
- fwrite($fh,$output_str);
-
- } // for loop though number of combinations
-
- fclose($fh);
-
- echo "Completed!" ;
-
- }else{
-
- die ("Please set the properties for the class");
-
- }
-
- }
-
-
-
- }
-
-
-
-
-
- // usage-----------------------------------------------------------------
-
- $oLock = new combo_lock();
-
- $oLock->max_number = 10;
-
- $oLock->number_sets = 5;
-
- $oLock->filename = "combo_lock.txt";
-
- $oLock->start_unlock(1); // 1 to echo the values (true)
-
-
Well that was loads of fun! I wish Ozzu had a code challenge. Really nice to build these small things. Challenging
Watch me grow