All SolutionsAll Solutions
🪢
Hangman Solver
Week 47, 2026
Python - Read, Filter, Match & Write | BMC | Python Solutions
def isMatch(a:str, b:str) -> bool:
if len(a) == len(b):
for i in range(len(a)):
if (a[i] != b[i] and a[i] != '_'):
return False
return True
return False
def readToList(fileName:str) -> list[str | None]:
data = []
try:
file = open(fileName, 'r')
for line in file:
data.append(line.rstrip().lower())
file.close()
print(f"[RTL] {len(data)} line read to list.")
except Exception as e:
print(e)
return data
def writeToFile(fileName:str, data:list[str]) -> None:
try:
file = open(fileName, 'w')
for line in data:
file.write(line+'\n')
file.close()
print("[WTF] Ans.txt made..")
except Exception as e:
print(e, '[WTF] File problem :/')
def main() -> None:
inputs = [
"a___e",
"___z",
"u___",
"a_____a",
"a_b___",
"__oo__",
"e_____t",
"_y____",
"pro____",
"___________________"
]
data = readToList("words-strict.txt")
ans = []
for i in inputs:
ans.append(f"--- Matches for {i} ---")
for word in data:
if isMatch(i, word):
ans.append(word)
writeToFile("ans.txt", ans)
if __name__ == "__main__":
main()
Prepare map, scan dictionary, match mask with word | greenya | Odin Solutions
package main
import "core:fmt"
import "core:strings"
WORDS_TXT := #load("words-strict.txt", string)
// INPUT_MASKS := [] string { "_a__t_", "b__", "____z" }
INPUT_MASKS := [] string { "a___e", "___z", "u___", "a_____a", "a_b___", "__oo__", "e_____t", "_y____", "pro____", "___________________" }
main :: proc () {
result: map [string] [dynamic] string
for mask in INPUT_MASKS do result[mask] = {}
for word in strings.split_lines_iterator(&WORDS_TXT) {
for mask in INPUT_MASKS {
if matching(mask, word) {
append(&result[mask], word)
}
}
}
for mask in INPUT_MASKS {
fmt.printfln("--- Matches for %s ---", mask)
for s in result[mask] {
fmt.println(s)
}
}
}
matching :: proc (mask, word: string) -> bool #no_bounds_check {
if len(mask) != len(word) do return false
for i:=0; i<len(mask); i+=1 {
if mask[i] == '_' do continue
if mask[i] == word[i] do continue
return false
}
return true
}