- 364
- 6
- 11
Shell - Bytes: 2546, False +ves: 0, False -ves: 0 = Score: (削除) 2582 (削除ここまで) (削除) 2548 (削除ここまで) 2546
Code: (削除) 54 (削除ここまで) 52 bytes, Dictionary: (削除) 2518 (削除ここまで) 2494 bytes, FP: (削除) 1 (削除ここまで) 0, FN: 0
[ 1ドル = "${1#?*[A-Z]}" ]&&unlzma<z|grep -qix "${1%I}"
Quotes are not needed around the initial 1ドル because "the input is always a single word, all ASCII [A-Za-z]." However, quotes are needed around the parameter expansions because they might generate an empty string.
Examples:
In shell, an exit code of 0 is true (anything else is false). $? is the previous command's exit code.
$ sh in997 colour; echo $?
0
$ sh in997 Colour; echo $?
0
$ sh in997 coLoUr; echo $?
1
$ sh in997 COLOUR; echo $? # I chose to consistently mark these false
1
$ sh in997 Color; echo $?
1
$ sh in997 I; echo $?
0
$ sh in997 i; echo $?
1
$ sh in997 ColourI; echo $? # testing because I strip off one trailing "I"
1
$ sh in997 II; echo $? # ... and "I" isn't in the dictionary (see below)
1
(Examples copied from Nejc's answer plus an all-caps example and two tests for specific corner cases used to vet my i vs I logic. I have no idea why Google Prettify likes Proper Case and CamelCase words when highlighting lang-bash.)
Ungolfed and explained:
if [ "1ドル" = "${1#?*[A-Z]}" ]; then
cat z | unlzma | grep -q -i -x "${1%I}"
fi
The conditional compares the argument with itself after a string manipulation that alters the string if it matches any character followed by any number of characters followed by an uppercase letter (equivalent to s/^..*?[A-Z]// – ignore the ? if you don't understand it), which would compare coLoUr to oUr and Colour to Colour. This controls for mixed case (and fails all-caps), though because it skips the first letter, it doesn't reject i. Yet.
If the conditional matches (there are no capitals after the first letter), unlzma is run on the contents of the file named z, which puts the uncompressed dictionary into standard output. grep then queries for the argument* quietly (-q), without regard to case—we've already controlled for case (-i), and on a whole line (-x).
*The argument is altered a bit: if there is a trailing capital I, it is removed. This is fine because I converted the dictionary's I entry to be a blank line, so it still matches.
The last item that executed dictates the exit value of the script. If the conditional was false, the script exits false. If grep found no match, it (and the script) return false. If grep found a match, it and the script return true.
Dictionary creation:
Wow. There are some awesome dictionary compression schemes in other answers here. I didn't do anything that fancy. There is zero custom compression in this code (unless you count removing DOS linebreaks), just minor prep work that makes it easier to check later on (blanking the I line actually costs a byte!).
I just took the stock dictionary, blanked the I, and compressed the output. I chose LZMA because it compressed this particular file better than zip, gzip, bzip2, xz, or zlib.
Here's my code:
wget -qqO- 'http://pastebin.com/raw.php?i=wh6yxrqp' \
| perl -pne 's/\r//; s/^I$//' | lzma -c > z
wget flags -qqO- suppress all output and sends the HTML content to standard output. That's parsed by perl, which removes the DOS line breaks (\r) and converts the I entry to a blank line (since we stripped the trailing I in grep). That's then passed to lzma which outputs the compressed dictionary into a pipe to file z.
Note that unlzma will refuse to operate on a file lacking the .lzma extension, but it's fine with a pipeline. That means unlzma z fails while unlzma<z (effectively cat z | unlzma) succeeds.
- 364
- 6
- 11