forked from kennyledet/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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))) |