blob: b80e27a333eb77cec310373bc91d40bfa1744d6d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# Perl Weekly Challenge - 005
# Challenges #1 and #2
# (This program solves both challenges.)
#
# See
# engineering.purdue.edu/~mark/pwc-005.pdf
# for more information.
# Run using Perl 6.
use v6;
#
# Challenge #1.
#
# Read the words in "words6.txt" to @word.
my @word = lines 'words6.txt'.IO;
# Define %hash.
# Each key is a string with the letters in lexigraphic order.
# Each value is an array of words that use those letters.
my Array %hash;
# Construct the array.
for (@word)
{
# The key for "family" is "amfily".
my $key = .comb(/./).sort.join;
# Add the current word to the hash.
%hash{$key}.push($_);
}
# Print the array.
# for (%hash.keys.sort) -> $key
# {
# print "$key:";
# for (%hash{$key}.Array)
# {
# print " $_";
# }
# print "\n";
# }
# Pick a random word.
my $word = @word.pick;
# Convert $word to $key.
my $key = $word.comb(/./).sort.join;
# Print the output.
print "Challenge 1\n";
print "print all anagrams for a word\n";
print "original word: $word\n";
print "anagrams: ";
for (%hash{$key}.Array.sort)
{
($word eq $_) or print " $_";
}
print "\n";
#
# Challenge 2.
#
my $n = 0;
for (%hash.keys)
{
my $t = %hash{$_}.Array.elems;
if ($t > $n)
{
$n = $t;
$key = $_;
}
}
# Print the output
print "\n";
print "Challenge 2\n";
print "sequence of letters with most anagrams\n";
print "letters: $key\n";
print "anagrams:";
for (%hash{$key}.Array.sort)
{
($word eq $_) or print " $_";
}
print "\n";
|