ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Since I am a rather lazy person (too lazy for my own good), I was wondering if any of you perl godz would like to help a lazy-ass person like me...
What I am looking for is a script that accepts a single word (scrambled) as input, finds all the permutations of letters in that word, and searches for each permutation in a text file (such as /usr/share/dict/linux.words). Once a match is found, it should be dumped to STDOUT/STDERR.
I looked at the perldocs, and there's a sample permuting function there, which works, but I'm too lazy to change it to suit my needs. I am even too lazy to solve newspaper scrambled word puzzles on my own...
are we really supposed to find it amusing that you are this lazy? what's the point in it being in perl if you don't even want to understand it. a little bit of effort makes the world go round, this is just insulting i think.
Originally posted by Tinkster
What's the hourly wage? :P > US$ 60 and I'll consider
With an attitude like that, the linux kernel would be worse off than the Windoze kernel when it comes to stability. Then again, my attitude is no better, so I shouldn't be talking...
Originally posted by acid_kewpie are we really supposed to find it amusing that you are this lazy? what's the point in it being in perl if you don't even want to understand it. a little bit of effort makes the world go round, this is just insulting i think.
Sorry, didn't mean to insult/offend you, I just have an odd sense of humour... Here's my version's screen dump:
Code:
[rouben@homenet rouben]$ ./unscramble
Enter scrambled word: slit
OK, trying to figure out what the heck "slit" is, hold on this may take a while...
slit
silt
list
And here's the source code, if anyone's interested...
Code:
#!/usr/bin/perl
# A really handy tool for solving those scrmbled word puzzles... couch potato
# style! Based on mjd_permute.
# The system dictionary file, full path please
$dictionary = "/usr/dict/words";
# The full path to the grep command (this may be optional, see below)
$grep = "/usr/local/bin/grep";
# Do you wanna use grep or an inefficient sequential search algorithm? ;)
# Too lazy to implement binary search... then again, I see no point with grep
# readily available to do the dirty work! ;)
$usegrep = 'yes';
print "Enter scrambled word: ";
my $word = <STDIN>;
# Get rid of the trailing newline...
chop($word);
print "OK, trying to figure out what the heck \"$word\" is, hold on this may take a while...\n";
# Split the entered word into letters
my @data = split(/ */, $word);
my $num_permutations = factorial(scalar @data);
for (my $i=0; $i < $num_permutations; $i++) { # Da magic for loop
my @permutation = @data[n2perm($i, $#data)];
$currentpermutation = join("",@permutation);
# If we're not supposed to use grep...
if (uc($usegrep) ne 'YES') {
open(DICT, $dictionary);
while (<DICT>) {
$dictword = $_;
chop($dictword);
if ($dictword eq $currentpermutation) { print $currentpermutation."\n"; }
}
close(DICT);
} else { # If we're supposed to use grep...
# Make grep to the dirty work!!! Yeeeha!
system($grep." ^".$currentpermutation." ".$dictionary);
} # End of the if that decides whether we're supposed to use grep...
} # ENd of da magic for loop
# Utility function: factorial with memoizing
BEGIN {
my @fact = (1);
sub factorial($) {
my $n = shift;
return $fact[$n] if defined $fact[$n];
$fact[$n] = $n * factorial($n - 1);
}
}
# n2pat($N, $len) : produce the $N-th pattern of length $len
sub n2pat {
my $i = 1;
my $N = shift;
my $len = shift;
my @pat;
while ($i <= $len + 1) { # Should really be just while ($N) { ...
push @pat, $N % $i;
$N = int($N/$i);
$i++;
}
return @pat;
}
# pat2perm(@pat) : turn pattern returned by n2pat() into
# permutation of integers. XXX: splice is already O(N)
sub pat2perm {
my @pat = @_;
my @source = (0 .. $#pat);
my @perm;
push @perm, splice(@source, (pop @pat), 1) while @pat;
return @perm;
}
# n2perm($N, $len) : generate the Nth permutation of $len objects
sub n2perm {
pat2perm(n2pat(@_));
}
Man, I hate obscure code... Coding's not my thing, I'm more of an IT guy... Networking, digging up systems, spare parts, upgrades, servers... That's my cup of tea.
Originally posted by shoot2kill why dun you do computer science? i think you can do better in this field.....
Nah... I don't like coding as much as I like tinkering with drivers, servers, networks... You know, more like IT stuff, and no university has a program that I really like... They all seem to focus theoretical comp sci, development... That's not my cup of tea.
Originally posted by tundra and it looks like no one's able to to better than you yet sewer primate. lol!
don't look at me... never touched perl before...
Hehehe... Trust me, there are many freaks out there that enjoy writing programs that look like a bunch of nonsense... Here's a good example (try running this, don't worry, it just outputs a few lines of text):
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.