Skip to content

Instantly share code, notes, and snippets.

@groesser3
Created November 5, 2010 06:59
Show Gist options
  • Save groesser3/663758 to your computer and use it in GitHub Desktop.
Save groesser3/663758 to your computer and use it in GitHub Desktop.
Simple LIFO, FIFO Functions written in CoffeeScript
# ---------------------------------
# Simple LIFO, FIFO Functions
# written in CoffeeScript
# ---------------------------------
verbrauch = (arr, menge) ->
res= []
for el in arr
if el >= 0
if el > menge
res.push (el-menge)
menge = 0
else
res.push 0
menge = menge - el
res
fifo= (arr) ->
res= []
for el in arr
if el < 0
res = verbrauch(res,-el)
else
res.push (el)
res
lifo= (arr) ->
res= []
for el in arr
if el < 0
res.reverse().push(el)
res = fifo(res).reverse()
else
res.push(el)
res
# ---------------------------------
# Test
# ---------------------------------
arr1 = [5,-4,3,6,-2,11,-3,1,1]
# FiFo [0,0,5,11,1,1]
alert "FiFo: ["+arr1+"] -> ["+(fifo arr1)+"]"
# LiFo [1,3,4,8,1,1]
alert "LiFo: ["+arr1+"] -> ["+(lifo arr1)+"]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment