in reply to Help thinking about an alternate algorithm
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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Help thinking about an alternate algorithm
by locked_user sundialsvc4 (Abbot) on Jan 15, 2014 at 23:38 UTC | |
|
Re^2: Help thinking about an alternate algorithm
by hdb (Monsignor) on Jan 16, 2014 at 08:21 UTC |