home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sources / wanted / 5791 < prev    next >
Encoding:
Text File  |  1993-01-24  |  2.4 KB  |  78 lines

  1. Newsgroups: comp.sources.wanted
  2. Path: sparky!uunet!usc!sol.ctr.columbia.edu!news.unomaha.edu!cwis!mfuhr
  3. From: mfuhr@cwis.unomaha.edu (Michael Fuhr)
  4. Subject: Re: anagram
  5. Message-ID: <mfuhr.727843735@cwis>
  6. Sender: news@news.unomaha.edu (UNO Network News Server)
  7. Organization: University of Nebraska at Omaha
  8. References: <EDSTROM.93Jan23152334@Elmer.hsc.ucalgary.ca>
  9. Distribution: comp
  10. Date: Sun, 24 Jan 1993 02:48:55 GMT
  11. Lines: 65
  12.  
  13. edstrom@Elmer.hsc.ucalgary.ca (John Edstrom) writes:
  14.  
  15. >I'm looking for a program that will take an input string of characters
  16. >or letters and output a list of legal words and phrases that can be
  17. >constructed from the letters in the input.
  18.  
  19. I came up with a quickie perl program that does words; phrases are going
  20. to be a lot harder.  The perl program is a bit slow - 20 secs or so on my
  21. machine to try everything in /usr/dict/words (about 25k words).  A C program
  22. would be almost as simple and probably faster, but the machine I'm on isn't
  23. intended for programming and won't allow me to use cc.
  24.  
  25. Everything between here and my .signature is the program.
  26.  
  27. #!/usr/local/bin/perl
  28. #
  29. # anagram - Print a list of words that an anagram represents.
  30. #
  31. # Usage: anagram <characters>
  32. #
  33. # Run with the characters in the anagram as the argument.  For example:
  34. #
  35. #    anagram lkea
  36. #
  37. # The above invocation will produce the following output:
  38. #
  39. #    kale
  40. #    lake
  41. #    leak
  42. #
  43. # The program first sorts the characters in the anagram specified on the
  44. # command line (e.g., the string "lkae" becomes "aekl").  It then reads
  45. # each word in the dictionary, sorts the characters in the word, and
  46. # compares it with the sorted anagram.  Any match is printed.
  47.  
  48.  
  49. $dictionary = "/usr/dict/words";    # system dictionary - change if
  50.                     # necessary
  51.  
  52. # subroutine sortchars
  53. #
  54. # Argument is a string.  Returns a string containing the same characters
  55. # as the argument, but sorted.
  56. sub sortchars {
  57.     join('', sort split(//, $_[0]));
  58. }
  59.  
  60. # Print usage message if user entered wrong number of arguments.
  61. chop ($progname = `basename $0`);
  62. die "Usage: $progname word\n" unless (@ARGV == 1);
  63.  
  64. # Sort the characters in the anagram.
  65. $charlist = &sortchars($ARGV[0]);
  66.  
  67. # Read words from dictionary and print those that can be represented by
  68. # the anagram.
  69. open(DICT, $dictionary) || die "$dictionary: $!\n";
  70. while (<DICT>) {
  71.     chop;
  72.     print $_, "\n" if ($charlist eq &sortchars($_));
  73. }
  74. close(DICT);
  75. --
  76. Michael Fuhr
  77. mfuhr@cwis.unomaha.edu
  78.