Created
November 5, 2010 06:58
-
-
Save groesser3/663757 to your computer and use it in GitHub Desktop.
fifo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "test/unit" | |
class Array | |
def fifo | |
verbrauch = inject(0) {|sum, el| sum+=el.abs if el < 0; sum } | |
res = select{|el| el > 0}.inject([]) do |result, el| | |
if el > verbrauch | |
result << (el - verbrauch) | |
verbrauch = 0 | |
else | |
result << 0 | |
verbrauch -= el | |
end | |
result | |
end | |
res | |
end | |
end | |
p [5,-4,3,6,-2,11,-3,1,1].fifo | |
class TestFiFo < Test::Unit::TestCase | |
def test_0 | |
assert_equal([],[].fifo) | |
end | |
def test_1 | |
assert_equal([1,2],[1,2].fifo) | |
end | |
def test_2 | |
assert_equal([0,2], [1,2,-1].fifo) | |
end | |
def test_3 | |
assert_equal([0,2,3], [3,3,3,-4].fifo) | |
end | |
def test_loesung | |
assert_equal([0, 0, 5, 11, 1, 1], [5,-4,3,6,-2,11,-3,1,1].fifo) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment