LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   You a Perl GOD? Prove it! (https://www.linuxquestions.org/questions/programming-9/you-a-perl-god-prove-it-23363/)

sewer_monkey 06-13-2002 03:52 PM

You a Perl GOD? Prove it!
 
Hey all!

Since I am a rather lazy person :D (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. :D I am even too lazy to solve newspaper scrambled word puzzles on my own... ;) :p

acid_kewpie 06-13-2002 04:02 PM

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.

Tinkster 06-13-2002 05:30 PM

Re: You a Perl GOD? Prove it!
 
Quote:

Originally posted by sewer_monkey
I am even too lazy to solve newspaper scrambled word puzzles on my own... ;) :p
What's the hourly wage? :P > US$ 60 and I'll consider

sewer_monkey 06-14-2002 10:11 AM

Re: Re: You a Perl GOD? Prove it!
 
Quote:

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... :eek:

kahuna 06-14-2002 10:38 AM

No, we will *not* be doing any of your homework for you today!

sewer_monkey 06-14-2002 10:46 AM

Quote:

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... :p 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.

shoot2kill 06-14-2002 11:24 AM

cool....so u did your homework at last. :D

cheers mate!

kahuna 06-14-2002 01:04 PM

Hehe! :) I was only joking if you guys couldn't tell already!

sewer_monkey 06-14-2002 07:14 PM

Quote:

Originally posted by shoot2kill
cool....so u did your homework at last. :D

cheers mate!

Homework?!? I'm a Psychology and Biology double major for cryin' out loud, what does perl have to do with my homework?! :D

shoot2kill 06-14-2002 08:05 PM

why dun you do computer science? i think you can do better in this field.....

sewer_monkey 06-14-2002 11:42 PM

Quote:

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.

tundra 06-15-2002 01:10 AM

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... ;)

sewer_monkey 06-15-2002 11:52 AM

Quote:

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):

Code:

#!/usr/bin/perl
@a=(Lbzjoftt,Inqbujfodf,
Hvcsjt); $b="Lbssz Wbmm"
;$b =~ y/b-z/a-z/ ; $c =
" Tif ". @a ." hsfbu wj"
."suvft pg b qsphsbnnfs"
. ":\n";$c =~y/b-y/a-z/;
print"\n\n$c ";for($i=0;
$i<@a; $i++) { $a[$i] =~
y/b-y/a-z/;if($a[$i]eq$a
[-1]){print"and $a[$i]."
;}else{ print"$a[$i], ";
}}print"\n\t\t--$b\n\n";


tundra 06-16-2002 01:32 AM

that looks diabolical enough. deserves a certain kind of cackle there. lol!

sewer_monkey 06-16-2002 10:04 PM

Quote:

Originally posted by tundra
that looks diabolical enough. deserves a certain kind of cackle there. lol!
Have you tried running it yet? Don't worry, it's harmless. I just don't want to spoil it for everyone by posting the output, it's amazing...


All times are GMT -5. The time now is 04:15 PM.