Would it be possible to provide consistent boilerplate on Lua track - or at the least better instructions how to submit solutions.
On most other tracks once you get through setting up the language and running "Hello World" - and maybe one or two more problems - you pretty much know how to test. But on Lua track the boilerplate evolves without any explanation. I solved "Hello World", "Hamming", "House" "Difference of Squares" and "Leap" and for each one the setup for testing looks slightly different. Perhaps it is obvious once you progress in Lua a bit but it is really confusing for beginner.
Can you help me understand what you mean when you say that the setup for testing looks different? I'm not a beginner so this may be a blind spot for me, but all of the exercises you mentioned have test suites that look very similar to me.
Look at the solution side of things.
Hello World:
local hello_world = {}
function hello_world.hello()
return 'Goodbye, Mars!'
end
return hello_world
Hamming:
local Hamming = {}
function Hamming.compute(a,b)
end
return Hamming
House
local house = {}
house.verse = function(which)
end
house.recite = function()
end
return house
Leap
local leap_year = function(number)
end
return leap_year
Difference of Squares:
local function square_of_sum(n)
end
local function sum_of_squares(n)
end
local function difference_of_squares(n)
end
return {
square_of_sum = square_of_sum,
sum_of_squares = sum_of_squares,
difference_of_squares = difference_of_squares
}
And now Matrix:
return function(s)
end
So while some problems bear some resemblance you setup class and functions on class or you create one or more functions - it s not obvious at all how to handle Matrix - I suppose I need a Matrix class, constructor and some accessor functions (row and column). But the solution template does not fit it at all.
It sounds like your biggest obstacle is that Matrix is the first "object-oriented" exercise and you're assuming that you need to create much more structure than is actually necessary. For example, a solution can look like this:
return function(s)
-- some local variables
local function row(...)
end
local function column(...)
end
return {
row = row,
column = column
}
end
If we were to provide something looking a bit like that in the skeleton, would that address the issue you're running into?
It sounds like your biggest obstacle is that Matrix is the first "object-oriented" exercise and you're assuming that you need to create much more structure than is actually necessary.
Correct - Lua seems to have "there is more than one way to do it" aspect. I solved the problem on my own but not before seeing closure type of code in one of the tutorials online:
-- auxiliary to split string inputstr on separator sep
local function mysplit (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
local function Matrix(s)
local matrix = {}
local rows = mysplit(s, "\n")
for i, row_str in pairs(rows) do
--print(i .. ": " ..row_str)
matrix[i] = {}
local row = mysplit(row_str, "%s")
for j, elem in pairs(row) do
matrix[i][j] = tonumber(elem)
end
end
function matrix.row(row)
return matrix[row]
end
function matrix.column(column)
local col = {}
for i = 1, #matrix do
col[i] = matrix[i][column]
end
return col
end
return matrix
end
return Matrix
No big deal - but it would make it a bit easier for a beginner to clear the hurdle if you add couple code skeletons as examples. Or perhaps just a hint. Thanks for the reply and the pattern.
Just to elaborate - what confused me was these two lines in test file:
local Matrix = require('matrix')
.....
local matrix = Matrix('1 2 3\n4 5 6\n7 8 9')
So when I saw local matrix = Matrix('1 2 3\n4 5 6\n7 8 9') my first reaction was "OK Matrix is a class and that is a constructor". But in reality Matrix is a function imported from matrix.lua. That function is then evaluated on the string and to create object matrix. That object is endowed with some additional properties that happen to be functions - column and row.
I guess this is something which can wonderfully explained in a concept. I wanted to start writing these for some time, but unfortunately I haven't had the time yet...
I guess this is something which can wonderfully explained in a concept. I wanted to start writing these for some time, but unfortunately I haven't had the time yet...
Thanks! Once I am more advanced in Lua and I will start contributing too.