Autogram checker: Difference between revisions
m
→{{header|Perl}}: various simplifications
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: oops) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: various simplifications) |
||
Line 216:
<lang perl>use v5.36;
use experimental <builtin for_list>;
use Sub::Util 'subname';
use List::Util 'sum';
my %nums = (
Line 229 ⟶ 226:
'ninety' => 90, 'hundred' => 100,
);
sub whitespace ($text) { $text =~ s/\s/ /gr }▼
my @tests = (
\&punctuation, "This sentence employs two a's, two c's, two d's, twenty-eight e's, five f's, three g's, eight h's, eleven i's, three l's, two m's, thirteen n's, nine o's, two p's, five r's, twenty-five s's, twenty-three t's, six v's, ten w's, two x's, five y's, and one z.",
\&punctuation, "This sentence employs two a's, two c's, two d's, twenty eight e's, five f's, three g's, eight h's, eleven i's, three l's, two m's, thirteen n's, nine o's, two p's, five r's, twenty five s's, twenty three t's, six v's, ten w's, two x's, five y's, and one z.",
\&punctuation, "This pangram contains four as, one b, two cs, one d, thirty es, six fs, five gs, seven hs, eleven is, one j, one k, two ls, two ms, eighteen ns, fifteen os, two ps, one q, five rs, twenty-seven ss, eighteen ts, two us, seven vs, eight ws, two xs, three ys, & one z.",
\&punctuation, "This sentence contains one hundred and ninety-seven letters: four a's, one b, three c's, five d's, thirty-four e's, seven f's, one g, six h's, twelve i's, three l's, twenty-six n's, ten o's, ten r's, twenty-nine s's, nineteen t's, six u's, seven v's, four w's, four x's, five y's, and one z.",
\&punctuation, "Thirteen e's, five f's, two g's, five h's, eight i's, two l's, three n's, six o's, six r's, twenty s's, twelve t's, three u's, four v's, six w's, four x's, two y's.",
\&punctuation, "Sixteen e's, five f's, three g's, six h's, nine i's, five n's, four o's, six r's, eighteen s's, eight t's, three u's, three v's, two w's, four z's."
);
sub wrap { (shift) =~ s/(.{96}.*?\s)/$1\n/gr }
for my($filter, $text) (@tests) {
my(%Claim, %Actual, $count_claim, $count_actual);
my %Punc = ('apostrophe' => "'", 'hyphen' => '-', 'comma' => ',', 'exclamation' => '!');
my $str = my $
$str =~ s/\!/exclamation/g;▼
say wrap
my $
say "\
▲ $str =~ s/\!/exclamation/g;
$str =~ s/\bone ([a-z])[,.]\b/one $1's,/g;
for my($word, $number) (%nums) { $str =~ s/ \b $word \b /$number/
$str =~ s/(\d0)[ \-](\d)/$1 + $2/gxe;
$str =~ s/\b1 100 and (\d+)\b/100 + $1/e;
$str =~ s/(\d+) ([a-z])['
$Actual{$_} = () = $filtered =~ /$_/g for keys %Claim;
$count_claim .= "$_($Claim{$_}) " for sort keys %Claim;
$count_actual .= "$_($Actual{$_}) " for sort keys %Actual;
for (sort keys %
$str =~ m/(\d+) ($_)/;
$count_claim .=
my $n = () = $filtered =~ /
$count_actual .= "$
}
}
say "\nClaimed character counts:\n" . wrap
say "\nActual character counts:\n" . wrap
say "\nAutogram? ". (my $AG = $count_claim eq $count_actual ? 'True' : 'False');
Line 284 ⟶ 278:
if $str =~ /(\d+) letters/;
say "\n" . '=' x 101;
}</lang>▼
▲</lang>
{{out}}
<pre style="height:60ex">This sentence employs two a's, two c's, two d's, twenty-eight e's, five f's, three g's, eight h's,▼
▲This sentence employs two a's, two c's, two d's, twenty-eight e's, five f's, three g's, eight h's,
eleven i's, three l's, two m's, thirteen n's, nine o's, two p's, five r's, twenty-five s's,
twenty-three t's, six v's, ten w's, two x's, five y's, and one z.
Filtering out punctuation▼
Claimed character counts:
Line 308 ⟶ 298:
eleven i's, three l's, two m's, thirteen n's, nine o's, two p's, five r's, twenty five s's, twenty
three t's, six v's, ten w's, two x's, five y's, and one z.
Filtering out punctuation▼
Claimed character counts:
Line 325 ⟶ 313:
eleven i's, three l's, two m's, thirteen n's, nine o's, two p's, five r's, twenty five s's, twenty
three t's, six v's, ten w's, two x's, five y's, and one z.
Claimed character counts:
Line 344 ⟶ 330:
t's, ten u's, eight v's, eight w's, four x's, eleven y's, twenty-seven commas, twenty-three
apostrophes, seven hyphens and, last but not least, a single !
Claimed character counts:
Line 359 ⟶ 347:
is, one j, one k, two ls, two ms, eighteen ns, fifteen os, two ps, one q, five rs, twenty-seven ss,
eighteen ts, two us, seven vs, eight ws, two xs, three ys, & one z.
Claimed character counts:
a(4) b(1) c(2) d(1) e(30) f(6) g(5) h(7) i(11) j(1) k(1) l(2) m(2) n(18) o(15) p(2) q(1) r(5) s(27
t(18) u(2) v(7) w(8) x(2) y(3) z(1)
Actual character counts:
a(4) b(1) c(2) d(1) e(30) f(6) g(5) h(7) i(11) j(1) k(1) l(2) m(2) n(18) o(15) p(2) q(1) r(5) s(27
t(18) u(2) v(7) w(8) x(2) y(3) z(1)
Autogram? True
Line 376 ⟶ 362:
thirty-four e's, seven f's, one g, six h's, twelve i's, three l's, twenty-six n's, ten o's, ten r's,
twenty-nine s's, nineteen t's, six u's, seven v's, four w's, four x's, five y's, and one z.
Claimed character counts:
a(4) b(1) c(3) d(5) e(34) f(7) g(1) h(6) i(12) l(3) n(26) o(10) r(10) s(29) t(19) u(6) v(7) w(4
x(4) y(5) z(1)
Actual character counts:
a(4) b(1) c(3) d(5) e(34) f(7) g(1) h(6) i(12) l(3) n(26) o(10) r(10) s(29) t(19) u(6) v(7) w(4
x(4) y(5) z(1)
Autogram? True
Line 393 ⟶ 377:
Thirteen e's, five f's, two g's, five h's, eight i's, two l's, three n's, six o's, six r's, twenty
s's, twelve t's, three u's, four v's, six w's, four x's, two y's.
Claimed character counts:
Line 407 ⟶ 389:
Fifteen e's, seven f's, four g's, six h's, eight i's, four n's, five o's, six r's, eighteen s's,
eight t's, four u's, three v's, two w's, three x's.
Claimed character counts:
Line 419 ⟶ 403:
Sixteen e's, five f's, three g's, six h's, nine i's, five n's, four o's, six r's, eighteen s's,
eight t's, three u's, three v's, two w's, four z's.
Claimed character counts:
|