File tree Expand file tree Collapse file tree 1 file changed +10
-12
lines changed Expand file tree Collapse file tree 1 file changed +10
-12
lines changed Original file line number Diff line number Diff line change @@ -234,19 +234,17 @@ void main()
234
234
{
235
235
import std.stdio, std.string, std.algorithm, std.conv;
236
236
237
- // Reduce the RPN expression using a stack
238
- readln.split.fold!((stack, op )
237
+ // arr is real[] and sym is the current symbol
238
+ readln.split.fold!((arr, sym )
239
239
{
240
- switch (op)
241
- {
242
- // Generate operator switch cases statically
243
- static foreach (c; "+-*/")
244
- case [c]:
245
- return stack[0 .. $ - 2] ~
246
- mixin("stack[$ - 2] " ~ c ~
247
- " stack[$ - 1]");
248
- default: return stack ~ op.to!real;
249
- }
240
+ static foreach (c; "+-*/")
241
+ if (sym == [c])
242
+ // replace the last 2 elements with the binary op
243
+ return arr[0 .. $-2] ~
244
+ mixin("arr[$-2] " ~ c ~ " arr[$-1]");
245
+
246
+ // sym must be a number
247
+ return arr ~ sym.to!real;
250
248
})((real[]).init).writeln;
251
249
}
252
250
----
You can’t perform that action at this time.
0 commit comments