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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -35
app.py CHANGED
@@ -1,68 +1,69 @@
1
  import gradio as gr
 
2
 
3
  def solve_cryptarithm(equation):
4
- # Parse the words from the equation "SEND + MORE = MONEY"
5
- left, right = equation.split(" = ")
6
- words = left.split(" + ") + [right]
7
-
 
 
 
8
  # Extract unique letters
9
  unique_letters = set("".join(words))
10
  if len(unique_letters) > 10:
11
- return "Error: More than 10 unique letters in equation, unsolvable with digits 0-9."
12
-
13
- # Helper function to convert words to numbers based on current letter-to-digit mapping
14
  def word_to_number(word, mapping):
15
  return int("".join(str(mapping[char]) for char in word))
16
-
17
- # Depth-First Search with Pruning
18
  def dfs(index, letter_to_digit, used_digits):
19
- # If we've assigned all letters, check if equation holds
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)
23
  return left_sum == right_val
24
-
25
- # Current letter to assign
26
  letter = unique_letters_list[index]
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
35
  used_digits.add(digit)
36
-
37
- # Recursive DFS call
38
  if dfs(index + 1, letter_to_digit, used_digits):
39
- return True
40
-
41
  # Backtrack
42
  del letter_to_digit[letter]
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()
52
-
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}"
@@ -71,7 +72,7 @@ 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)
@@ -80,7 +81,7 @@ def process_input(equations):
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",
@@ -88,5 +89,5 @@ interface = gr.Interface(
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()
 
1
  import gradio as gr
2
+ from itertools import permutations
3
 
4
  def solve_cryptarithm(equation):
5
+ try:
6
+ # Parse the left and right parts of the equation
7
+ left, right = equation.replace(" ", "").split("=")
8
+ words = left.split("+") + [right]
9
+ except ValueError:
10
+ return "Invalid format. Use the format 'SEND + MORE = MONEY'."
11
+
12
  # Extract unique letters
13
  unique_letters = set("".join(words))
14
  if len(unique_letters) > 10:
15
+ return "Error: Too many unique letters (more than 10), unsolvable with digits 0-9."
16
+
17
+ # Function to convert words to numbers based on current letter-to-digit mapping
18
  def word_to_number(word, mapping):
19
  return int("".join(str(mapping[char]) for char in word))
20
+
21
+ # Depth-First Search with pruning and backtracking
22
  def dfs(index, letter_to_digit, used_digits):
23
+ # If all letters are assigned, check if the equation holds
24
  if index == len(unique_letters):
25
  left_sum = sum(word_to_number(word, letter_to_digit) for word in words[:-1])
26
  right_val = word_to_number(words[-1], letter_to_digit)
27
  return left_sum == right_val
28
+
29
+ # Get current letter to assign
30
  letter = unique_letters_list[index]
31
+
32
+ # Try assigning each digit to the letter
33
  for digit in range(10):
34
+ # Skip used digits and avoid leading zero for multi-letter words
35
+ if digit in used_digits or (digit == 0 and letter in leading_letters):
36
  continue
37
+
38
+ # Assign the digit and proceed
39
  letter_to_digit[letter] = digit
40
  used_digits.add(digit)
41
+
 
42
  if dfs(index + 1, letter_to_digit, used_digits):
43
+ return True # Found a solution
44
+
45
  # Backtrack
46
  del letter_to_digit[letter]
47
  used_digits.remove(digit)
 
 
48
 
49
+ return False # No solution found along this path
50
+
51
+ # Identify leading letters to avoid leading zeros
52
+ leading_letters = {word[0] for word in words if len(word) > 1}
53
  unique_letters_list = list(unique_letters)
54
  letter_to_digit = {}
55
  used_digits = set()
56
+
57
+ # Start DFS to find a solution
58
  if dfs(0, letter_to_digit, used_digits):
59
+ # Format the result in the desired output format
60
  formatted_result = f"Input: {equation}\n\n"
 
 
61
  for i, word in enumerate(words):
62
+ formatted_result += " ".join(str(letter_to_digit[char]) for char in word)
63
  if i < len(words) - 2:
64
  formatted_result += " + "
65
  elif i == len(words) - 2:
66
  formatted_result += " = "
 
67
  return formatted_result
68
  else:
69
  return f"No solution found for: {equation}"
 
72
  results = []
73
  for equation in equations.splitlines():
74
  equation = equation.strip()
75
+ if equation:
76
  result = solve_cryptarithm(equation)
77
  results.append(result)
78
  return "\n\n".join(results)
 
81
  interface = gr.Interface(
82
  fn=process_input,
83
  inputs=gr.Textbox(
84
+ label="Enter Cryptarithm Equations (one per line)",
85
  placeholder="E.g., SEND + MORE = MONEY\nSEND + MORE = MONEY"
86
  ),
87
  outputs="text",
 
89
  description="Enter cryptarithm equations (like SEND + MORE = MONEY) one per line to find solutions. Each letter represents a unique digit from 0-9."
90
  )
91
 
92
+ # Launch the Gradio app
93
  interface.launch()