Advertisement
acclivity

pyFindWordsInMatrix

Nov 16th, 2021 (edited)
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. # Find words within a matrix of letters
  2. # Corrected 17-Nov-2021
  3.  
  4. matrix = ['nlifeloofw',
  5.           'liargoyloh',
  6.           'oanairbens',
  7.           'troeasonst',
  8.           'swgndmkdhr']
  9.  
  10. # Build a dictionary of all words up to length 10, from Official Scrabble Words 2015
  11. mywords = []
  12. fin = open("OSW2015.txt")
  13. for lin in fin:
  14.     lin = lin.strip()
  15.     if len(lin) < 11:
  16.         mywords.append(lin.lower())
  17.  
  18. m2 = ["===HORIZONTAL==="]
  19. # Use all rows, both forwards and reversed
  20. for w in matrix:
  21.     m2.append(w)
  22.     m2.append(w[::-1])
  23.  
  24. m2.append("===VERTICAL===")
  25. # Use all columns, both up and down
  26. for h in range(10):
  27.     w = ""
  28.     for v in range(5):
  29.         w += matrix[v][h]
  30.     m2.append(w)
  31.     m2.append(w[::-1])
  32.  
  33. m2.append("===DIAGONAL===")
  34. # Extract all diagonals upper left to lower right, and reversed
  35. for vv in range(4, -10, -1):
  36.     w = ""
  37.     v = vv
  38.     for h in range(9, -1, -1):
  39.         if v >= 0:
  40.             w += matrix[v][h]
  41.         v += 1
  42.         if v > 4:
  43.             break
  44.     m2.append(w)
  45.     m2.append(w[::-1])
  46.  
  47. # Extract all diagonals upper right to lower left, and reversed
  48. for vv in range(4, -10, -1):
  49.     w = ""
  50.     v = vv
  51.     for h in range(10):
  52.         if v >= 0:
  53.             w += matrix[v][h]
  54.         v += 1
  55.         if v > 4:
  56.             break
  57.     m2.append(w)
  58.     m2.append(w[::-1])
  59.  
  60. found = []
  61. outline = ""
  62. for w in m2:
  63.     if w.isupper():         # HORIZONTAL / VERTICAL / DIAGONAL
  64.         print(outline)
  65.         outline = ""
  66.         print(w)
  67.     else:
  68.         for mw in mywords:          # Does any word in our dictionary exist in this matrix line?
  69.             if mw not in found:     # ignore words already found
  70.                 if mw in w:
  71.                     outline += mw
  72.                     outline += "  "
  73.                     if len(outline) > 60:
  74.                         print(outline)
  75.                         outline = ""
  76.                     found.append(mw)
  77. print(outline)
  78.  
  79. # Results:
  80. # ===HORIZONTAL===
  81. # i  o  el  fe  if  li  life  lo  loo  loof  of  oo  oof  ef  fil
  82. # foo  fool  ole  a  ar  go  goy  liar  oh  oy  ai  ail  grail
  83. # ho  holy  rai  rail  yo  air  an  ana  be  ben  bens  en  ens
  84. # na  ne  neb  ria  sneb  as  ea  eas  oe  on  ons  onst  roe
  85. # so  son  sons  st  ae  no  nos  or  ort  os  sae
  86. # ===VERTICAL===
  87. # lot  lots  stoln  to  nog  gon  fra  frae  arf  ear  near  ad
  88. # gi  da  lor  ors  bo  bok  by  ko  kob  ob  end  lend  fon  fons
  89. # sh
  90. # ===DIAGONAL===
  91. # om  wo  woe  mo  moe  ow  ran  ary  nary  oi  io  ag  er  ern
  92. # re  aa  at  fa  faa  ta  org  la  al  ed  in  ned  nine  de
  93. # den  deni  am  aal  ma  maa  iris  is  risk  si  sir  siri  od
  94. # rod  do  dor  es  lye  lyes  ye  yes  sey  oos
  95. #
  96. # Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement