[Perl] p6 problème dépilatoire
Dominique
d.minix at gmail.com
Sam 28 Oct 12:07:40 CEST 2017
perl6 nombre_en_mots.p6|sed -e 's/un cent/cent/'
oui je sais, c'est pas beau
ma version finale.
my @names = <zero un deux trois quatre cinq six sept huit neuf dix onze
douze treize quatorze quinze seize dix-sept dix-huit dix-neuf vingt trente
quarante cinquante>;
multi sub spell-number(Int $n where {$n < 20}) {
return @names[$n];
}
multi sub spell-number(Int $n where {$n < 60}) {
my $r = @names[$n / 10 + 18];
$r ~= '-' ~ @names[$n % 10] if $n % 10;
return $r;
}
multi sub spell-number(Int $n where {$n < 80}) {
my $r ~= 'soixante ' ~ @names[$n-60];
return $r;
}
multi sub spell-number(Int $n where {$n < 100}) {
my $r ~= 'quatre-vingt ' ~ @names[$n-80];
return $r;
}
multi sub spell-number(Int $n where {$n < 1000}) {
return spell-part($n, 100, 'cent');
}
multi sub spell-number(Int $n where {$n < 1000000}) { return
spell-part($n,1000,"mille")};
multi sub spell-number(Int $n where {$n < 1000000000}) { return
spell-part($n,1000000,"million")};
multi sub spell-number(Int $n where {$n < 1000000000000}) { return
spell-part($n,1000000000,"milliard")};
multi sub spell-number(Int $n where {$n < 1000000000000000}) { return
spell-part($n,1000000000000,"billion")};
multi sub spell-number(Int $n where {$n < 1000000000000000000}) { return
spell-part($n,1000000000000000,"billiard")};
multi sub spell-number(Int $n where {$n < 1000000000000000000000}) { return
spell-part($n,1000000000000000000,"trillion")};
multi sub spell-number(Int $n where {$n < 1000000000000000000000000}) {
return spell-part($n,1000000000000000000000,"trilliard")};
multi sub spell-number(Int $n where {$n < 1000000000000000000000000000}) {
return spell-part($n,1000000000000000000000000,"quadrillion")};
multi sub spell-number(Int $n where {$n < 1000000000000000000000000000000})
{ return spell-part($n,1000000000000000000000000000,"quadrilliard")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000,"quintillion")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000000,"quintilliard")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000000000,"sextillion")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000000000000,"sextilliard")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000000000000000,"septillion")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000000000000000000,"septilliard")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000000000000000000000,"octillion")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000000000000000000000000,"octilliard")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000000000000000000000000000,"nonillion")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000000000000000000000000000000,"nonilliard")};
multi sub spell-number(Int $n where {$n <
1000000000000000000000000000000000000000000000000000000000000000}) { return
spell-part($n,1000000000000000000000000000000000000000000000000000000000000,"decillion")};
sub spell-part(Int $n, Int $base, Str $name) {
my $r = spell-number(($n / $base).Int) ~ ' ' ~ $name;
$r ~= ' ' ~ spell-number($n % $base) if $n % $base;
return $r;
}
say spell-number(147);
say spell-number(397);
say spell-number(452397);
say spell-number(123452197);
say spell-number(123452397187);
say spell-number(1231452397287);
Le 27 octobre 2017 à 21:33, Dominique <d.minix at gmail.com> a écrit :
> suite a la lecture de "using perl6" je tombe sur le cas du spell-number.
> chouette me dis-je, je vais en faire une version frenchy pour m’entraîner
> au 6lang, j'y suis presque mais ...
> je m'arrache les cheveux à solutionner élégamment le problème suivant, je
> n'arrive pas a faire disparaître le "un cent"
> que me conseilleriez vous ? sachant que pour millions et milliard je veux
> bien le garder.
>
>
> my @names = <zero un deux trois quatre cinq six sept huit neuf dix onze
> douze treize quatorze quinze seize dix-sept dix-huit dix-neuf vingt trente
> quarante cinquante>;
>
> multi sub spell-number(Int $n where {$n < 20}) {
> return @names[$n];
> }
> multi sub spell-number(Int $n where {$n < 60}) {
> my $r = @names[$n / 10 + 18];
> $r ~= '-' ~ @names[$n % 10] if $n % 10;
> return $r;
> }
> multi sub spell-number(Int $n where {$n < 80}) {
> my $r ~= 'soixante ' ~ @names[$n-60];
> return $r;
> }
> multi sub spell-number(Int $n where {$n < 100}) {
> my $r ~= 'quatre-vingt ' ~ @names[$n-80];
> return $r;
> }
> multi sub spell-number(Int $n where {$n < 1000}) {
> return spell-part($n, 100, 'cent');
> }
> multi sub spell-number(Int $n where {$n < 1_000_000}) {
> return spell-part($n, 1000, 'mille');
> }
> sub spell-part(Int $n, Int $base, Str $name) {
> my $r = spell-number(($n / $base).Int) ~ ' ' ~ $name;
> $r ~= ' ' ~ spell-number($n % $base) if $n % $base;
> return $r;
> }
>
> say spell-number(147);
> say spell-number(397);
> say spell-number(452397);
>
>
-------------- section suivante --------------
Une pièce jointe HTML a été nettoyée...
URL: <http://listes.mongueurs.net/mailman/private/perl/attachments/20171028/2613019e/attachment-0001.html>
Plus d'informations sur la liste de diffusion Perl