Merge pull request #3 from MaximumGeeker/master
authorOscar J Rodriguez <josuer08@gmail.com>
Fri, 19 Feb 2021 14:27:12 +0000 (09:27 -0500)
committerGitHub <noreply@github.com>
Fri, 19 Feb 2021 14:27:12 +0000 (09:27 -0500)
Added fizzbuzz and hanoi towers examples in Lua

fizzbuzz/lua/MaximumGeeker/fizzbuzz.lua [new file with mode: 0644]
towers_of_hanoi/lua/MaximumGeeker/hanoi.lua [new file with mode: 0644]
towers_of_hanoi/lua/MaximumGeeker/hanoi_stack.lua [new file with mode: 0644]

diff --git a/fizzbuzz/lua/MaximumGeeker/fizzbuzz.lua b/fizzbuzz/lua/MaximumGeeker/fizzbuzz.lua
new file mode 100644 (file)
index 0000000..a4c6a65
--- /dev/null
@@ -0,0 +1,23 @@
+--[[ FizzBuzz, Lua 5.1 ]]--
+
+local ans = {
+  'Fizz',
+  'Buzz',
+  'FizzBuzz'
+}
+
+for i = 1, 100 do
+  local c = 0
+
+  if i%3 == 0 then
+    c = c + 1
+  end
+
+  if i%5 == 0 then
+    c = c + 2
+  end
+
+  if ans[c] then
+    print( i, ans[c] )
+  end
+end
diff --git a/towers_of_hanoi/lua/MaximumGeeker/hanoi.lua b/towers_of_hanoi/lua/MaximumGeeker/hanoi.lua
new file mode 100644 (file)
index 0000000..95771f1
--- /dev/null
@@ -0,0 +1,42 @@
+--[[ Towers of hanoi - Lua 5.1 ]]--
+
+local move = function(a, b)
+  print( a, '->', b )
+end
+
+local hanoi
+hanoi = function(n, orig, pivot, dest)
+  if n < 1 then
+    return
+  end
+
+  if n == 1 then
+    move( orig, dest )
+    return
+  end
+
+  if n == 2 then
+    move( orig, pivot )
+    move( orig, dest )
+    move( pivot, dest )
+    return
+  end
+
+  hanoi(n-2, orig, pivot, dest)
+  move( orig, pivot )
+  hanoi(n-2, dest, orig, pivot)
+  move( orig, dest )
+  hanoi(n-1, pivot, orig, dest)
+end
+
+io.write "Value of N: "
+local n = io.read "*n"
+
+if type(n) == 'number' then
+  n = math.floor(n)
+else
+  io.stderr "Value given is not a number.\n"
+  os.exit(1)
+end
+
+hanoi(n, 'Left', 'Center', 'Right')
diff --git a/towers_of_hanoi/lua/MaximumGeeker/hanoi_stack.lua b/towers_of_hanoi/lua/MaximumGeeker/hanoi_stack.lua
new file mode 100644 (file)
index 0000000..faea574
--- /dev/null
@@ -0,0 +1,68 @@
+--[[ Towers of hanoi - Lua 5.1 ]]--
+
+local leftStack, centerStack, rightStack = {name = 'Left'}, {name = 'Center'}, {name = 'Right'}
+
+local visualize = function(...)
+  local z = {...}
+
+  for i = 1, 3 do
+    print(string.format("%s:\t%s", z[i].name, table.concat(z[i], ' ')))
+  end
+  print()
+end
+
+local move = function(a, b)
+  print( a.name, '->', b.name )
+  table.insert( b, table.remove(a) )
+
+  coroutine.yield(true)
+end
+
+local hanoi
+hanoi = function(n, orig, pivot, dest)
+  if n < 1 then
+    return
+  end
+
+  if n == 1 then
+    move( orig, dest )
+    return
+  end
+
+  if n == 2 then
+    move( orig, pivot )
+    move( orig, dest )
+    move( pivot, dest )
+    return
+  end
+
+  hanoi(n-2, orig, pivot, dest)
+  move( orig, pivot )
+  hanoi(n-2, dest, orig, pivot)
+  move( orig, dest )
+  hanoi(n-1, pivot, orig, dest)
+end
+
+io.write "Value of N: "
+local n = tonumber( io.read "*l", 10 )
+
+if type(n) == 'number' then
+  n = math.floor(n)
+else
+  io.stderr "Value given is not a number.\n"
+  os.exit(1)
+end
+
+for i = n, 1, -1 do
+  leftStack[ #leftStack+1 ] = i
+end
+
+local hanoiInteractive = coroutine.wrap(hanoi)
+local counter = 0
+
+repeat
+  print('#'..counter..': ')
+  counter = counter + 1
+  visualize( leftStack, centerStack, rightStack )
+  io.read()
+until hanoiInteractive(n, leftStack, centerStack, rightStack) ~= true