Spaces:
Running
Implement new method: Depth First Search (DFS)
Browse filesUpdate:
- Implemented Depth First Search (DFS) to explore possible digit assignments for each letter, with pruning to discard any paths that can't yield a valid solution. This optimization speeds up runtime by eliminating unfeasible assignments early.
- Added backtracking to revert and try alternative assignments if a partial solution doesn’t lead to a valid final solution.
How This Works:
- The solver first extracts all unique letters from the puzzle.
- DFS explores each possible digit for these letters.
- Pruning: The solver skips digits already in use and only continues exploring if the partial assignment can still lead to a valid solution.
- Backtracking: If a particular assignment fails, the solver backtracks to try the next possible digit, ensuring all potential solutions are explored efficiently.
@@ -2,7 +2,7 @@ import gradio as gr
|
|
2 |
from itertools import permutations
|
3 |
|
4 |
def solve_cryptarithm(equation):
|
5 |
-
# Parse the words from the equation
|
6 |
left, right = equation.split(" = ")
|
7 |
words = left.split(" + ") + [right]
|
8 |
|
@@ -17,6 +17,7 @@ def solve_cryptarithm(equation):
|
|
17 |
|
18 |
# Depth-First Search with Pruning
|
19 |
def dfs(index, letter_to_digit, used_digits):
|
|
|
20 |
if index == len(unique_letters):
|
21 |
left_sum = sum(word_to_number(word, letter_to_digit) for word in words[:-1])
|
22 |
right_val = word_to_number(words[-1], letter_to_digit)
|
@@ -52,29 +53,40 @@ def solve_cryptarithm(equation):
|
|
52 |
# Start DFS and find solution
|
53 |
if dfs(0, letter_to_digit, used_digits):
|
54 |
# Format the solution as per the user's requirements
|
55 |
-
formatted_result = "Input:
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
60 |
return formatted_result
|
61 |
else:
|
62 |
-
return "No solution found"
|
63 |
|
64 |
def process_input(equations):
|
65 |
results = []
|
66 |
for equation in equations.splitlines():
|
67 |
-
|
68 |
-
|
|
|
|
|
69 |
return "\n\n".join(results)
|
70 |
|
71 |
-
|
72 |
interface = gr.Interface(
|
73 |
fn=process_input,
|
74 |
-
inputs=gr.Textbox(
|
|
|
|
|
|
|
75 |
outputs="text",
|
76 |
title="Cryptarithm Solver",
|
77 |
description="Enter cryptarithm equations (like SEND + MORE = MONEY) one per line to find solutions. Each letter represents a unique digit from 0-9."
|
78 |
)
|
79 |
|
|
|
80 |
interface.launch()
|
|
|
2 |
from itertools import permutations
|
3 |
|
4 |
def solve_cryptarithm(equation):
|
5 |
+
# Parse the words from the equation "SEND + MORE = MONEY"
|
6 |
left, right = equation.split(" = ")
|
7 |
words = left.split(" + ") + [right]
|
8 |
|
|
|
17 |
|
18 |
# Depth-First Search with Pruning
|
19 |
def dfs(index, letter_to_digit, used_digits):
|
20 |
+
# If we've assigned all letters, check if equation holds
|
21 |
if index == len(unique_letters):
|
22 |
left_sum = sum(word_to_number(word, letter_to_digit) for word in words[:-1])
|
23 |
right_val = word_to_number(words[-1], letter_to_digit)
|
|
|
53 |
# Start DFS and find solution
|
54 |
if dfs(0, letter_to_digit, used_digits):
|
55 |
# Format the solution as per the user's requirements
|
56 |
+
formatted_result = f"Input: {equation}\n\n"
|
57 |
+
|
58 |
+
# Convert each word to its number representation
|
59 |
+
for i, word in enumerate(words):
|
60 |
+
formatted_result += " ".join(str(letter_to_digit[char]) for char in word)
|
61 |
+
if i < len(words) - 2:
|
62 |
+
formatted_result += " + "
|
63 |
+
elif i == len(words) - 2:
|
64 |
+
formatted_result += " = "
|
65 |
+
|
66 |
return formatted_result
|
67 |
else:
|
68 |
+
return f"No solution found for: {equation}"
|
69 |
|
70 |
def process_input(equations):
|
71 |
results = []
|
72 |
for equation in equations.splitlines():
|
73 |
+
equation = equation.strip()
|
74 |
+
if equation: # Ensure it's not an empty line
|
75 |
+
result = solve_cryptarithm(equation)
|
76 |
+
results.append(result)
|
77 |
return "\n\n".join(results)
|
78 |
|
79 |
+
# Set up Gradio interface
|
80 |
interface = gr.Interface(
|
81 |
fn=process_input,
|
82 |
+
inputs=gr.Textbox(
|
83 |
+
label="Enter Cryptarithm Equations (one per line)",
|
84 |
+
placeholder="E.g., SEND + MORE = MONEY\nSEND + MORE = MONEY"
|
85 |
+
),
|
86 |
outputs="text",
|
87 |
title="Cryptarithm Solver",
|
88 |
description="Enter cryptarithm equations (like SEND + MORE = MONEY) one per line to find solutions. Each letter represents a unique digit from 0-9."
|
89 |
)
|
90 |
|
91 |
+
# Launch Gradio app
|
92 |
interface.launch()
|