Skip to content

Commit 482ab31

Browse files
committed
Added 99 bottles problem
1 parent 3545bdf commit 482ab31

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- 99 bottles of beer implementation
2+
-- See : http://99-bottles-of-beer.net/lyrics.html
3+
4+
-- Pluralization
5+
local function bottles(count)
6+
local pref = (count > 1 and ' bottles ')
7+
or count == 1 and ' bottle '
8+
or 'no more bottle '
9+
return (count == 0 and '' or count) .. pref .. 'of beer'
10+
end
11+
12+
-- Capitalizes the first char of a string
13+
local function caps(str) return str:gsub('^%l',string.upper) end
14+
15+
-- Prints he bottles of beer song
16+
-- nbootles : the number of bottles of beer
17+
local function print_bottles(nbottles)
18+
for i = nbottles, 1, -1 do
19+
io.write(('%s on the wall, %s.\n'):format(caps(bottles(i)), bottles(i)))
20+
io.write(('Take one down and pass it around, %s on the wall.\n\n'):format(bottles(i-1)))
21+
end
22+
io.write(('%s on the wall, %s.\n'):format(caps(bottles(0)), bottles(0)))
23+
io.write(('Go to the store and buy some more, %d bottles of beer on the wall.'):format(nbottles-1))
24+
end
25+
26+
return print_bottles
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Tests for monty_hall.lua
2+
local nbottles = require '99bottles'
3+
4+
local total, pass = 1, 1
5+
6+
local function dec(str, len)
7+
return #str < len
8+
and str .. (('.'):rep(len-#str))
9+
or str:sub(1,len)
10+
end
11+
12+
nbottles(99)
13+
14+
print('\n'..('-'):rep(80))
15+
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
16+
:format(total, pass, total-pass, (pass*100/total)))

0 commit comments

Comments
 (0)