Skip to content

Commit

Permalink
Added 99 bottles problem
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonaba committed Mar 21, 2014
1 parent 3545bdf commit 482ab31
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
26 changes: 26 additions & 0 deletions 99_Bootles_Problem/Lua/Yonaba/99bottles.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- 99 bottles of beer implementation
-- See : http://99-bottles-of-beer.net/lyrics.html

-- Pluralization
local function bottles(count)
local pref = (count > 1 and ' bottles ')
or count == 1 and ' bottle '
or 'no more bottle '
return (count == 0 and '' or count) .. pref .. 'of beer'
end

-- Capitalizes the first char of a string
local function caps(str) return str:gsub('^%l',string.upper) end

-- Prints he bottles of beer song
-- nbootles : the number of bottles of beer
local function print_bottles(nbottles)
for i = nbottles, 1, -1 do
io.write(('%s on the wall, %s.\n'):format(caps(bottles(i)), bottles(i)))
io.write(('Take one down and pass it around, %s on the wall.\n\n'):format(bottles(i-1)))
end
io.write(('%s on the wall, %s.\n'):format(caps(bottles(0)), bottles(0)))
io.write(('Go to the store and buy some more, %d bottles of beer on the wall.'):format(nbottles-1))
end

return print_bottles
16 changes: 16 additions & 0 deletions 99_Bootles_Problem/Lua/Yonaba/99bottles_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Tests for monty_hall.lua
local nbottles = require '99bottles'

local total, pass = 1, 1

local function dec(str, len)
return #str < len
and str .. (('.'):rep(len-#str))
or str:sub(1,len)
end

nbottles(99)

print('\n'..('-'):rep(80))
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
:format(total, pass, total-pass, (pass*100/total)))

0 comments on commit 482ab31

Please sign in to comment.