FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need a script





krazycapital
Sorry if this is the wrong place.

I need a script that makes every possible combo of every length under 50. Like, I have an array called "use" and in that is all the letters I want to use. I want to basically have it create an array called "check" and that array will contain something like this (if in "use" array is "A","B","C"):
Code:
A
B
C
AA
AB
AC
BA
BB
BC
CA
CB
CC
AAA
AAB
AAC
ABA
ACA
On and on till the length reaches variable "length". I'll pay... 75 frih$ for who makes this to the specifications. Thanks!
hexkid
krazycapital wrote:
I need a script that makes every possible combo of every length under 50. Like, I have an array called "use" and in that is all the letters I want to use. I want to basically have it create an array called "check" and that array will contain something like this (if in "use" array is "A","B","C"):
Code:
A
B
C
AA
...
ACA
On and on till the length reaches variable "length". I'll pay... 75 frih$ for who makes this to the specifications. Thanks!


Code:
<?php
$use = array('A', 'B', 'C');
$length = 17;

/* *********************************************** */

$done = false;
$check = $prev = $use;
while (count($check) < $length) {
  $newest = array();
  $added = 0;
  foreach ($use as $ch) {
    $more = add_letter_before($ch, $prev, $length - (count($check) + $added));
    $newest = array_merge($newest, $more);
    $added += count($more);
  }
  $prev = $newest;
  $check = array_merge($check, $newest);
}
echo '<pre>'; print_r($check); echo '</pre>';

/* *********************************************** */

function add_letter_before($letter, $arr, $max_elems) {
  if ($max_elems < 1) return array();
  $retval = array();
  $elems = 0;
  foreach ($arr as $prev) {
    $retval[] = $letter . $prev;
    if (++$elems == $max_elems) break;
  }
  return $retval;
}
?>
krazycapital
Thanks! There's just one thing wrong. By length, I meant like the function strlen type length, not the length of the array. I'll pay you the frih once it's like that. Thanks so much!

PS: I still want the max array length too though!
hexkid
krazycapital wrote:
Thanks! There's just one thing wrong. By length, I meant like the function strlen type length, not the length of the array. I'll pay you the frih once it's like that. Thanks so much!

PS: I still want the max array length too though!


Code:
<?php
echo '<pre>';
print_r(permute(array('A', 'B', 'C'), 1000, 5));
echo '</pre>';

/* *********************************************** */

function permute($use, $max_elems, $max_length) {
  $check = $prev = $use;
  while (count($check) < $max_elems) {
    $newest = array();
    $added = 0;
    foreach ($use as $ch) {
      $more = add_letter_before($ch, $prev, $max_elems - (count($check) + $added));
      $newest = array_merge($newest, $more);
      $added += count($more);
    }
    $prev = $newest;
    $check = array_merge($check, $newest);
    if (strlen($newest[0]) == $max_length) break;
  }
  return $check;
}

/* *********************************************** */

function add_letter_before($letter, $arr, $max_elems) {
  if ($max_elems < 1) return array();
  $retval = array();
  $elems = 0;
  foreach ($arr as $prev) {
    $retval[] = $letter . $prev;
    if (++$elems == $max_elems) break;
  }
  return $retval;
}
?>
krazycapital
Could I get a min_length too?
hexkid
krazycapital wrote:
Could I get a min_length too?


That's not in the specification LOL

You could copy all elements you want to another array.
I'll give it to you free. Not tested! Written directly in the browser.

Code:
<?php
$x = permute(array('A', 'B', 'C'), 1000, 5);

$min_length = 3;
$y = array();
foreach ($x as $z) {
  if (strlen($z) >= $min_length) $y[] = $z;
}
echo '<pre>';
print_r($y);
echo '</pre>';
?>
krazycapital
Sorry. I had to type fast before my parents demanded I go to bed... I'll give you 100 $frih cause I don't really have any use for it.

Thanks again!
hexkid
krazycapital wrote:
[...] I'll give you 100 $frih cause I don't really have any use for it.

Thank you. Now it is I who must find use for the FRIH$ Smile

krazycapital wrote:
Thanks again!

You're very welcome.
Related topics
Which is your favourite weblog Script?
Chat script
Script Upload (obiefileman)
script backup database
Script run background
Can someone make me a php script?
target = _parent redirect script?
Bookmark Script:
Some Script Show
Image transparency script
A script for Loading
Set As Home Page Script (IE Only)
Where I can find a 3d chat script
Un script installer? laissez faire frihost!
Need a script - navigation
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.