Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Find words within a matrix of letters
- # Corrected 17-Nov-2021
- matrix = ['nlifeloofw',
- 'liargoyloh',
- 'oanairbens',
- 'troeasonst',
- 'swgndmkdhr']
- # Build a dictionary of all words up to length 10, from Official Scrabble Words 2015
- mywords = []
- fin = open("OSW2015.txt")
- for lin in fin:
- lin = lin.strip()
- if len(lin) < 11:
- mywords.append(lin.lower())
- m2 = ["===HORIZONTAL==="]
- # Use all rows, both forwards and reversed
- for w in matrix:
- m2.append(w)
- m2.append(w[::-1])
- m2.append("===VERTICAL===")
- # Use all columns, both up and down
- for h in range(10):
- w = ""
- for v in range(5):
- w += matrix[v][h]
- m2.append(w)
- m2.append(w[::-1])
- m2.append("===DIAGONAL===")
- # Extract all diagonals upper left to lower right, and reversed
- for vv in range(4, -10, -1):
- w = ""
- v = vv
- for h in range(9, -1, -1):
- if v >= 0:
- w += matrix[v][h]
- v += 1
- if v > 4:
- break
- m2.append(w)
- m2.append(w[::-1])
- # Extract all diagonals upper right to lower left, and reversed
- for vv in range(4, -10, -1):
- w = ""
- v = vv
- for h in range(10):
- if v >= 0:
- w += matrix[v][h]
- v += 1
- if v > 4:
- break
- m2.append(w)
- m2.append(w[::-1])
- found = []
- outline = ""
- for w in m2:
- if w.isupper(): # HORIZONTAL / VERTICAL / DIAGONAL
- print(outline)
- outline = ""
- print(w)
- else:
- for mw in mywords: # Does any word in our dictionary exist in this matrix line?
- if mw not in found: # ignore words already found
- if mw in w:
- outline += mw
- outline += " "
- if len(outline) > 60:
- print(outline)
- outline = ""
- found.append(mw)
- print(outline)
- # Results:
- # ===HORIZONTAL===
- # i o el fe if li life lo loo loof of oo oof ef fil
- # foo fool ole a ar go goy liar oh oy ai ail grail
- # ho holy rai rail yo air an ana be ben bens en ens
- # na ne neb ria sneb as ea eas oe on ons onst roe
- # so son sons st ae no nos or ort os sae
- # ===VERTICAL===
- # lot lots stoln to nog gon fra frae arf ear near ad
- # gi da lor ors bo bok by ko kob ob end lend fon fons
- # sh
- # ===DIAGONAL===
- # om wo woe mo moe ow ran ary nary oi io ag er ern
- # re aa at fa faa ta org la al ed in ned nine de
- # den deni am aal ma maa iris is risk si sir siri od
- # rod do dor es lye lyes ye yes sey oos
- #
- # Process finished with exit code 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement