Arithmetic Expression Placeholders
Description:
In Scala, an underscore may be used to create a partially applied version of an infix operator using placeholder syntax. For example, (_ * 3)
is a function that multiplies its input by 3. With a bit of manipulation, this idea can be extended to work on any arbitrary expression.
Create an value/object named x
that acts as a placeholder in an arithmetic expression. The placeholder should support the four basic integer arithmetic operations: addition, subtraction, multiplication, and integral (floor) division. When the expression with placeholders is called, it should fill the placeholders in the expression from left to right (regardless of operator precedence) with the values it is given.
Here are a few examples:
calling (x + 3) with [1] gives 1 + 3 = 4
calling (10 - x) with [4] gives 10 - 4 = 6
calling (x + 2 * x) with [1 3] gives 1 + 2 * 3 = 1 + 6 = 7
calling ((x + 2) * x) with [1 3] gives (1 + 2) * 3 = 3 * 3 = 9
calling (4 * (x / 2)) with [5] gives 4 * (5 / 2) = 4 * 2 = 8
All inputs and outputs to/from the expression will be integer types. All expressions tested in this kata will be valid, i.e. there will be no division by zero and the number of values passed in will always be the same as the number of placeholders.
Note: eval
and exec
are disabled
Similar Kata:
Stats:
Created | Mar 25, 2020 |
Published | Mar 26, 2020 |
Warriors Trained | 628 |
Total Skips | 22 |
Total Code Submissions | 395 |
Total Times Completed | 59 |
Python Completions | 59 |
Total Stars | 16 |
% of votes with a positive feedback rating | 94% of 24 |
Total "Very Satisfied" Votes | 22 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 1 |
Total Rank Assessments | 4 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 4 kyu |
Lowest Assessed Rank | 6 kyu |