All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Word Ladder Construction (Posted on 2004-01-15) Difficulty: 3 of 5
Write an algorithm to create word ladders such as any of the following:
hard bard bird bind bins
hard ward word wood wool
hard herd head dead dean
hard hand band bind bins
hard harp hasp wasp wisp

where a word is given which must be transformed into some other word (not specified in advance) with the same number of letters, but each letter position changed from its original value, in as many steps as there are letters in the word, each step also being a valid word. This is similar to the various Word Meld puzzles on this site, except there are as many steps as there are letters in the word and each letter is changed, with each letter position being changed exactly once.

Input consists of the starting word only. It is assumed that the number of steps will be the same as the number of letters in the word. Available are files containing lists of words of any given length; call one such file, say WORDS5.TXT containing a list of words of 5 letters each, and so on. The program will find all the eligible word ladders given the words in the list.

The program will, of course, terminate without finding any results if in fact a word ladder cannot be constructed starting with the input word.

  Submitted by Charlie    
Rating: 3.0000 (4 votes)
Solution: (Hide)
Since each letter position within the word has to be changed, but not necessarily in order, we try all permutations of the letter position, which we obtain by permuting the first n digits each time through.

The vary procedure then tries each letter of the alphabet at each of these positions, checking via a binary search that the result is a word, and checking that it is a different word from the one before. The fact that it is following a permutation of the unique first n digits assures that at each new step, the new word is different from words prior to the one previous word.

The parameter l (lower case L) keeps track of the level at which we are, pointing to a digit in the string base$ that tells us which position is to be changed at that level.
DECLARE SUB permute (a$)
DECLARE FUNCTION isWord! (w$)
DECLARE SUB vary (w$, l!)
DIM SHARED hist$(20), base$
INPUT "word:", w$
OPEN w$ + ".mld" FOR OUTPUT AS #1
w$ = LCASE$(LTRIM$(RTRIM$(w$)))
hist$(1) = w$
n = LEN(w$)
w2$ = SPACE$(n)

base$ = LEFT$("123456789", n)
nfact = 1
FOR i = 1 TO n
  nfact = nfact * i
NEXT

FOR p = 1 TO nfact
  vary w$, 1
  permute base$
NEXT


CLOSE

FUNCTION isWord (w$)
  n = LEN(w$)
  w1$ = SPACE$(n)
  OPEN "words" + LTRIM$(STR$(n)) + ".txt" FOR BINARY AS #2
  l = LOF(2) / n
  low = 1: high = l
  DO
    mid = INT((low + high) / 2)
    GET #2, (mid - 1) * n + 1, w1$
    IF w1$ = w$ THEN isWord = 1: CLOSE 2: EXIT FUNCTION
    IF w1$ < w$ THEN low = mid + 1: ELSE high = mid - 1 high
  isWord = 0
  CLOSE 2
END FUNCTION

DEFINT A-Z
SUB permute (a$)
 x$ = ""
 FOR i = LEN(a$) TO 1 STEP -1
  l$ = x$
  x$ = MID$(a$, i, 1)
  IF x$ < l$ THEN EXIT FOR
 IF i = 0 THEN
  FOR j = 1 TO LEN(a$) \ 2
   x$ = MID$(a$, j, 1)
   MID$(a$, j, 1) = MID$(a$, LEN(a$) - j + 1, 1)
   MID$(a$, LEN(a$) - j + 1, 1) = x$
  NEXT
 ELSE
  FOR j = LEN(a$) TO i + 1 STEP -1
   IF MID$(a$, j, 1) > x$ THEN EXIT FOR
  NEXT
  MID$(a$, i, 1) = MID$(a$, j, 1)
  MID$(a$, j, 1) = x$
  FOR j = 1 TO (LEN(a$) - i) \ 2
   x$ = MID$(a$, i + j, 1)
   MID$(a$, i + j, 1) = MID$(a$, LEN(a$) - j + 1, 1)
   MID$(a$, LEN(a$) - j + 1, 1) = x$
  NEXT
 END IF

END SUB

DEFSNG A-Z
SUB vary (w$, l)
    i = VAL(MID$(base$, l, 1))
    FOR a = 97 TO 122
      wTest$ = w$
      MID$(wTest$, i, 1) = CHR$(a)
      IF w$ <> wTest$ THEN
        IF isWord(wTest$) THEN
          good = 1
          IF wTest$ = hist$(l) THEN good = 0
          IF good THEN
            hist$(l + 1) = wTest$
            IF l = LEN(w$) THEN
             FOR k = 1 TO l + 1
               PRINT hist$(k); " ";
               PRINT #1, hist$(k); " ";
             NEXT
             PRINT
             PRINT #1,
            ELSE
             vary wTest$, l + 1
            END IF
          END IF
        END IF
      END IF
    NEXT
END SUB

Comments: ( You must be logged in to post comments.)
  Subject Author Date
SolutionSolution (VB)Penny2004-11-07 08:04:17
The program posted above produces....Penny2004-11-07 08:00:04
re: I think this pseudocode will do itCharlie2004-01-23 14:07:48
SolutionI think this pseudocode will do itPenny2004-01-16 03:30:18
re: can someone review thisCharlie2004-01-15 21:13:41
can someone review thisSurender Taalla2004-01-15 17:31:19
No SubjectSurender Taalla2004-01-15 17:29:46
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (16)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information