aboutsummaryrefslogtreecommitdiff
path: root/challenge-160/james-smith/perl/ch-1.pl
blob: 6d0a332f38fd6625fa8bbd4fb75bd30c1aaa7a48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/local/bin/perl

use strict;

use warnings;
use feature qw(say);
use Test::More;
use Benchmark qw(cmpthese timethis);
use Data::Dumper qw(Dumper);
use Lingua::EN::Numbers qw(num2en);

my @TESTS = (
  [ 5, 'Five is four, four is magic.' ],
  [ 7, 'Seven is five, five is four, four is magic.' ],
  [ 6, 'Six is three, three is five, five is four, four is magic.' ],
  [ 4, 'Four is magic.' ],
);

say magic($_) for 0..1000;
is( magic($_->[0]), $_->[1] ) foreach @TESTS;

done_testing();

sub magic {
  my $r = ucfirst num2en( my $n = shift ).' is ';
  $r .= join num2en( $n = length num2en($n)=~s/\W//rg ), '', ', ', ' is ' until $n==4;
  $r.'magic.';
}