Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Really slow sort, but useful.

by BrowserUk (Pope)
on Nov 29, 2002 at 00:22 UTC ( #216398=CUFP: print w/ replies, xml ) Need Help??

I recently had occasion to try and determine the relative densities of blocks of different characters in the default font for my CLI. First I just printed out one line of each character on the screen and started to write down a list putting them in order of relative density, but this rapidly became untenable. So I set about trying to write something that would allow me to at least partially automate the process. After writing a skeleton with a couple of for loops it struck me that what I was doing was sorting the characters by a visual criteria.

Whilst I couldn't think of a simple way of having the script make the determination for me, I realised that I could use the built-in sort function to take care of the houskeeping and came up with the following script.

#! perl -slw use strict; use Term::ReadKey; sub key{ my $key; ReadMode 4; 1 while not defined( $key = ReadKey(0.1) ); ReadMode 0; return $key; } my @c = (32..127); @c = sort { system 'cls'; print chr($a) x 16, ' ', chr($b) x 16, ' ', chr($a) x 16, ' ', chr +($b) x 16 for 1 .. 8; print ''; print chr($b) x 16, ' ', chr($a) x 16, ' ', chr($b) x 16, ' ', chr +($a) x 16 for 1 .. 8; print ''; print chr($a) x 16, ' ', chr($b) x 16, ' ', chr($a) x 16, ' ', chr +($b) x 16 for 1 .. 8; print ''; print chr($b) x 16, ' ', chr($a) x 16, ' ', chr($b) x 16, ' ', chr +($a) x 16 for 1 .. 8; print ''; print 'Enter 1 if top left is darker, 2 if they are the same, 3 if + top right is darker'; key()-2; } @c; print $_, ' : ', chr() x 50 for @c;

Whilst this particular task is not likely to be needed very often, the basic idea could be used for anything that would benefit from the A/B comparison technique. Whether it's putting your favorite bands in order of preference, or deciding which college to go to. Basically anything where it's easier to make a judgement by considering 2 things at a time rather than the a whole group together.

Silly idea, but I found it useful:)


Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Comment on Really slow sort, but useful.
Download Code
Re: Really slow sort, but useful.
by theorbtwo (Prior) on Nov 29, 2002 at 04:26 UTC

    I like the idea, but it has a couple of flaws. First off, it's quite possible for humans to give inconsistent choices, such that a>b && b>c && a<c. Obviously, such a set of choices will not give well-sorted output. Worse, it will cause a segfault with some perls. Secondly, it won't neccessarly give the same ordering that different pairs, or considering things in groups, would give, because humans aren't really exact in these things. Thirdly, in this purticular case, there's a fully automatic method you could use -- render the character to a bitmap of some sort, and take the mean color of the bitmap. (This has faults, though -- which is lighter, '-' or '!'. Since the bounding-box of the ! is tight, it's very dark. The bounding-box of the - is much looser, because bbs of chars are always computed as being a fixed height, so it's ratehr light. However, a human would probably call them about even.)


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      Dealing with the last point first. As the comparison was for a CLI, the bounding box are always the same as its fixed pitch fonts I'm dealing with. I did consider trying to grab bitmaps and determine it that way, but given that the use is for human perception rather than machine perception that is difficult. Even dealing with monochrome becomes dependant not just on the number of pels that are on relative to those off, it also depends on the concentration of those pels. It further depends on the way the pels in adjacent cells intereact.

      I found that but not trying to hard to judge it, but rather reacting to the obvious choices as appropriate and opting for "the same" whenever I didn't immediately perceive a difference, I ended up with a fairly good result. It probably wasn't perfect, but then no lives were dependant upon the accuracy of the conclusion:) For my purposes I only needed to grade them approximately as I then chose the two from the ends, space and #, and then 6 roughly evenly spaced throughout the range.

      I did find that it helped if I reduced the size of the font to the smallest available on my system (5pt Lucida TT or a 4x6 raster) as when I could no longer make out what the chars were and stopped trying to decide which was darker on the basis of logic and just went with my visual perception.

      Your right that humans aren't consistant, but that would always be true, regardless of the method used to marshall and record the decisions made.

      I have not been able to create a seg fault even when I just deliberately tried to make inconsistant decisions. I am unsure as to how that would occur? It's not like the sort would 'remember' my decisions, It's only ever comparing two values at a time?

      YMMV as they say:)... I wouldn't rely upon it for anything important, but it served it purpose.


      Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
      Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
      Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
      Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

        Oh, sort does remember you choices. It's not that it keeps a record, but it is remembered in the way the data is shuffled around. Otherwise, there wouldn't be O (n log n) sorts.

        As for the segfaults, pre-5.6 perls used the qsort available in your C libraries. And that might segfault when given inconsistent choices. 5.6.x has its own quicksort, which will not segfault when given inconsistent choices. From 5.8 on, you can actually choice between mergesort (default) and quicksort.

        Abigail

Re: Really slow sort, but useful.
by submersible_toaster (Chaplain) on Dec 16, 2002 at 03:45 UTC

    Perhaps (though overengineering is often discouraged) you might use AI::Fuzzy to build and interrogate the Darker than the "^" char, lighter than the "." char properties of "~".

    I should point out that I have been really meaning to get around to looking at that module for a few weeks, but not actually used it yet. :( !

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://216398]
Approved by TStanley
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-07-12 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (238 votes), past polls