[Linuxtrent] Anagrammi in Python

  • From: Lele Gaifax <lele@xxxxxxxxxxxxxxxxxxx>
  • To: linuxtrent@xxxxxxxxxxxxx
  • Date: 09 Aug 2004 01:13:18 +0200

Stimolato in chat da un programmillo Perl

  perl -e 'a("",split("",$ARGV[0]));sub a{my ($p,@s)=@_;if(@s){my
  $r=@s;while($r--){my $c=shift @s;a($p.$c,@s);push @s,$c}}else{print
  "$p\n"}}'

che stampa gli anagrammi di una parola data come argomento, son
riuscito a controbattere questo in Python, che, senza eguagliare in
"cortezza" quello perl... si lascia leggere :-)

  import sys
  def a(w):
      if len(w)==1: yield w
      for i in range(len(w)):
          c,r = w[i],w[:i]+w[i+1:] 
          for ra in a(r): yield c+''.join(ra)
  for w in a(sys.argv[1]): print w

Mi piacerebbe essere superato... :-]

ciao, lele.
-- 
nickname: Lele Gaifax   | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas   | comincerò ad aver paura di chi mi copia.
email: lele@xxxxxxxxxx  |               -- Fortunato Depero, 1929.

--
Per iscriversi  (o disiscriversi), basta spedire un  messaggio con OGGETTO
"subscribe" (o "unsubscribe") a mailto:linuxtrent-request@xxxxxxxxxxxxx


Other related posts: