## DEV Community is a community of 757,079 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Discussion on: Daily Challenge #10 - Calculator

Dylan Paulus • Edited on

Nim.

``````from strutils import split, parseFloat
from sequtils import foldl, map
import re

type CalcKind = enum
vkOperator
vkValue

type
CalcItem = ref object
case Kind: CalcKind
of vkOperator: Operator: string
of vkValue: Value: float

proc toCalcItem(item: string): CalcItem =
if item.match(re"\d+"):
return CalcItem(Kind: CalcKind.vkValue, Value: parseFloat(item))

return CalcItem(Kind: CalcKind.vkOperator, Operator: item)

proc calculate(input: string): float =
var stack: seq[CalcItem] = input.split(" ").map(toCalcItem)
var backStack: seq[CalcItem]

while len(stack) > 0:
let item = stack.pop()

if item.Kind == CalcKind.vkOperator:
if item.Operator == "*":
let curr = backStack.pop()
let other = stack.pop()
backStack.add(CalcItem(Kind: CalcKind.vkValue, Value: other.Value * curr.Value))
elif item.Operator == "/":
let curr = backStack.pop()
let other = stack.pop()
backStack.add(CalcItem(Kind: CalcKind.vkValue, Value: other.Value / curr.Value))
else:
else:

while len(backStack) > 2:
let curr = backStack.pop()
let operator = backStack.pop()
let other = backStack.pop()

if operator.Operator == "+":
backStack.add(CalcItem(Kind: CalcKind.vkValue, Value: curr.Value + other.Value))
else:
backStack.add(CalcItem(Kind: CalcKind.vkValue, Value: curr.Value - curr.Value))

return foldl(backStack, a + b.Value, 0.0)

when isMainModule:
echo \$calculate("2 / 2 + 3 * 4 - 6") #7
``````
Dylan Paulus • Edited on

Did some cleanup using types. No more string->float->string conversions. Adding old solution below!

``````from strutils import split, parseFloat
from sequtils import foldl

proc calculate(input: string): float =
var stack: seq[string] = input.split(" ")
var backStack: seq[string]

while len(stack) > 0:
let item = stack.pop()

if item == "*":
let curr = parseFloat(backStack.pop())
let other = parseFloat(stack.pop())
elif item == "/":
let curr = parseFloat(backStack.pop())
let other = parseFloat(stack.pop())
else:

while len(backStack) > 2:
let item = parseFloat(backStack.pop())
let operator = backStack.pop()
let other = parseFloat(backStack.pop())

if operator == "+":