DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #293 - Name the Operations

You'll be given a string with numbers as input. The first two numbers are the operands, while the last three are the result of the operation.

Available operations include:

  • addition
  • subtraction
  • multiplication
  • division

Example

For string: 9 4 5 20 25

Your function must return: subtraction, multiplication, addition

Because:

9 - 4 = 5         subtraction
4 * 5 = 20        multiplication
5 + 20 = 25       addition

Tests

Good luck!


This challenge comes from hubencu_st on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (4)

Collapse
 
willsmart profile image
willsmart

Just a quicky in JS

ops = [
  { name: 'subtraction', op: (a, b) => a - b },
  { name: 'addition', op: (a, b) => a + b },
  { name: 'multiplication', op: (a, b) => a * b },
  { name: 'division', op: (a, b) => a / b },
];

nameTheOps = s => {
  let [left, right, ...rest] = s.trim().split(/\s+/g);
  return rest
    .map(res => {
      const name = ops.find(({ op }) => op(+left, +right) == res)?.name ?? 'unknown op';
      left = right;
      right = res;
      return name;
    })
    .join(', ');
};

nameTheOps('9 4 5 20 25');
//> "subtraction, multiplication, addition"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
peter279k profile image
peter279k

Here is the simple solution with Python and while loop:

import math


def sayMeOperations(string_numbers):
    list_string_numbers = string_numbers.split(' ')

    index = 0
    result_str = ''
    while index < len(list_string_numbers):
        if index == len(list_string_numbers)-2:
            break

        number_one = int(list_string_numbers[index])
        number_two = int(list_string_numbers[index+1])
        result = int(list_string_numbers[index+2])

        if (number_one + number_two) == result:
            result_str += 'addition, '
        elif int(number_one - number_two) == result:
            result_str += 'subtraction, '
        elif int(number_one * number_two) == result:
            result_str += 'multiplication, '      
        elif math.floor(number_one / number_two) == result:
            result_str += 'division, '

        index += 1
    return result_str[0:-2]
Collapse
 
_bkeren profile image
''

JS

const nameOp = stringList => {

 const numList = stringList.split(" ").map(x => +x)
 const _nameOp = (numberList, result= "") => {
   if(numberList.length < 3) return result
   const op = `${numberList[0] + numberList[1] == numberList[2] ? 'addition' : numberList[0] - numberList[1] == numberList[2] ? 'subtraction' : numberList[0] * numberList[1] == numberList[2] ? 'multiplication' : 'division'},`
   return _nameOp(numberList.slice(1),`${result}${op}`)
 }

 return _nameOp(numList).slice(0,-1)
}

Collapse
 
qm3ster profile image
Mihail Malo • Edited

What do we return for 1 1 1 and 2 2 4 ?