kendrickfff commited on
Commit
8e111ac
·
verified ·
1 Parent(s): 4e8471f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -2,58 +2,58 @@ import itertools
2
  import gradio as gr
3
 
4
  def solve_cryptarithm(equation):
5
- # Hilangkan spasi dari input user
6
  equation = equation.replace(" ", "")
7
 
8
- # Pisahkan bagian kiri dan kanan persamaan
9
  left_side, right_side = equation.split('=')
10
 
11
- # Ambil semua huruf unik dari persamaan
12
  letters = set(filter(str.isalpha, equation))
13
  letters = ''.join(letters)
14
 
15
- # Jika huruf terlalu banyak (lebih dari 10), maka tidak mungkin menyelesaikan
16
  if len(letters) > 10:
17
- return "Terlalu banyak huruf untuk diselesaikan!"
18
 
19
- # Dapatkan semua kombinasi angka unik untuk huruf
20
  digits = '0123456789'
21
  for perm in itertools.permutations(digits, len(letters)):
22
- # Buat mapping huruf ke angka
23
  translation = str.maketrans(letters, ''.join(perm))
24
 
25
- # Terjemahkan persamaan ke angka
26
  translated_left_side = left_side.translate(translation)
27
  translated_right_side = right_side.translate(translation)
28
 
29
- # Cek apakah ada angka yang dimulai dengan 0 pada posisi pertama
30
  if any(part.startswith('0') and len(part) > 1 for part in translated_left_side.split('+') + translated_right_side.split('+')):
31
  continue
32
 
33
  try:
34
- # Evaluasi apakah nilai kiri sama dengan nilai kanan
35
  if eval(translated_left_side) == eval(translated_right_side):
36
- return f"Solusi ditemukan: {translated_left_side} = {translated_right_side}"
37
- except:
38
  continue
39
 
40
- return "Tidak ada solusi yang ditemukan."
41
 
42
  def solve_multiple_equations(equations):
43
  results = []
44
  for equation in equations.splitlines():
45
- if equation.strip(): # Hanya memproses baris yang tidak kosong
46
  result = solve_cryptarithm(equation)
47
  results.append(result)
48
  return "\n".join(results)
49
 
50
- # Membuat antarmuka Gradio
51
  iface = gr.Interface(
52
  fn=solve_multiple_equations,
53
- inputs=gr.Textbox(lines=10, placeholder="Masukkan satu atau beberapa persamaan cryptarithm di sini..."),
54
  outputs="text",
55
- title="Solver Cryptarithm",
56
- description="Masukkan persamaan cryptarithm (misal: SEND + MORE = MONEY) di setiap baris."
57
  )
58
 
59
  if __name__ == "__main__":
 
2
  import gradio as gr
3
 
4
  def solve_cryptarithm(equation):
5
+ # Remove spaces from user input
6
  equation = equation.replace(" ", "")
7
 
8
+ # Split the equation into left and right sides
9
  left_side, right_side = equation.split('=')
10
 
11
+ # Get all unique letters from the equation
12
  letters = set(filter(str.isalpha, equation))
13
  letters = ''.join(letters)
14
 
15
+ # If there are too many letters (more than 10), it's impossible to solve
16
  if len(letters) > 10:
17
+ return "Too many letters to solve!"
18
 
19
+ # Get all unique digit combinations for the letters
20
  digits = '0123456789'
21
  for perm in itertools.permutations(digits, len(letters)):
22
+ # Create mapping of letters to numbers
23
  translation = str.maketrans(letters, ''.join(perm))
24
 
25
+ # Translate the equation to numbers
26
  translated_left_side = left_side.translate(translation)
27
  translated_right_side = right_side.translate(translation)
28
 
29
+ # Check if there are numbers starting with 0
30
  if any(part.startswith('0') and len(part) > 1 for part in translated_left_side.split('+') + translated_right_side.split('+')):
31
  continue
32
 
33
  try:
34
+ # Evaluate whether the left side equals the right side
35
  if eval(translated_left_side) == eval(translated_right_side):
36
+ return f"Solution found: {translated_left_side} = {translated_right_side}"
37
+ except Exception as e:
38
  continue
39
 
40
+ return "No solution found."
41
 
42
  def solve_multiple_equations(equations):
43
  results = []
44
  for equation in equations.splitlines():
45
+ if equation.strip(): # Only process non-empty lines
46
  result = solve_cryptarithm(equation)
47
  results.append(result)
48
  return "\n".join(results)
49
 
50
+ # Create Gradio interface
51
  iface = gr.Interface(
52
  fn=solve_multiple_equations,
53
+ inputs=gr.Textbox(lines=10, placeholder="Enter one or more cryptarithm equations here..."),
54
  outputs="text",
55
+ title="Cryptarithm Solver",
56
+ description="Enter cryptarithm equations (e.g., SEND + MORE = MONEY) on each line."
57
  )
58
 
59
  if __name__ == "__main__":