Help create a php word generator. Generate words from the following letters: e, r, t, u, i, o, p, a, s, d, f, g, h, k, l, z, v, b, n, m

The length of the received word is 5 characters. From the generation you need to delete the words in which:

  • The contract contains 2 or more vowels
  • The contract contains 2 or more identical consonants.
  • The contract contains 3 or more consonants

All data must be written to the file.

I.e:

eraab, eraaa, errab, ertde - such words are deleted

ertab, ritar - such remain

  • Study assignment? - ling
  • No, just a generator of beautiful nicknames - brainit
  • ling, all he suspects. =) On PHP, the probability of a learning task is extremely low: they do not teach it to us . = ( - knes
  • Yeah, I suspect). Now I'll jot down something. - ling

4 answers 4

There are two ways. 1) simple: we go through all the words from aaaaa to zzzzz.

if(preg_match("/(?:[euioa]{2,})|(?:[qwrtypsdfghjklzxcvbnm]{3,})/",$word){ //Убираем из массива, как несоответствующий п1 или п3 } $doubles = str_split('qqwwrrttyyppssddffgghhjjkkllzzxxccvvbbnnmm',2); if(strpos($word,$doubles)!==false){ //убираем из-за двойной согласной } 
  • Thank you for exactly what you need. - brainit
  • To the left of the answer there is a tick "to accept the answer" - knes
 $minlen = 5; $maxlen = 10; $num = 10; $l = array('euioa', 'rtpsdfghklzvbnm'); $ll = array(strlen($l[0]), strlen($l[1])); $out = array(); $ll0 = sizeof($l[0]); $ll1 = sizeof($l[1]); for($j = 0; $j < $num; ++$j){ $cl = rand($minlen, $maxlen); $word = ''; for($i = 0; $i < $cl; ++$i){ if($n === 0) $n = 1; else $n = rand(0, 1); do{ $s = $l[$n][rand(0, $ll[$n]-1)]; }while($s == $word[strlen($word)-1]); $word .= $s; } $out[] = $word; } print_r($out); 

A bit more general solution, implying a variable nickname length. If you need exactly 5 letters, change $ maxlen to 5.

    Brute force:

     const VOWS = 0; const CONS = 1; const ALL = 2; $lists = array( VOWS => array(), CONS => array(), ALL => array() ); foreach (array('euioa', 'rtpsdfghklzvbnm') as $i => $chars) { for ($j = 0; $j < strlen($chars); $j++) { $char = $chars[$j]; $lists[$i][$char] = $i + 1; $lists[ALL][$char] = 1 - $i; } } function generate($chars, $word = '', $level = 0) { global $lists; if ($level == 5) { echo "$word\n"; return; } foreach ($chars as $char => $type) { $list = $lists[$type]; unset($list[$char]); generate($list, $word.$char, $level + 1); } } generate($lists[VOWS] + $lists[CONS]); 

      And what exactly is your problem?

      The usual search in depth and depth limited. As well as checking the rules at each step of generation. There will be many options, I immediately warn you.

      Regarding the generation of nicknames - I would recommend a genetic algorithm. Before this, having trained him with a list of beautiful nicknames. Then it will generate no worse :)