dev-resources.site
for different kinds of informations.
PWC 293 Similar Dominos Done Badly
Published at
11/21/2024
Categories
pwc
perl
perlweeklychallenge
Author
Bob Lied
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) )
{
}
Articles
12 articles in total
PWC 296 Matchstick Square
read article
PWC 296 String Compression
read article
PWC 293 Similar Dominos Done Badly
currently reading
PWC 293 Similar Dominos Done Badly
read article
PWC 287 Strength in Numbers
read article
PWC 281 Knight's Move
read article
PWC 274 Waiting at the Bus Stop
read article
PWC 269 Two of Us Distributing Elements
read article
PWC 268 Games Numbers Play
read article
PWC 267 Positively Perl Street
read article
PWC 263.1 Don't Sort It, Be Happy
read article
PWC 262 Grep it once, grep it twice
read article
Featured ones: