# Python - Arithmetic Arranger Feedback

**URL:** <https://forum.freecodecamp.org/t/python-arithmetic-arranger-feedback/444701>\
**Category:** I Built This\
**Created:** [February 6, 2021, 7:11am UTC](https://forum.freecodecamp.org/t/python-arithmetic-arranger-feedback/444701 "2021-02-06T07:11:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtrivera3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mtrivera3/32/151715_2.png) [@mtrivera3](https://forum.freecodecamp.org/u/mtrivera3)\
**Post date:** [February 6, 2021, 7:11am UTC](https://forum.freecodecamp.org/t/python-arithmetic-arranger-feedback/444701/1 "2021-02-06T07:11:22Z")

</div>

Here is my solution to Arithmetic Arranger project.  
I used PyCharm CE (Community Edition), which helped out in debugging.

```auto
import re

def get_largest_operand(op1, op2):
    if len(op1) > len(op2):
        return op1
    return op2

def is_valid(re_rule, problem):
    return bool(re.fullmatch(re_rule, problem))

def is_valid_size(problems, MAX_SIZE=5):
    return len(problems) <= MAX_SIZE

def arithmetic_arranger(problems, show_answers=False):
    top_row = []
    bottom_row = []
    dash_row = []
    FOUR_SPACES = ' '
    answers = None
    ans_count = None
    
    # For validation
    rules = {
        'is_valid_operator': '[-+]',
        'is_valid_digit': '\d+',
        'is_valid_digit_size': '\d{1,4}'
    }

    if show_answers:
        answers = []
        ans_count = 0

    if not is_valid_size(problems):
        return "Error: Too many problems."

    for problem in problems:
        top_operand, operator, bottom_operand = problem.split()

        if not is_valid(rules['is_valid_digit'], top_operand)\
        or not is_valid(rules['is_valid_digit'], bottom_operand):
            return "Error: Numbers must only contain digits."
        elif not is_valid(rules['is_valid_digit_size'], top_operand)\
        or not is_valid(rules['is_valid_digit_size'], bottom_operand):
            return "Error: Numbers cannot be more than four digits."
        elif not is_valid(rules['is_valid_operator'], operator):
            return "Error: Operator must be '+' or '-'."

        largest_operand = get_largest_operand(top_operand, bottom_operand)
        dash_size = len(largest_operand) + 2 # Two accounts for whitespace and operator

        if show_answers:
            if operator == '-':
                answers.append(int(top_operand) - int(bottom_operand))
            else:
                answers.append(int(top_operand) + int(bottom_operand))
            answers[ans_count] = str(answers[ans_count]).rjust(dash_size)
            ans_count += 1

        top_row.append(top_operand.rjust(dash_size))
        bottom_row.append(operator + bottom_operand.rjust(dash_size - 1)) # account for whitespace
        dash_row.append('-' * dash_size)

    # Add four spaces to each string and create new formatted string
    top_result = FOUR_SPACES.join(top_row)
    bottom_result = FOUR_SPACES.join(bottom_row)
    dash_result = FOUR_SPACES.join(dash_row)

    if show_answers:
        answers_result = FOUR_SPACES.join(answers)
        return '\n'.join([top_result, bottom_result, dash_result, answers_result])

    return '\n'.join([top_result, bottom_result, dash_result])

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 7, 2021, 7:11pm UTC](https://forum.freecodecamp.org/t/python-arithmetic-arranger-feedback/444701/2 "2021-08-07T19:11:24Z")

</div>

Closed on August 7, 2021, 7:11pm UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
