Advertisement
Guest User

y(B)=_allpossibleA_S(B)_N(B).py

a guest
May 13th, 2023
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import tkinter as tk
  2. import tkinter.messagebox as messagebox
  3.  
  4. def generate_A():
  5.     B = entry.get()
  6.     n = len(B)  # Number of digits in B
  7.     B_digits = [int(digit) for digit in B]  # Convert B to a list of digits
  8.     possible_A = find_possible_A(B_digits)
  9.     possible_A.sort()
  10.     listbox.delete(0, tk.END)
  11.     sum_A = 0  # Variable to store the sum of A values
  12.     for A in possible_A:
  13.         listbox.insert(tk.END, str(A))
  14.         sum_A += A
  15.     sum_label.configure(text="Sum of A values: " + str(sum_A))
  16.     num_elements = len(possible_A)
  17.     num_elements_label.configure(text="Number of Elements: " + str(num_elements))
  18.  
  19. def clear_entries():
  20.     entry.delete(0, tk.END)
  21.     listbox.delete(0, tk.END)
  22.     sum_label.configure(text="Sum of A values: ")
  23.     num_elements_label.configure(text="Number of Elements: ")
  24.  
  25. def copy_numbers():
  26.     numbers = listbox.get(0, tk.END)
  27.     if numbers:
  28.         numbers_text = "\n".join(numbers)
  29.         window.clipboard_clear()
  30.         window.clipboard_append(numbers_text)
  31.         messagebox.showinfo("Numbers Copied", "The numbers have been copied to the clipboard.")
  32.     else:
  33.         messagebox.showwarning("No Numbers", "No numbers to copy.")
  34.  
  35. def find_possible_A(B_digits):
  36.     n = len(B_digits)
  37.     possible_A = []
  38.     for i in range(10**(n+1)):
  39.         A_digits = [int(digit) for digit in str(i).zfill(n+1)]  # Convert i to a list of digits
  40.         conditions_met = True
  41.         for j in range(n):
  42.             if abs(A_digits[j+1] - A_digits[j]) != B_digits[j]:
  43.                 conditions_met = False
  44.                 break
  45.         if conditions_met:
  46.             possible_A.append(i)
  47.     return possible_A
  48.  
  49. # Create the main window
  50. window = tk.Tk()
  51. window.title("Possible A Values")
  52.  
  53. # Create input label and entry box
  54. label = tk.Label(window, text="Enter B (n-digit number):")
  55. label.pack()
  56. entry = tk.Entry(window)
  57. entry.pack()
  58.  
  59. # Create clear, generate, and copy buttons
  60. button_frame = tk.Frame(window)
  61. button_frame.pack(pady=10)
  62. clear_button = tk.Button(button_frame, text="Clear", command=clear_entries)
  63. clear_button.grid(row=0, column=0, padx=5)
  64. generate_button = tk.Button(button_frame, text="Generate", command=generate_A)
  65. generate_button.grid(row=0, column=1, padx=5)
  66. copy_button = tk.Button(button_frame, text="Copy Numbers", command=copy_numbers)
  67. copy_button.grid(row=0, column=2, padx=5)
  68.  
  69. # Create listbox to display possible A values
  70. listbox = tk.Listbox(window)
  71. listbox.pack(padx=10, pady=10)
  72.  
  73. # Create label to display the sum of A values
  74. sum_label = tk.Label(window, text="Sum of A values: ")
  75. sum_label.pack()
  76.  
  77. # Create label to display the number of elements
  78. num_elements_label = tk.Label(window, text="Number of Elements: ")
  79. num_elements_label.pack()
  80.  
  81. # Run the GUI
  82. window.mainloop()
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement