8.0 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 5e44412c903586ffb414c94c | 算术格式化 | 23 | 462359 | arithmetic-formatter |
--description--
小学生经常把算术题垂直排列,这样更容易解决。 例如,“235 + 52” 变成:
235
+ 52
-----
Finish the arithmetic_arranger function that receives a list of strings which are arithmetic problems, and returns the problems arranged vertically and side-by-side. 该函数应该接受可选的第二个参数。 当第二个参数设置为 True 时,应显示答案。
示例
函数调用:
arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])
输出:
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
函数调用:
arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)
输出:
32 1 9999 523
+ 8 - 3801 + 9999 - 49
---- ------ ------ -----
40 -3800 19998 474
规则
如果提供的问题格式正确,该函数将返回正确的转换,否则,它将 返回 一个 字符串 来描述对用户有意义的错误。
- 会返回错误的情况:
- 如果提供给函数的问题过多。 The limit is five, anything more will return:
'Error: Too many problems.' - 函数可以接受的运算符是加法和减法。 乘法和除法将返回错误。 本要点中未提及的其他运算符将不需要进行测试。 The error returned will be:
"Error: Operator must be '+' or '-'." - 每个数字(操作数)应该只包含数字。 Otherwise, the function will return:
'Error: Numbers must only contain digits.' - 每个操作数(即运算符每一侧的数字)的宽度最多为四位数字。 Otherwise, the error string returned will be:
'Error: Numbers cannot be more than four digits.'
- 如果提供给函数的问题过多。 The limit is five, anything more will return:
- 如果用户提供了正确格式的问题,返回的转换将遵循以下规则:
- 操作符和两个操作数中最长的一个之间应该有一个空格,操作符将与第二个操作数在同一行,两个操作数的顺序与提供的相同(第一个是上面的,第二个是下面的)。
- 数字应该右对齐。
- 每个问题之间应该有四个空格。
- 每个问题的底部都应该有破折号。 破折号应该单独沿着每个问题的整个长度延伸。 (上面的例子展示了这应该是什么样子。)
--hints--
arithmetic_arranger(["3801 - 2", "123 + 49"]) should return 3801 123\n- 2 + 49\n------ -----.
({
test: () => {
runPython(`
from unittest import TestCase
TestCase().assertEqual(arithmetic_arranger(["3801 - 2", "123 + 49"]), ' 3801 123\\n- 2 + 49\\n------ -----')`);
}
})
arithmetic_arranger(["1 + 2", "1 - 9380"]) should return 1 1\n+ 2 - 9380\n--- ------.
({
test: () => {
runPython(`
from unittest import TestCase
TestCase().assertEqual(arithmetic_arranger(["1 + 2", "1 - 9380"]), ' 1 1\\n+ 2 - 9380\\n--- ------')`);
}
})
arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]) should return 3 3801 45 123\n+ 855 - 2 + 43 + 49\n----- ------ ---- -----.
({
test: () => {
runPython(`
from unittest import TestCase
TestCase().assertEqual(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]), ' 3 3801 45 123\\n+ 855 - 2 + 43 + 49\\n----- ------ ---- -----')`);
}
})
arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]) should return 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ --- ----- ------.
({
test: () => {
runPython(`
from unittest import TestCase
TestCase().assertEqual(arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]), ' 11 3801 1 123 1\\n+ 4 - 2999 + 2 + 49 - 9380\\n---- ------ --- ----- ------')`);
}
})
arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"]) should return 'Error: Too many problems.'.
({
test: () => {
runPython(`
from unittest import TestCase
TestCase().assertEqual(arithmetic_arranger(["44 + 815", "909 - 2", "45 + 43", "123 + 49", "888 + 40", "653 + 87"]), 'Error: Too many problems.')`);
}
})
arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"]) should return "Error: Operator must be '+' or '-'.".
({
test: () => {
runPython(`
from unittest import TestCase
TestCase().assertEqual(arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"]), "Error: Operator must be '+' or '-'.")`);
}
})
arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"]) should return 'Error: Numbers cannot be more than four digits.'.
({
test: () => {
runPython(`
from unittest import TestCase
TestCase().assertEqual(arithmetic_arranger(["24 + 85215", "3801 - 2", "45 + 43", "123 + 49"]), "Error: Numbers cannot be more than four digits.")`);
}
})
arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"]) should return 'Error: Numbers must only contain digits.'.
({
test: () => {
runPython(`
from unittest import TestCase
TestCase().assertEqual(arithmetic_arranger(["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"]), "Error: Numbers must only contain digits.")`);
}
})
arithmetic_arranger(["3 + 855", "988 + 40"], True) should return 3 988\n+ 855 + 40\n----- -----\n 858 1028.
({
test: () => {
runPython(`
from unittest import TestCase
TestCase().assertEqual(arithmetic_arranger(["3 + 855", "988 + 40"], True), " 3 988\\n+ 855 + 40\\n----- -----\\n 858 1028")`);
}
})
arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True) should return 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028.
({
test: () => {
runPython(`
from unittest import TestCase
TestCase().assertEqual(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True), " 32 1 45 123 988\\n- 698 - 3801 + 43 + 49 + 40\\n----- ------ ---- ----- -----\\n -666 -3800 88 172 1028")`);
}
})
--seed--
--seed-contents--
def arithmetic_arranger(problems, show_answers=False):
return problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
--solutions--
def arithmetic_arranger(problems, result=False):
lin1 = ""
lin2 = ""
lin3 = ""
lin4 = ""
if len(problems) > 5:
return 'Error: Too many problems.'
for problem in problems:
[num1, sym, num2] = problem.split()
sign = ['+', '-']
if sym not in sign:
return ("Error: Operator must be '+' or '-'.")
if len(num1) > 4 or len(num2) > 4:
return ("Error: Numbers cannot be more than four digits.")
if not num1.isnumeric() or not num2.isnumeric():
return ("Error: Numbers must only contain digits.")
lin1 += " " + num1 + " " if len(num1) >= len(
num2) else " " * (len(num2) + 2 - len(num1)) + num1 + " "
lin2 += sym + " " + num2 + " " if len(num2) >= len(
num1) else sym + " " * (len(num1) - len(num2) + 1) + num2 + " "
nmax = (len(num1) + 2) if len(num1) >= len(num2) else (len(num2) + 2)
lin3 += "-" * nmax + " "
op = int(num1) + int(num2) if sym == "+" else int(num1) - int(num2)
lin4 += (" " * (nmax - len(str(op)))) + str(op) + " "
arranged_problems = lin1.rstrip() + "\n" + lin2.rstrip(
) + "\n" + lin3.rstrip()
if result:
arranged_problems += "\n" + lin4.rstrip()
return arranged_problems