kendrickfff commited on
Commit
3ca8c04
·
verified ·
1 Parent(s): 2e2c700

fix the DFS algorithm

Browse files

Fix

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 +6 -6
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- from itertools import permutations
3
 
4
  def solve_cryptarithm(equation):
5
  # Parse the words from the equation "SEND + MORE = MONEY"
@@ -28,8 +27,8 @@ def solve_cryptarithm(equation):
28
 
29
  # Try every digit for this letter
30
  for digit in range(10):
31
- # Prune: skip used digits
32
- if digit in used_digits:
33
  continue
34
  # Map the letter to the current digit
35
  letter_to_digit[letter] = digit
@@ -44,8 +43,9 @@ def solve_cryptarithm(equation):
44
  used_digits.remove(digit)
45
 
46
  return False
47
-
48
- # Main DFS invocation
 
49
  unique_letters_list = list(unique_letters)
50
  letter_to_digit = {}
51
  used_digits = set()
@@ -57,7 +57,7 @@ def solve_cryptarithm(equation):
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:
 
1
  import gradio as gr
 
2
 
3
  def solve_cryptarithm(equation):
4
  # Parse the words from the equation "SEND + MORE = MONEY"
 
27
 
28
  # Try every digit for this letter
29
  for digit in range(10):
30
+ # Prune: skip used digits and avoid leading zero for multi-letter words
31
+ if digit in used_digits or (digit == 0 and letter_is_leading[letter]):
32
  continue
33
  # Map the letter to the current digit
34
  letter_to_digit[letter] = digit
 
43
  used_digits.remove(digit)
44
 
45
  return False
46
+
47
+ # Identify which letters are leading letters in each word
48
+ letter_is_leading = {word[0]: True for word in words}
49
  unique_letters_list = list(unique_letters)
50
  letter_to_digit = {}
51
  used_digits = set()
 
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: