I normally never chime in on Limbic~Region questions as they're usually way over my head, but this one was intriguing.

Assuming arrays of students (from your classroom example) describing their attire selection as such:

my @students = ( [qw(cap shortsleeve jeans leather flipflop)], [qw(beenie longsleeve shorts cloth highheel)], [qw(cap tanktop shorts rope flat)], [qw(beenie longsleeve shorts cloth flipflop)] );

You can transpose the @students array and compare each individual category (hat, shirt, ...). The combinations that show up:

Hat: 0,2 and 1,3 Shirt: 1,3 ...

add to a count for that student pair. The higher the count for the student pair, the more similar they are, the shorter "distance".

You should only need to look at the combinations, in this case 0,1; 0,2; 0,3; 1,2; 1,3; 2,3. The combination with the lowest count is your "winner".

Again, this is a bit over my head, so maybe what I'm proposing is the wrong approach or just as "bad" (if not worse) than your worst case solution.

UPDATE: I have code but am reluctant to post if it's a suboptimal approach and will lead others astray.

UPDATE: Code added below.

use strict; use warnings; use Array::Transpose; my @students = ( [qw(cap shortsleeve jeans leather flipflop)], [qw(beenie longsleeve shorts cloth highheel)], [qw(cap tanktop shorts rope flat)], [qw(beenie longsleeve shorts cloth flipflop)] ); my %compares; for my $x (0..$#students) { for my $y ($x+1..$#students) { $compares{$x.",".$y} = 1 } } my $categories = transpose([$students[0],$students[1],$students[2],$st +udents[3]]); my %diffs; for my $category (0..$#{$categories}) { my %type; my @kidcompare; for my $type (0..$#{$categories->[$category]}) { if (exists($type{$categories->[$category]->[$type]})) { push @kidcompare, $type{$categories->[$category]->[$type]} + . "," . $type; } else { $type{$categories->[$category]->[$type]} = $type } } for (@kidcompare) { $diffs{$_}++ } } my $winner = 4; for (sort(keys(%compares))) { if (exists($diffs{$_})) { if ($diffs{$_} < $winner) { $winner = $diffs{$_} } } else { print "Short Winner $_\n"; exit } } print "Winner $winner\n";
use strict; use warnings; use Array::Transpose; my @students = ( [qw(cap tanktop shorts rope flat)], [qw(cap shortsleeve jeans leather flipflop)], #Winner [qw(beenie longsleeve shorts cloth highheel)], #Winner [qw(beenie longsleeve shorts cloth flipflop)], ); my %compares; for my $x (0..$#students) { for my $y ($x+1..$#students) { $compares{$x.",".$y} = 1 } } my $categories = transpose([$students[0],$students[1],$students[2],$st +udents[3]]); my %type; for my $category (0..$#{$categories}) { for my $type (0..$#{$categories->[$category]}) { if (exists($type{$categories->[$category]->[$type]})) { push @{$type{$categories->[$category]->[$type]}}, $type } else { push @{$type{$categories->[$category]->[$type]}}, $type } } } my %diffs; for (sort(keys(%type))) { if ($#{$type{$_}} > 0) { for my $x (0..$#{$type{$_}}) { for my $y ($x+1..$#{$type{$_}}) { $diffs{$type{$_}->[$x].",".$type{$_}->[$y]} = 1 } } } } my $winner = 4; for (sort(keys(%compares))) { if (exists($diffs{$_})) { if ($diffs{$_} < $winner) { $winner = $diffs{$_} } } else { print "Short Winner $_\n"; exit } } print "Winner $winner\n";

In reply to Re: Help thinking about an alternate algorithm by VinsWorldcom
in thread Help thinking about an alternate algorithm by Limbic~Region

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.