#! /bin/usr/env ruby # HelloWorldFizzBuzz module HelloWorldFizzBuzz # Sqlite database Module module Sqlite def connect_database @db = Sequel.sqlite # create an items table begin @db.create_table :list do primary_key :id String :language String :helloworld String :fizzbuzz end end # create a dataset from the items table @posts = @db[:list] # populate the table ## Add hello-world && fizz-buzz examples Below (In Alphabetic Order) =begin Example entry: @posts.insert(language: 'programinglanguage', helloworld: " helloworld example ", fizzbuzz: " fizzbuzz example ",) =end ### A @posts.insert(language: 'ada', helloworld: "with Text_IO; procedure Hello_World is begin Text_IO.Put_line('Hello World!'); end Hello_World; ", fizzbuzz: "procedure FizzBuzz( X : Natural ) is function FB( I : Integer ) return String is (if I mod 15 = 0 then 'FizzBuzz' elsif I mod 5 = 0 then 'Buzz'' elsif I mod 3 = 0 then 'Fizz' else "" ); begin for I in 1..X loop Ada.Integer_Text_IO.Put ( I ); Ada.Text_IO.Put ( ' ' & FB(I) ); Ada.Text_IO.New_Line; end loop; end FizzBuzz;") ### B @posts.insert(language: 'basic', helloworld: " 10 PRINT 'Hello World!' 20 GOTO 10 ", fizzbuzz: " 10 DEF FN M(N) = ((A / N) - INT (A / N)) * N 20 FOR A = 1 TO 100 30 LET O$ = "" 40 IF FN M(3) = 0 THEN O$ = 'FIZZ' 50 IF FN M(5) = 0 THEN O$ = O$ + 'BUZZ' 60 IF O$ = "" THEN O$ = STR$ (A) 70 PRINT O$ 80 NEXT A ") ### C @posts.insert(language: 'C', helloworld: "#include main() { printf('Hello World'); } ", fizzbuzz: "#include int main(void) { int i; for(i=1; i<=100; ++i) { if (i % 3 == 0) printf('Fizz'); if (i % 5 == 0) printf('Buzz'); if ((i % 3 != 0) && (i % 5 != 0)) printf('number=%d', i); printf('\n'); } return 0; } ") ### D @posts.insert(language: 'dart', helloworld: "void main() { print('hello world!'); } ", fizzbuzz: "void main() { for (var i = 1; i <= 100; i ++) { if (i % 15 == 0) { print('FizzBuzz'); } else if (i % 3 == 0) { print('Fizz'); } else if (i % 5 == 0) { print('Buzz'); } else { print(i); } } }") ### E @posts.insert(language: 'erlang', helloworld: "-module(hello). -export([hello_world/0]). hello_world() -> io:fwrite('hello, world\n'). ", fizzbuzz: "-module(fizzbuzz). -export([fizzbuzz/1]). -define(MOD_THREE, 'fizz'). -define(MOD_FIVE, 'buzz'). % Helper function to map fizzbuzz across a sequence of numbers fizzbuzz(To) -> lists:map(fun nt/1, lists:seq(1, To)). % initiate the number transform for a Number nt(Number) -> number_transform(Number, []). % transform the number to either nothing or 'fizz' or 'buzz' or 'fizzbuzz' %% NOTE: This style of fizzbuzz allows arbitrary matching %% e.g. add bazz for number where Number rem 7 == 0 number_transform(0, Words) -> lists:flatten(lists:reverse(Words)); number_transform(Number, Words) when Number rem 3 == 0 -> case lists:member(?MOD_THREE, Words) of true -> number_transform(round(Number / 3), Words); false -> number_transform(round(Number / 3), [?MOD_THREE|Words]) end; number_transform(Number, Words) when Number rem 5 == 0 -> case lists:member(?MOD_FIVE, Words) of true -> number_transform(round(Number / 5), Words); false -> number_transform(round(Number / 5), [?MOD_FIVE|Words]) end; number_transform(Number, []) -> Number; number_transform(Number, Words) -> Number, lists:flatten(lists:reverse(Words)). ") ### F ### G ### H ### I ### J ### K ### L ### M ### N ### O ### P @posts.insert(language: 'php', helloworld: "", fizzbuzz: " for ($i = 1; $i <= 100; $i++) { if($i % 3 == 0 && $i % 5 ==0){ echo 'FizzBuzz
'; } else if($i % 3 == 0){ echo 'Fizz
'; } else if($i % 5 == 0){ echo 'Buzz
'; } else { echo $i.'
''; } } ") ### Q ### R @posts.insert(language: 'ruby', helloworld: "puts 'Hello, world!'", fizzbuzz: " def fizzbuzz(n) (1..n).each do |i| if i % 3 == 0 && i % 5 == 0 puts 'fizzbuzz' elsif i % 3 == 0 puts 'fizz' elsif i % 5 == 0 puts 'buzz' else puts i end end end fizzbuzz(100) ") ### S ### T ### U ### V ### W ### X ### Y ### Z end end end