Coding Challenge #5
Welcome back, to another coding challenge! Let’s do this as usual.
As usual, same requirements:
- Has to be written in Lua 5.1 (the version roblox uses)
- The program has to be compatible with Roblox Studio, meaning you can’t use features vanilla Lua has but rbxlua disabled
- You can’t use httpservice to cheat the challenge
Given a 2D table, write a function Rotate(t)
which will rotate the table 90 degrees.
Let’s see what I mean. This is a 2D table, which some other people would like to call a matrix.
{{1,2,3},
{4,5,6},
{7,8,9}}
You need to rotate it 90 degrees, which in linear algebra is a matrix operation known as transposing (although transposing is actually rotating 90 degrees clockwise and not anti-clockwise) what emily said.
{{7,4,1},
{8,5,2},
{9,6,3}}
If you want to check if your code is working by printing the output, here is a function which will print the 2D table for you.
local function Print(t)
for i, v in pairs(t) do
print(unpack(v))
print("")
end
end
Goodluck! You can find a list of all the challenges here or a github version here.
Answer!
Here is my solution! I have a great feeling everybody gonna have the same setup.
local function Rotate(t)
local output = {{}, {}, {}}
for i, row in pairs(t) do
for j = 1,3 do
output[j][4-i] = row[j]
end
end
return output
end