Commit 97f39d80 authored by Niccolo Luis J. Jorge's avatar Niccolo Luis J. Jorge

Pasted working code and added readme

parent fda88de5
Pipeline #2562 failed with stages
# Bonus Points
Lua mergesort
##Instructions for interpreting:
Copy and paste the code in `mergesort.lua` into an online Lua interpreter. The following is confirmed to be functional:
https://www.tutorialspoint.com/execute_lua_online.php
You can also use the Minecraft mod ComputerCraft (or the modern version CC:Restitched); it is assumed you know how to run a modded installation using the Curseforge launcher or the MultiMC launcher.
- Create a new world and obtain any kind of computer (this is easiest in creative mode)
- Type `id` into the prompt and note the number.
- Go into the world save folder and navigate to `computercraft/computers`, and paste `mergesort.lua` into the folder named as the computer's ID.
- On the in-game computer, type `mergesort`. From here, it's just as though you ran it on an online interpreter.
##Instructions for use
Simply type a list of numbers separated by spaces, and press enter when you're done. The next line is a sorted list of numbers separated by spaces.
**WARNING**: Inputting anything other than numbers will result in strange output. I do not have time to account for this.
--comment
function merge(arr1, arr2)
p1 = 1; p2 = 1;
g1 = (type(arr1) == "table") and #arr1 or 1
g2 = (type(arr2) == "table") and #arr2 or 1
ret = {}
n = 1
if not (type(arr1) == "table") then arr1 = {arr1, 1000000000} end
if not (type(arr2) == "table") then arr2 = {arr2, 1000000000} end
while(p1 <= g1) and (p2 <= g2) do
--print(arr1[p1]); print(arr2[p2]);
if (arr1[p1] > arr2[p2]) then
ret[n] = arr2[p2]
p2 = p2 + 1
else
ret[n] = arr1[p1]
p1 = p1 + 1
end
n = n + 1
end
if p1 > g1 then
while p2 <= g2 do
ret[n] = arr2[p2]
p2 = p2 + 1
n = n + 1
end
end
if p2 > g2 then
while p1 <= g1 do
ret[n] = arr1[p1]
p1 = p1 + 1
n = n + 1
end
end
--showArr(ret)
return ret
end
function mergeSort(arr)
ret = {}
len = (type(arr) == "table") and #arr or 1
local a = {}; local b = {};
if len == 1 then
--print("Len = 1")
--print(arr)
ret = arr
elseif len == 2 then
--print("Len = 2")
--showArr(arr)
a = arr[1]
b = arr[2]
if a > b then
a = arr[2]
b = arr[1]
end
ret[1] = a;
ret[2] = b;
--showArr(ret)
else
--print("Len > 2")
--showArr(arr)
n = 1
if (len % 2 == 0) then
--print("building a")
for i=1,(len/2) do
a[i] = arr[n]
--print(n.." is "..arr[n])
n = n + 1
end
--print("building b")
for i=1,(len/2) do
b[i] = arr[n]
--print(n.." is "..arr[n])
n = n + 1
end
else
for i=1,((len-1)/2)+1 do
a[i] = arr[n]
n = n + 1
end
for i=1,((len-1)/2) do
b[i] = arr[n]
n = n + 1
end
end
--print("a")
--showArr(a)
--print("b")
--showArr(b)
a = mergeSort(a)
b = mergeSort(b)
--print("Merge")
ret = merge(a, b)
end
return ret
end
function showArr(arr)
out = ""
--print(type(arr) ~= "table")
--if (type(arr) ~= "table") then print(arr); return; end
len = #arr
for i=1,len do
out = out..arr[i].." "
--print(out)
end
print(out)
end
function split(uin)
ret = {}
n = 1
for i in string.gmatch(uin, "%S+") do
ret[n] = tonumber(i)
n = n + 1
end
return ret
end
print("Input your list")
arr = split(io.read())
--showArr(arr)
arr = mergeSort(arr)
showArr(arr)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment