Spaces:
Running
Running
File size: 2,137 Bytes
1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c 8e111ac 1fcdf1c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
import itertools
import gradio as gr
def solve_cryptarithm(equation):
# Remove spaces from user input
equation = equation.replace(" ", "")
# Split the equation into left and right sides
left_side, right_side = equation.split('=')
# Get all unique letters from the equation
letters = set(filter(str.isalpha, equation))
letters = ''.join(letters)
# If there are too many letters (more than 10), it's impossible to solve
if len(letters) > 10:
return "Too many letters to solve!"
# Get all unique digit combinations for the letters
digits = '0123456789'
for perm in itertools.permutations(digits, len(letters)):
# Create mapping of letters to numbers
translation = str.maketrans(letters, ''.join(perm))
# Translate the equation to numbers
translated_left_side = left_side.translate(translation)
translated_right_side = right_side.translate(translation)
# Check if there are numbers starting with 0
if any(part.startswith('0') and len(part) > 1 for part in translated_left_side.split('+') + translated_right_side.split('+')):
continue
try:
# Evaluate whether the left side equals the right side
if eval(translated_left_side) == eval(translated_right_side):
return f"Solution found: {translated_left_side} = {translated_right_side}"
except Exception as e:
continue
return "No solution found."
def solve_multiple_equations(equations):
results = []
for equation in equations.splitlines():
if equation.strip(): # Only process non-empty lines
result = solve_cryptarithm(equation)
results.append(result)
return "\n".join(results)
# Create Gradio interface
iface = gr.Interface(
fn=solve_multiple_equations,
inputs=gr.Textbox(lines=10, placeholder="Enter one or more cryptarithm equations here..."),
outputs="text",
title="Cryptarithm Solver",
description="Enter cryptarithm equations (e.g., SEND + MORE = MONEY) on each line."
)
if __name__ == "__main__":
iface.launch()
|