mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-11 09:01:44 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
2.2 KiB
2.2 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6821eca7237de8297eaee79f | Python Challenge 17: Unorder of Operations | 29 | python-challenge-17 |
--description--
Given an array of integers and an array of string operators, apply the operations to the numbers sequentially from left-to-right. Repeat the operations as needed until all numbers are used. Return the final result.
For example, given [1, 2, 3, 4, 5] and ['+', '*'], return the result of evaluating 1 + 2 * 3 + 4 * 5 from left-to-right ignoring standard order of operations.
- Valid operators are
+,-,*,/, and%.
--hints--
evaluate([5, 6, 7, 8, 9], ['+', '-']) should return 3
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(evaluate([5, 6, 7, 8, 9], ['+', '-']), 3)`)
}})
evaluate([17, 61, 40, 24, 38, 14], ['+', '%']) should return 38
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(evaluate([17, 61, 40, 24, 38, 14], ['+', '%']), 38)`)
}})
evaluate([20, 2, 4, 24, 12, 3], ['*', '/']) should return 60
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(evaluate([20, 2, 4, 24, 12, 3], ['*', '/']), 60)`)
}})
evaluate([11, 4, 10, 17, 2], ['*', '*', '%']) should return 30
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(evaluate([11, 4, 10, 17, 2], ['*', '*', '%']), 30)`)
}})
evaluate([33, 11, 29, 13], ['/', '-']) should return -2
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(evaluate([33, 11, 29, 13], ['/', '-']), -2)`)
}})
--seed--
--seed-contents--
def evaluate(numbers, operators):
return numbers
--solutions--
def do_math(a, b, operator):
if operator == '+':
return a + b
elif operator == '-':
return a - b
elif operator == '*':
return a * b
elif operator == '/':
return a / b
else:
return a % b
def evaluate(numbers, operators):
total = numbers[0]
for i in range(1, len(numbers)):
operator = operators[(i - 1) % len(operators)]
total = do_math(total, numbers[i], operator)
return total