Skip to content

Instantly share code, notes, and snippets.

@mandos1995
Created November 19, 2023 06:28
import Foundation
enum OperType: Character {
case plus = "+"
case minus = "-"
case multiply = "*"
case divisor = "/"
func calculate(_ n1: Double, _ n2: Double) -> Double {
switch self {
case .plus: return n1 + n2
case .minus: return n2 - n1
case .multiply: return n1 * n2
case .divisor: return n2 / n1
}
}
}
let n = Int(readLine()!)!
let command = readLine()!
var dict: [Character: Double] = [:]
var numStack: [Double] = []
for i in 0..<n {
dict[Character(UnicodeScalar(i + 65)!)] = Double(readLine()!)!
}
for cmd in command {
if let oper = OperType(rawValue: cmd) {
numStack.append(oper.calculate(numStack.removeLast(), numStack.removeLast()))
} else {
numStack.append(dict[cmd, default: 0])
}
}
print(String(format: "%.2f", numStack.last!))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment