-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefix.rb
55 lines (51 loc) · 1.52 KB
/
prefix.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
=begin
PREFIX EXPRESSIONS
CHALLENGE DESCRIPTION:
You are given a prefix expression. Write a program which evaluates it.
INPUT SAMPLE:
Your program should accept a file as its first argument. The file contains one
prefix expression per line.
For example:
* + 2 3 4
Your program should read this file and insert it into any data structure you
like. Traverse this data structure and evaluate the prefix expression. Each
token is delimited by a whitespace. You may assume that sum ‘+’,
multiplication ‘*’ and division ‘/’ are the only valid operators appearing in
the test data.
OUTPUT SAMPLE:
Print to stdout the output of the prefix expression, one per line.
For example:
20
CONSTRAINTS:
The evaluation result will always be an integer ≥ 0.
The number of the test cases is ≤ 40.
=end
lines = File.readlines(ARGV[0])
OPERATORS = %w[+ * /]
operators = []
accumulator_empty = true
accumulator = nil
lines.each do |line|
characters = line.split(" ")
next if characters == []
characters.each do |character|
if OPERATORS.include?(character)
operators << character
elsif accumulator_empty
accumulator = character.to_f
accumulator_empty = false
else
operator = operators.pop
case operator
when "+"
accumulator += character.to_i
when "*"
accumulator *= character.to_i
when "/"
accumulator /= character.to_i
end
end
end
puts accumulator.to_i
accumulator_empty = true
end