Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Neat! I disagree, though, with your for- and while-loop construction. I have a weakness for low-byte constructions, of course, but I really think it's easier to read that main loop as

while (<IN>) { chomp; printf "%4.2f $_\n", score (\%rows, \%cols,$_) if length >=$MIN && not /[A-Z]/; }

In fact, given my druthers, I'd invert the logic to

unless length < $MIN || /[A-Z]/;
though I know some would argue against that move.

And while I'm engaged in pointless nitpicking (<grin>), wouldn't it be more appropriate to change the scoring algorithm so that it wouldn't penalize for column shifts that could be managed without moving your hand? As it stands, "asdfdsa" and "asdfghjkl" both score 1.0,though it's obviously much easier to type the former...

Of course, there are more or less infinite variations on how one could score that (which may be why you didn't try), but as a first attempt, how about this?

sub score { my ($rows, $cols, $str) = @_; my ($tot, $ch); my ($r, $c, $lr, $lc); my ($leastc,$mostc); ($ch) = $str =~ /(.)/; ($lr, $lc,$leastc,$mostc) = ($rows->{$ch}, ($cols->{$ch}) x 3); for $ch (split('', $str)) { ($r, $c) = ($rows->{$ch}, $cols->{$ch}); $tot += abs($lr - $r) + abs($lc - $c); $leastc = $c if $c < $leastc; $mostc = $c if $c > $mostc; ($lr, $lc) = ($r, $c); } return ($tot / (length($str) - 1) + ($mostc-$leastc) / 3); }

I don't think that's the best adjustment to apply, though--it's just the easiest. Maybe a floor function there (that is, int ( ($mostc-$leastc) / 3) ))? Hmmm...



If God had meant us to fly, he would *never* have given us the railroads.
    --Michael Flanders


In reply to Re: Words that suck... by ChemBoy
in thread Words that suck... by YuckFoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found