def arithmetic_arranger(problems, val=False):
arranged_problems = ''
if len(problems) > 5:
arranged_problems = "Error: Too many problems."
return arranged_problems
# list of all operations in str format
operations = list(map(lambda x: x.split()[1], problems))
if set(operations) != {'+', '-'} and len(set(operations)) != 2:
arranged_problems = "Error: Operator must be '+' or '-'."
return arranged_problems
numbers = [] # list of all operands in str format
for i in problems:
p = i.split()
numbers.extend([p[0], p[2]])
if not all(map(lambda x: x.isdigit(), numbers)):
arranged_problems = "Error: Numbers must only contain digits."
return arranged_problems
if not all(map(lambda x: len(x) < 5, numbers)):
arranged_problems = "Error: Numbers cannot be more than four digits."
return arranged_problems
top_row = ''
dashes = ''
values = list(map(lambda x: eval(x), problems))
solutions = ''
for i in range(0, len(numbers), 2):
space_width = max(len(numbers[i]), len(numbers[i+1])) + 2
top_row += numbers[i].rjust(space_width)
dashes += '-' * space_width
solutions += str(values[i // 2]).rjust(space_width)
if i != len(numbers) - 2:
top_row += ' ' * 4
dashes += ' ' * 4
solutions += ' ' * 4
bottom_row = ''
for i in range(1, len(numbers), 2):
space_width = max(len(numbers[i - 1]), len(numbers[i])) + 1
bottom_row += operations[i // 2]
bottom_row += numbers[i].rjust(space_width)
if i != len(numbers) - 1:
bottom_row += ' ' * 4
if val:
arranged_problems = '\n'.join((top_row, bottom_row, dashes, solutions))
else:
arranged_problems = '\n'.join((top_row, bottom_row, dashes))
return arranged_problems,
the test did not passed
arithmetic_arranger(["3 + 855", "988 + 40"], True)
should return 3 988\n+ 855 + 40\n----- -----\n 858 1028
.
ILM
March 7, 2024, 7:06pm
2
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
ILM
March 7, 2024, 7:08pm
3
your comma here is having the output as a tuple, where the first element is a string and the second is empty, so first thing remove the comma and try again
Thank you very much colleagues.,
I refactored my code and I think it’s cleaner:
def arithmetic_arranger(problems, *args):
if len(problems) > 5:
return "Error: Too many problems."
arranged_problems = []
for index, value in enumerate(problems):
# ["32", "+", "8"]
operation = value.split(" ")
if operation[1] not in "-+":
return "Error: Operator must be '+' or '-'."
if len(operation[0]) > 4 or len(operation[2]) > 4:
return "Error: Numbers cannot be more than four digits."
try:
value_1 = int(operation[0])
value_2 = int(operation[2])
except ValueError:
return "Error: Numbers must only contain digits."
# calculate the length of each line
longest_val = max(len(operation[0]), len(operation[2]))
width = longest_val + 2
# operation = ["32", "+", "8"]
# output = f"{operation[0]:>{width}}\n{f'{operation[1]} {operation[2]}':>{width}}\n{'-'*width}"
L1 = f"{operation[0]:>{width}}"
L2 = operation[1] + f"{operation[2]:>{width-1}}"
d = '-' * width
try:
arranged_problems[0] += (' ' * 4) + L1
except IndexError:
arranged_problems.append(L1)
try:
arranged_problems[1] += (' ' * 4) + L2
except IndexError:
arranged_problems.append(L2)
try:
arranged_problems[2] += (' ' * 4) + d
except IndexError:
arranged_problems.append(d)
if args:
"""
This runs if the second parameter True is passed in denoting we need to calculate the answer value.
"""
ans = int(operation[0]) + int(operation[2]) if operation[1] == '+' else int(operation[0]) - int(operation[2])
a = f"{str(ans):>{width}}"
try:
arranged_problems[3] += (' ' * 4) + a
except IndexError:
arranged_problems.append(a)
output = f"{arranged_problems[0]}\n{arranged_problems[1]}\n{arranged_problems[2]}"
output = output + f"\n{arranged_problems[3]}" if args else output
return output
Are you still getting an error?
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').
Continuing the discussion from Arithmetic formatter :
hi, not anymore, Ty
1 Like
system
Closed
September 8, 2024, 12:28am
8
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.