info:myphp:anagramme

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
info:myphp:anagramme [2026/03/05 08:57] radeffinfo:myphp:anagramme [2026/03/05 09:14] (Version actuelle) radeff
Ligne 1: Ligne 1:
-====== PHP anagramme ======+====== PHP anagrammes et permutations ====== 
 +===== anagrammes =====
  
 <code> <code>
Ligne 32: Ligne 33:
 </CENTER> </CENTER>
 </FORM> </FORM>
 +</code>
 +
 +===== permutations 1 =====
 + 
 +<code>
 +<?php
 +#source:http://www.phpindex.com/trucsetastuces/trucsetastuces_lire.php3?element=148
 +$chaine1="j";
 +$chaine2="e";
 +$chaine3="m";
 +$chaine4="b";
 +$chaine5="f";
 +$chaine6="r";
 +
 +// Permutation de $chaine1 avec $chaine2
 +
 +#list($chaine1, $chaine2) = array ($chaine2, $chaine1);
 +list($chaine1, $chaine2, $chaine3, $chaine4, $chaine5, $chaine6) = array ($chaine3, $chaine1.$chaine2.$chaine3, $chaine1); 
 +echo $chaine1;
 +echo $chaine2;
 +?> 
 +</code>
 +
 +===== permutations 2 =====
 +
 +
 +<code php>
 +#! /usr/bin/php
 +<? 
 +#################
 +#pour afficher les permutations d'une chaîne de caractère fournie en ligne de commande
 +#source: http://www.webmasterworld.com/forum88/778.htm
 +#adapted fred radeff, radeff.red 25 nov 2008
 +#################
 +print "Input:\n";
 +$name = trim(fgets(STDIN));
 +#print "hello $name !";
 +$test="";
 +for($i=0;$i<strlen($name);$i++){
 +#echo substr($name,$i,1).";";
 +$test.=substr($name,$i,1).";";
 +}
 +$test=ereg_replace(";$","",$test);
 +#echo "\ntest input: " .$test;
 +
 +$yourArr=explode(";",$test);
 +
 +$n=count($yourArr);
 +
 +
 +for ($i=0; $i <= $n; $i++) $pArr[$i]=$i; //The permutation array.
 +
 +function PrintPerm()
 +{
 +global $yourArr,$pArr,$n;
 +
 +for ($i=1; $i <= $n; $i++) echo $yourArr[$pArr[$i]-1];
 +#echo "<br>";
 +echo "\n";
 +
 +return;
 +}
 +
 +function swapThem($i,$j)
 +{
 +global $pArr;
 +
 +$temp = $pArr[$i];
 +$pArr[$i] = $pArr[$j];
 +$pArr[$j] = $temp;
 +}
 +
 +function NextPerm()
 +{
 +global $pArr,$n;
 +
 +$k = $n-1;
 +while ($pArr[$k] > $pArr[$k+1]) $k--;
 +if ($k == 0) return(0);
 +else
 +{
 +$j = $n;
 +while ($pArr[$k] > $pArr[$j]) $j--;
 +swapThem($j,$k);
 +$r = $n;
 +$s = $k+1;
 +while ($r > $s)
 +{
 +swapThem($r,$s);
 +$r--;
 +$s++;
 +}
 +}
 +PrintPerm();
 +return(1);
 +}
 +
 +//Print the array values
 +PrintPerm();
 +while (NextPerm()); //Permute and print
 +?> 
 </code> </code>
  • info/myphp/anagramme.1772697464.txt.gz
  • Dernière modification : 2026/03/05 08:57
  • de radeff