Autogram checker: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: oops) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 1:
[[Category: String manipulation]]
[[Category:Strings]]
{{draft task}}
;Definition
An '''autogram''' is a sentence that describes itself in the sense of providing an inventory of its own characters. An essential feature is the use of full cardinal number names such as "one", "two", etc., in recording character counts.
Line 66:
In other words, we explicitly ignore details which were not specified in the task description as requirements of all autograms.
Implementation: <
normalize=: {{ tolower ' '(I.(tolower=toupper)y)}y }}
normalize=: tolower@#~ tolower~:toupper
Line 92:
grams=: (, }.@rplc&('Zone';'Zsingle')@('Z'&,)L:0) grams
*/+./+./@E.&(normalizep2 Y) every grams
}}</
Task examples:<
1
autogram {{)n 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.}}
Line 109:
0
autogram {{)n 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.}}
0</
As an aside, this illustrates that rosettacode's J syntax highlighter does not properly handle J's {{)n delimited character literals (a recent language feature, introduced in J903).
Line 115:
=={{header|Julia}}==
Validating the total letter count when used.
<
using DataStructures
Line 192:
println("Test phrase $i is", isautogram(t[1], t[2]) ? " " : " not ", "a valid autogram.\n")
end
</
<pre>
Test phrase 1 is a valid autogram.
Line 211:
The count of z in the phrase is incorrect.
Test phrase 8 is not a valid autogram.
</pre>
=={{header|Nim}}==
{{trans|Wren}}
<syntaxhighlight lang="Nim">import std/[algorithm, sequtils, strformat, strutils, tables]
const
Numbers = {"fourteen": "14", "sixteen": "16", "seventeen": "17", "eighteen": "18",
"nineteen": "19", "sixty": "60", "seventy": "70", "eighty": "80",
"ninety": "90", "one": "1", "two": "2", "three": "3",
"four": "4", "five": "5", "six": "6", "seven": "7",
"eight": "8", "nine": "9", "ten": "10", "eleven": "11",
"twelve": "12", "thirteen": "13", "fifteen": "15", "twenty": "20",
"thirty": "30", "forty": "40", "fifty": "50", "single": "1"}.toOrderedTable
Punctuation = {"comma": ",", "hyphen": "-", "apostrophe": "'", "exclamation": "!"}.toTable
LowerLetters = {'a'..'z'}
Symbols = {',', '-', '\'', '!'}
proc autogram(sentence: string; ignorePunct: bool) =
echo &"Sentence:\n{sentence}"
echo &"""Ignore punctuation: {["no", "yes"][ord(ignorePunct)]}"""
var s = sentence.toLowerAscii
# Get actual character counts.
let chars = if ignorePunct: LowerLetters else: LowerLetters + Symbols
var ctable1: CountTable[char]
for c in s:
if c in chars:
ctable1.inc(c)
let keys1 = sorted(ctable1.keys.toSeq) # Sort into lexicographical order.
let charCounts1 = keys1.mapIt((it, ctable1[it])).join(" ")
echo "\nActual character counts:"
echo charCounts1
for text, value in Numbers.pairs:
s = s.replace(text, value)
if not ignorePunct:
for punct in Punctuation.pairs:
s = s.replace(punct[0], punct[1])
let words = s.split(' ')
var ctable2: CountTable[char]
let wc = words.len
var i = 0
while i < wc - 1:
if words[i].all(isDigit):
if words[i+1].all(isDigit) and i + 2 < wc:
let count = words[i].parseInt() + words[i+1].parseInt()
let ch = words[i + 2][0]
ctable2[ch] = count
inc i, 3
else:
let count = words[i].parseInt()
let ch = words[i + 1][0]
ctable2[ch] = count
inc i, 2
elif '-' in words[i]:
let split = words[i].split('-')
if split[0].all(isDigit) and split[1].all(isDigit):
let count = split[0].parseInt() + split[1].parseInt()
let ch = words[i + 1][0]
ctable2[ch] = count
inc i, 2
else:
inc i
else:
inc i
let keys2 = sorted(ctable2.keys.toSeq)
let charCounts2 = keys2.mapIt((it, ctable2[it])).join(" ")
echo "\nPurported character counts:"
echo charCounts2
echo &"\nIs autogram? {charCounts1 == charCounts2}"
const Tests = [
("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.", true),
("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.", true),
("Only the fool would take trouble to verify that his sentence was composed of ten a's, three b's, four c's, four d's, forty-six e's, sixteen f's, four g's, thirteen h's, fifteen i's, two k's, nine l's, four m's, twenty-five n's, twenty-four o's, five p's, sixteen r's, forty-one s's, thirty-seven 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 !", false),
("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.", true),
("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.", true),
("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.", true),
("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.", false),
("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.", true)
]
for t in Tests:
autogram(t[0], t[1])
echo repeat('=', 80)
</syntaxhighlight>
{{out}}
<pre>Sentence:
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.
Ignore punctuation: yes
Actual character counts:
('a', 2) ('c', 2) ('d', 2) ('e', 28) ('f', 5) ('g', 3) ('h', 8) ('i', 11) ('l', 3) ('m', 2) ('n', 13) ('o', 9) ('p', 2) ('r', 5) ('s', 25) ('t', 23) ('v', 6) ('w', 10) ('x', 2) ('y', 5) ('z', 1)
Purported character counts:
('a', 2) ('c', 2) ('d', 2) ('e', 28) ('f', 5) ('g', 3) ('h', 8) ('i', 11) ('l', 3) ('m', 2) ('n', 13) ('o', 9) ('p', 2) ('r', 5) ('s', 25) ('t', 23) ('v', 6) ('w', 10) ('x', 2) ('y', 5) ('z', 1)
Is autogram? true
================================================================================
Sentence:
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.
Ignore punctuation: yes
Actual character counts:
('a', 2) ('c', 2) ('d', 2) ('e', 28) ('f', 5) ('g', 3) ('h', 8) ('i', 11) ('l', 3) ('m', 2) ('n', 13) ('o', 9) ('p', 2) ('r', 5) ('s', 25) ('t', 23) ('v', 6) ('w', 10) ('x', 2) ('y', 5) ('z', 1)
Purported character counts:
('a', 2) ('c', 2) ('d', 2) ('e', 28) ('f', 5) ('g', 3) ('h', 8) ('i', 11) ('l', 3) ('m', 2) ('n', 13) ('o', 9) ('p', 2) ('r', 5) ('s', 25) ('t', 23) ('v', 6) ('w', 10) ('x', 2) ('y', 5) ('z', 1)
Is autogram? true
================================================================================
Sentence:
Only the fool would take trouble to verify that his sentence was composed of ten a's, three b's, four c's, four d's, forty-six e's, sixteen f's, four g's, thirteen h's, fifteen i's, two k's, nine l's, four m's, twenty-five n's, twenty-four o's, five p's, sixteen r's, forty-one s's, thirty-seven 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 !
Ignore punctuation: no
Actual character counts:
('!', 1) ('\'', 23) (',', 27) ('-', 7) ('a', 10) ('b', 3) ('c', 4) ('d', 4) ('e', 46) ('f', 16) ('g', 4) ('h', 13) ('i', 15) ('k', 2) ('l', 9) ('m', 4) ('n', 25) ('o', 24) ('p', 5) ('r', 16) ('s', 41) ('t', 37) ('u', 10) ('v', 8) ('w', 8) ('x', 4) ('y', 11)
Purported character counts:
('!', 1) ('\'', 23) (',', 27) ('-', 7) ('a', 10) ('b', 3) ('c', 4) ('d', 4) ('e', 46) ('f', 16) ('g', 4) ('h', 13) ('i', 15) ('k', 2) ('l', 9) ('m', 4) ('n', 25) ('o', 24) ('p', 5) ('r', 16) ('s', 41) ('t', 37) ('u', 10) ('v', 8) ('w', 8) ('x', 4) ('y', 11)
Is autogram? true
================================================================================
Sentence:
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.
Ignore punctuation: yes
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)
Purported 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)
Is autogram? true
================================================================================
Sentence:
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.
Ignore punctuation: yes
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)
Purported 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)
Is autogram? true
================================================================================
Sentence:
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.
Ignore punctuation: yes
Actual character counts:
('e', 13) ('f', 5) ('g', 2) ('h', 5) ('i', 8) ('l', 2) ('n', 3) ('o', 6) ('r', 6) ('s', 20) ('t', 12) ('u', 3) ('v', 4) ('w', 6) ('x', 4) ('y', 2)
Purported character counts:
('e', 13) ('f', 5) ('g', 2) ('h', 5) ('i', 8) ('l', 2) ('n', 3) ('o', 6) ('r', 6) ('s', 20) ('t', 12) ('u', 3) ('v', 4) ('w', 6) ('x', 4) ('y', 2)
Is autogram? true
================================================================================
Sentence:
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.
Ignore punctuation: no
Actual character counts:
('\'', 14) (',', 13) ('e', 15) ('f', 7) ('g', 4) ('h', 6) ('i', 8) ('n', 4) ('o', 5) ('r', 6) ('s', 18) ('t', 8) ('u', 4) ('v', 3) ('w', 2) ('x', 3)
Purported character counts:
('e', 15) ('f', 7) ('g', 4) ('h', 6) ('i', 8) ('n', 4) ('o', 5) ('r', 6) ('s', 18) ('t', 8) ('u', 4) ('v', 3) ('w', 2) ('x', 3)
Is autogram? false
================================================================================
Sentence:
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.
Ignore punctuation: yes
Actual character counts:
('e', 16) ('f', 5) ('g', 3) ('h', 6) ('i', 9) ('n', 5) ('o', 4) ('r', 6) ('s', 18) ('t', 8) ('u', 3) ('v', 3) ('w', 2) ('x', 3) ('z', 1)
Purported character counts:
('e', 16) ('f', 5) ('g', 3) ('h', 6) ('i', 9) ('n', 5) ('o', 4) ('r', 6) ('s', 18) ('t', 8) ('u', 3) ('v', 3) ('w', 2) ('z', 4)
Is autogram? false
================================================================================
</pre>
=={{header|Perl}}==
<
use experimental <builtin for_list>;
use Sub::Util 'subname';
use List::Util 'sum';
my %nums = (
Line 229 ⟶ 414:
'ninety' => 90, 'hundred' => 100,
);
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 punctuation ($text) { $text =~ s/\W//grx }
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/\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 ⟶ 466:
if $str =~ /(\d+) letters/;
say "\n" . '=' x 101;
}</syntaxhighlight>
{{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,
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 308 ⟶ 486:
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 325 ⟶ 501:
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 ⟶ 518:
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 !
NOT filtering out punctuation
Claimed character counts:
Line 359 ⟶ 535:
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 ⟶ 550:
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 ⟶ 565:
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 ⟶ 577:
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.
NOT filtering out punctuation
Claimed character counts:
Line 419 ⟶ 591:
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:
Line 433 ⟶ 603:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"""
Line 540 ⟶ 710:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Autogram? %t%s\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">autogram</span><span style="color: #0000FF;">,</span><span style="color: #000000;">diff</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 578 ⟶ 748:
Using [https://raku.land/github:jkramer/Text::Wrap Text::Wrap] from the ecosystem. Install command: <code>zef install -v git://github.com/jkramer/p6-Text-Wrap.git</code>.
<syntaxhighlight lang="raku"
:12twelve, :13thirteen, :14fourteen, :15fifteen, :16sixteen, :17seventeen, :18eighteen, :19nineteen,
:20twenty, :30thirty, :40forty, :50fifty, :60sixty, :70seventy, :80eighty, :90ninety, :100hundred, :1single;
Line 628 ⟶ 798:
say '=' x 100;
}</
{{out}}
<pre style="height:50ex;overflow:scroll;">====================================================================================================
Line 756 ⟶ 926:
{{libheader|Wren-str}}
Frankly, not a bullet-proof solution but good enough to check the required sentences.
<
var numbers = [
Line 872 ⟶ 1,042:
autogram.call(t[0], t[1])
System.print("=" * 80)
}</
{{out}}
|