Fun project but fuzzy on logic. "PhoneCALLs"

Rob Cozens rcozens at pon.net
Sat Sep 20 20:11:01 EDT 2003


>I remember a program that you could type in your phone number and it would
>give you all permutations of what possible words could be formed.
>
>I just wanted to see how easy it was. Well... I'm really stumped on the
>logic.

Hi Andy,

I begin with these presumptions:

1.  We are dealing with 7-digit numbers: no area code.

2.  One has a list of all "legal" words up to seven characters in 
length in an array variable, myDictionary.  So that myDictionary[1] 
contains all single-character words, myDictionary[2] contains all 
two-character words, etc.

3.  One has a valid phone number in theNumber

4.  All words are the same length: one can't put blanks in places 
where there are characters.

local validWords

function goodWord thisWord
   return (offset(thisWord,validWords)<>0)
end goodWord

function phoneWords theNumber
   put empty into characterList
   put 0 into characterCount
   repeat for each char theDigit in theNumber
     switch theDigit
       case "2"
         put "ABC" into theCharacters
       break
       case "3"
         put "DEF" into theCharacters
       break
       case "4"
         put "GHI" into theCharacters
       break
       case "5"
         put "JKL" into theCharacters
       break
       case "6"
         put "MNO" into theCharacters
       break
       case "7"
         put "PQRS" into theCharacters
       break
       case "8"
         put "TUV" into theCharacters
       break
       case "9"
         put "WXYZ" into theCharacters
       break
       default
         next repeat
     end switch
     add 1 to characterCount
     put theCharacters into characterList[characterCount]
   end repeat
   put myDictionary[characterCount] into validWords
   put empty into wordList
   put empty into thisWord
   repeat for each char myCharacter1 in characterList[1]
     put myCharacter1 into char 1 of thisWord
     if characterCount is 1 then
       if goodWord(thisWord) then put thisWord&return after wordList
       next repeat
     end if
     repeat for each char myCharacter2 in characterList[2]
       put myCharacter2 into char 2 of thisWord
       if characterCount is 2 then
         if goodWord(thisWord) then put thisWord&return after wordList
         next repeat
       end if
       repeat for each char myCharacter3 in characterList[3]
         put myCharacter3 into char 3 of thisWord
         if characterCount is 3 then
           if goodWord(thisWord) then put thisWord&return after wordList
           next repeat
         end if
         repeat for each char myCharacter4 in characterList[4]
           put myCharacter4 into char 4 of thisWord
           if characterCount is 4 then
             if goodWord(thisWord) then put thisWord&return after wordList
             next repeat
           end if
           repeat for each char myCharacter5 in characterList[5]
             put myCharacter5 into char 5 of thisWord
             if characterCount is 5 then
               if goodWord(thisWord) then put thisWord&return after wordList
               next repeat
             end if
             repeat for each char myCharacter6 in characterList[6]
               put myCharacter6 into char 6 of thisWord
               if characterCount is 6 then
                 if goodWord(thisWord) then put thisWord&return after wordList
                 next repeat
               end if
               repeat for each char myCharacter7 in characterList[7]
                 put myCharacter7 into char 7 of thisWord
                 if goodWord(thisWord) then put thisWord&return after wordList
                end repeat
              end repeat
            end repeat
          end repeat
        end repeat
     end repeat
   end repeat
   return wordList
end phoneWords

The nested repeats could be a candidate for recursion, but that's not 
my strong suit.  This should be easier to understand anyway.      :{`)
-- 

Rob Cozens
CCW, Serendipity Software Company
http://www.oenolog.net/who.htm

"And I, which was two fooles, do so grow three;
Who are a little wise, the best fooles bee."

from "The Triple Foole" by John Donne (1572-1631)



More information about the use-livecode mailing list