|
| 1 | +# Define a function named arithmetic_arranger that takes a list of arithmetic problems and optionally a flag to show answers |
| 2 | +def arithmetic_arranger(problems, show_answer=False): |
| 3 | + # Check if the number of problems is greater than 5, if so, return an error message |
| 4 | + if len(problems) > 5: |
| 5 | + return "Error: Too many problems." |
| 6 | + |
| 7 | + # Create a dictionary to store different lines of arranged problems |
| 8 | + arranged_problems = { |
| 9 | + "first_line": "", # Store the first line of each problem |
| 10 | + "second_line": "", # Store the second line of each problem |
| 11 | + "dash_line": "", # Store the line containing dashes for formatting |
| 12 | + "answer_line": "" # Store the line containing answers (if show_answer is True) |
| 13 | + } |
| 14 | + |
| 15 | + # Loop through each problem in the list |
| 16 | + for problem in problems: |
| 17 | + # Split the problem into its components: operand1, operator, and operand2 |
| 18 | + operand1, operator, operand2 = problem.split() |
| 19 | + |
| 20 | + # Check if the operator is either '+' or '-'; if not, return an error message |
| 21 | + if operator not in ["+", "-"]: |
| 22 | + return "Error: Operator must be '+' or '-'." |
| 23 | + |
| 24 | + # Check if both operands contain only digits; if not, return an error message |
| 25 | + if not operand1.isdigit() or not operand2.isdigit(): |
| 26 | + return "Error: Numbers must only contain digits." |
| 27 | + |
| 28 | + # Check if the length of operands is greater than 4 digits; if so, return an error message |
| 29 | + if len(operand1) > 4 or len(operand2) > 4: |
| 30 | + return "Error: Numbers cannot be more than four digits." |
| 31 | + |
| 32 | + # Calculate the width needed for formatting based on the length of the operands |
| 33 | + width = max(len(operand1), len(operand2)) + 2 |
| 34 | + |
| 35 | + # Build the first line by right-aligning operand1 and appending it with proper spacing |
| 36 | + arranged_problems["first_line"] += operand1.rjust(width) + " " |
| 37 | + |
| 38 | + # Build the second line by adding the operator, operand2 right-aligned, and proper spacing |
| 39 | + arranged_problems["second_line"] += operator + " " + operand2.rjust(width - 2) + " " |
| 40 | + |
| 41 | + # Build the dash line by adding dashes to match the width of the operands |
| 42 | + arranged_problems["dash_line"] += "-" * width + " " |
| 43 | + |
| 44 | + # If show_answer is True, calculate the answer and build the answer line |
| 45 | + if show_answer: |
| 46 | + if operator == "+": |
| 47 | + answer = str(int(operand1) + int(operand2)) |
| 48 | + else: |
| 49 | + answer = str(int(operand1) - int(operand2)) |
| 50 | + arranged_problems["answer_line"] += answer.rjust(width) + " " |
| 51 | + |
| 52 | + # Concatenate the first line, second line, and dash line to form the arranged output |
| 53 | + arranged_output = arranged_problems["first_line"].rstrip() + "\n" |
| 54 | + arranged_output += arranged_problems["second_line"].rstrip() + "\n" |
| 55 | + arranged_output += arranged_problems["dash_line"].rstrip() |
| 56 | + |
| 57 | + # If show_answer is True, append the answer line to the arranged output |
| 58 | + if show_answer: |
| 59 | + arranged_output += "\n" + arranged_problems["answer_line"].rstrip() |
| 60 | + |
| 61 | + # Return the arranged output |
| 62 | + return arranged_output |
0 commit comments