kendrickfff commited on
Commit
d0aa436
·
verified ·
1 Parent(s): 810e7bf

Implement new method: Depth First Search (DFS)

Browse files

Update:

- 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.

Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -2,8 +2,8 @@ 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
 
9
  # Extract unique letters
@@ -51,24 +51,30 @@ def solve_cryptarithm(equation):
51
 
52
  # Start DFS and find solution
53
  if dfs(0, letter_to_digit, used_digits):
54
- return {letter: letter_to_digit[letter] for letter in unique_letters}
 
 
 
 
 
 
55
  else:
56
  return "No solution found"
57
 
58
  def process_input(equations):
59
- results = {}
60
  for equation in equations.splitlines():
61
- result = solve_cryptarithm(equation)
62
- results[equation] = result
63
- return results
64
 
65
 
66
  interface = gr.Interface(
67
  fn=process_input,
68
- inputs=gr.Textbox(label="Enter Cryptarithm Equations (one per line)", placeholder="E.g., SEND + MORE == MONEY"),
69
- outputs="json",
70
  title="Cryptarithm Solver",
71
- description="Enter cryptarithm equations (like SEND + MORE == MONEY) one per line to find solutions. Each letter represents a unique digit from 0-9."
72
  )
73
 
74
  interface.launch()
 
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
 
9
  # Extract unique letters
 
51
 
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: " + equation + "\n\n"
56
+ for word in words:
57
+ formatted_result += " ".join(str(letter_to_digit[char]) for char in word) + " "
58
+ if word != words[-1]:
59
+ formatted_result += "+ " if word != words[-2] else "= "
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
+ result = solve_cryptarithm(equation.strip())
68
+ results.append(result)
69
+ return "\n\n".join(results)
70
 
71
 
72
  interface = gr.Interface(
73
  fn=process_input,
74
+ inputs=gr.Textbox(label="Enter Cryptarithm Equations (one per line)", placeholder="E.g., SEND + MORE = MONEY"),
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()