5 kyu

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

Algorithms

Stats:

CreatedMar 25, 2020
PublishedMar 26, 2020
Warriors Trained628
Total Skips22
Total Code Submissions395
Total Times Completed59
Python Completions59
Total Stars16
% of votes with a positive feedback rating94% of 24
Total "Very Satisfied" Votes22
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes1
Total Rank Assessments4
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
6 kyu
Ad
Contributors
  • mingmingrr Avatar
  • Blind4Basics Avatar
  • user8436785 Avatar
Ad