I visited the Rosetta Code, for solution to a problem in a language, which both don't matter here; then checked Perl solution there; then, out of idle curiosity, as it's usual with time wasting e.g. browsing dictionaries, clicked for one more task; and -- I swear (and don't know what in its title attracted me)-- at exactly the 3d task I found a Perl solution:
https://rosettacode.org/wiki/Change_e_letters_to_i_in_words#PerlOut of ALL contestants, the task was misread to include 5-letter words, and even then, the algorithm is broken as it doesn't contain e.g. "crises crisis" pair, etc. So sad :-(. Village idiot, our Perl, no less. No, I don't have editing rights there, nor did I find in page's history how/when it was added.
However, I've been surprised to find it hard and not obvious (to me), how to match the solution's speed/memory. Presumably, if corrected they'd stay the same or close. My initial attempts with e.g. grep/map/split/regexes or what not -- they all were worse.
Finally, here's my solution (under Strawberry):
use strict; use warnings; sub memory { qx( tasklist /nh /fi "PID eq $$" ) =~ m[(\S+ K)$] } use Time::HiRes 'time'; my $t = time; my ( @a, %h ); open my $fh, '<:raw', 'unixdict.txt' or die; while ( <$fh> ) { next unless length > 6; chomp; ( -1 != index $_, 'e' ) ? ( push @a, $_ ) : ( -1 != index $_, 'i' ) ? ( $h{ $_ } = 1 ) : 1 } close $fh; my ( $s, $i ) = ''; exists $h{ $i = tr/e/i/r } and $s .= sprintf "%30s %s\n", $_, $i for @a; print time - $t, "\n"; print memory, "\n"; print $s;
It's on average "0.018 s, 10,100 K" vs "0.033 s, 11,100 K" of similarly modified (but still unfixed) original. Not that "performance" matters for the task, how ever ridiculous this achievement is.
In reply to Perl at Rosetta Code, with one particular example by Anonymous Monk
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |