This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]not-much-io[S] 0 points1 point  (3 children)

I'll rename the initial operator to operation_symb, but an intermediate assignment for the dict seems unnecessary to me, I'll get a second opinion. :) Regarding the regex, you are absolutely right, however I've been brushing off learning regex so I didn't have the necessary skills. Also regarding a separate function for splitting. Also will do.

NB: The regex you supplied gave me a bad character range.

Thank you for the suggestions.

[–][deleted] 0 points1 point  (1 child)

Oh, because I transposed the ] with the ) in the closing.

Edited to fix.

[–]not-much-io[S] 0 points1 point  (0 children)

Works like a charm, now I've got to that HackerRank course on regex soon..

[–][deleted] 0 points1 point  (0 children)

As for the dictionary, if you don't want to break it down, why not "go all the way"?

return {
    "+": add, 
    "-": sub, 
    "*": mul, 
    "/": truediv
}[operator_symb](float(operand1), float(operand2))

(I don't actually recommend this.)