Perl Weekly Challenge 293 gave us a problem that didn't really look that hard, yet I did it wrong at least three times before finishing. It reminded me of the song How to Save a Life, where the refrain goes "Where did I go wrong?"
The Task
You are given a list of dominos, @dominos.
Write a script to return the number of
dominoes that are similar to any other domino.
$dominos[i] = [a, b] and $dominos[j] = [c, d]
are the same if either (a = c and b = d) or
(a = d and b = c).
Example 1
- Input:
@dominos = ([1, 3], [3, 1], [2, 4], [6, 8])
- Output: 2
- Similar Dominos: $dominos[0], $dominos[1]
Example 2
- Input:
@dominos = ([1, 2], [2, 1], [1, 1], [1, 2], [2, 2])
- Output: 3
- Similar Dominos: $dominos[0], $dominos[1], $dominos[3]
Bad Start
First thought: oh, this is one of those find-all-pairs problem. Double loop, count up the matches. Simple.
while ( defined(my $d1 = shift @dominos) )
{
}
Top comments (0)