LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2002, 03:52 PM   #1
sewer_monkey
Member
 
Registered: May 2002
Location: Toronto, ON, Canada
Distribution: Ubuntu, Debian, RedHat/CentOS
Posts: 624

Rep: Reputation: 31
Talking You a Perl GOD? Prove it!


Hey all!

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...
 
Old 06-13-2002, 04:02 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 06-13-2002, 05:30 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 06-14-2002, 10:11 AM   #4
sewer_monkey
Member
 
Registered: May 2002
Location: Toronto, ON, Canada
Distribution: Ubuntu, Debian, RedHat/CentOS
Posts: 624

Original Poster
Rep: Reputation: 31
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...
 
Old 06-14-2002, 10:38 AM   #5
kahuna
Member
 
Registered: Jun 2002
Location: Grand Rapids, MI
Distribution: Redhat, Slackware
Posts: 78

Rep: Reputation: 15
No, we will *not* be doing any of your homework for you today!
 
Old 06-14-2002, 10:46 AM   #6
sewer_monkey
Member
 
Registered: May 2002
Location: Toronto, ON, Canada
Distribution: Ubuntu, Debian, RedHat/CentOS
Posts: 624

Original Poster
Rep: Reputation: 31
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... 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.
 
Old 06-14-2002, 11:24 AM   #7
shoot2kill
Member
 
Registered: Jan 2002
Location: California
Distribution: Red Hat
Posts: 402

Rep: Reputation: 30
cool....so u did your homework at last.

cheers mate!
 
Old 06-14-2002, 01:04 PM   #8
kahuna
Member
 
Registered: Jun 2002
Location: Grand Rapids, MI
Distribution: Redhat, Slackware
Posts: 78

Rep: Reputation: 15
Hehe! I was only joking if you guys couldn't tell already!
 
Old 06-14-2002, 07:14 PM   #9
sewer_monkey
Member
 
Registered: May 2002
Location: Toronto, ON, Canada
Distribution: Ubuntu, Debian, RedHat/CentOS
Posts: 624

Original Poster
Rep: Reputation: 31
Quote:
Originally posted by shoot2kill
cool....so u did your homework at last.

cheers mate!
Homework?!? I'm a Psychology and Biology double major for cryin' out loud, what does perl have to do with my homework?!
 
Old 06-14-2002, 08:05 PM   #10
shoot2kill
Member
 
Registered: Jan 2002
Location: California
Distribution: Red Hat
Posts: 402

Rep: Reputation: 30
why dun you do computer science? i think you can do better in this field.....
 
Old 06-14-2002, 11:42 PM   #11
sewer_monkey
Member
 
Registered: May 2002
Location: Toronto, ON, Canada
Distribution: Ubuntu, Debian, RedHat/CentOS
Posts: 624

Original Poster
Rep: Reputation: 31
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.
 
Old 06-15-2002, 01:10 AM   #12
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Rep: Reputation: 31
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...
 
Old 06-15-2002, 11:52 AM   #13
sewer_monkey
Member
 
Registered: May 2002
Location: Toronto, ON, Canada
Distribution: Ubuntu, Debian, RedHat/CentOS
Posts: 624

Original Poster
Rep: Reputation: 31
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";
 
Old 06-16-2002, 01:32 AM   #14
tundra
Member
 
Registered: Jun 2002
Location: Koom Valley
Distribution: rh8
Posts: 528

Rep: Reputation: 31
that looks diabolical enough. deserves a certain kind of cackle there. lol!
 
Old 06-16-2002, 10:04 PM   #15
sewer_monkey
Member
 
Registered: May 2002
Location: Toronto, ON, Canada
Distribution: Ubuntu, Debian, RedHat/CentOS
Posts: 624

Original Poster
Rep: Reputation: 31
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
My God! How to?!? cthinh Linux - Newbie 3 07-04-2005 10:11 PM
perl OH MY GOD!!!!!! youssefe2k Programming 1 04-28-2004 10:17 PM
Feel theres not enought support for X and Screen Refresh, Prove me wrong lol :-P ShadowRunner Linux - Newbie 7 11-23-2003 06:58 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
I think I need a perl, apache, cgi GOD WorldBuilder Linux - Software 1 09-21-2003 09:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration