-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumble.pl
More file actions
executable file
·119 lines (104 loc) · 2.46 KB
/
jumble.pl
File metadata and controls
executable file
·119 lines (104 loc) · 2.46 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/perl -l
use strict;
use warnings;
use List::Util qw/shuffle/;
my $startword = shift;
my @result;
my $DEBUG = 0;
my $word;
print "finding all permutations for $startword:\n";
my @sorted = sort split('',$startword);
permute(join("",@sorted));
exit;
# solve for 2 characters
#
sub permute {
my $list = shift;
my @result;
my $temp;
my $k;
my $l;
my @letters;
my $kval;
my $lval;
my @revlast;
print "permute got input of $list\n";
@letters = sort split('', $list);
if (checkword(\@letters)) {
print "a word: ",@letters,"\n";
}
#print "letters array has ",@letters, "\n" if $DEBUG;
$k = findk(\@letters);
$l = findl($k, \@letters);
while ($k > -1) {
$kval = $letters[$k];
$lval = $letters[$l];
print "k=$k, l=$l\n" if $DEBUG;
print "swapping...\n" if $DEBUG;
$letters[$k] = $lval; $letters[$l] = $kval;
print "after swap, list is ",@letters,"\n" if $DEBUG;
# reverse elements after kindex
# split list after kindex
my @first = @letters[0 .. ${k}];
my @last = @letters[${k}+1 .. scalar(@letters) - 1];
print "split into @first and @last\n" if $DEBUG;
# reverse the second portion
@revlast = reverse(@last);
print "reversing second portion... ", @revlast,"\n" if $DEBUG;
@letters = (@first, @revlast);
print "next sequence: ",@letters if $DEBUG;
if (checkword(\@letters)) {
print "a word: ",@letters,"\n";
}
# find new k,l
$k = findk(\@letters);
$l = findl($k, \@letters);
}
}
sub findk {
my $aref = shift;
my @a = @{$aref};
my $result = -1;
my $end = scalar(@a) - 1;
my $i;
#print "findk got array: ",@a, "\n" if $DEBUG;
#print "end is $end\n" if $DEBUG;
for ($i=0; $i < $end; $i++) {
#print "comparing ",$a[$i], " and ", $a[$i+1], "\n" if $DEBUG;
if ($a[$i] lt $a[$i+1]) {
if ($i > $result) {
$result = $i;
}
}
}
return $result;
}
sub findl {
my $k = shift;
my $aref = shift;
my @a = @{$aref};
my $result = 0;
for (my $i=$k; $i<scalar(@a); $i++) {
if ($a[$k] lt $a[$i]) {
if ($i > $result) {
$result = $i;
}
}
}
return $result;
}
sub checkword {
my $aref = shift;
my @in = @{$aref};
my $in = join('',@in);
my $result;
my $isword = 0;
#print "checking for word: $in...";
$result = `echo $in | aspell -a`;
$result =~ s/^.*International Ispell Version.*$//m;
#print "got result $result\n";
if ($result =~ m/\*/) {
$isword = 1;
}
return $isword;
}