Commit 50f7d9b
Changed files (1)
spec
heaps_stacks_queues
spec/heaps_stacks_queues/simplify_path_spec.rb
@@ -34,19 +34,21 @@ The simplified path.
DOC
describe "simplify_path" do
- def simplify_path(path)
- result = []
- path.split('/').each do |part|
+ def reduce(path)
+ path.split('/').inject([]) do |stack, part|
case part
- when '.'
+ when '.', ''
when '..'
- result.pop
- when ''
+ stack.pop
else
- result.push(part)
+ stack.push(part)
end
+ stack
end
- "/" + result.join('/')
+ end
+
+ def simplify_path(path)
+ "/#{reduce(path).join('/')}"
end
[