[Linuxtrent] Re: Anagrammi in Python

  • From: Flavio Visentin <THe_ZiPMaN@xxxxxxxxx>
  • To: linuxtrent@xxxxxxxxxxxxx
  • Date: Mon, 09 Aug 2004 12:18:11 +0200

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Il 08/09/04 01:13, Lele Gaifax, sedendosi sulla tastiera, ha scritto ...
| 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... :-]

E' semplicissimo: Se quello che vuoi è la leggibilità in perl è
sufficiente non eseguire da linea di comando e fare come hai fatto tu in
python, creando un file di script (che riporto sotto).

Il vantaggio del perl è che, a differenza del python, non è obbligatorio
creare un file per fare un programma; basta la linea di comando.

Viva la libertà!

#!/usr/bin/perl

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"
~    }
}

- --
Flavio Visentin

|                     \|||/
|                    @/0.0\@
|                     \ - /
+------------------oOOo---oOOo------------------

There are only 10 types of people in this world:
those who understand binary, and those who don't.

GPG Key: http://www.zipman.it/gpgkey.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.5 (GNU/Linux)

iD8DBQFBF09jusUmHkh1cnoRAntCAJ4xFPaN1JXkKKwEo9SHay6IQKMl6ACdH2LT
a99TB0A2XCGe+pzTk8p1kMY=
=0sE9
-----END PGP SIGNATURE-----
--
Per iscriversi  (o disiscriversi), basta spedire un  messaggio con OGGETTO
"subscribe" (o "unsubscribe") a mailto:linuxtrent-request@xxxxxxxxxxxxx


Other related posts: