Coding Exercise – 1. Life, the Universe, and Everything – Learning LUA programming language


LUA programming language is lightweight. I am recently learning it and am impressed by how small the final executable is (around 250 KB).

The LUA is supposed to be a scripting language, and it is widely used in gaming engines etc. It can be easily integrated in C/C++ programming language.

The syntax is simple, a bit similar to javascript especially it has the concept of returning function inside function (closure). For example, like this:

1
2
3
4
5
6
7
8
9
10
11
function newCounter()
    local i = 0
    return function()     -- anonymous function
       i = i + 1
        return i
    end
end
 
c1 = newCounter()
print(c1())  --> 1
print(c1())  --> 2
function newCounter()
    local i = 0
    return function()     -- anonymous function
       i = i + 1
        return i
    end
end

c1 = newCounter()
print(c1())  --> 1
print(c1())  --> 2

In LUA, we use double minus to start a line comment like –this, which is similar to // in C-like programming language. To start a block comment, use this

1
2
3
--[[
  block comment
--]]
--[[
  block comment
--]]

LUA is interpreted, so you can save the program in *.lua and execute in command line like this:  lua.exe sample.lua

In LUA, there are two types of data, one is string, represented by single or double quote signs, like other programming languages, or you can use multi line string definition. String supports C-like escape, such as \t representing a tab.

1
2
3
4
5
a = 'alo\n123"'
a = "alo\n123\""
a = '\97lo\10\04923"'
a = [[alo
123"]]
a = 'alo\n123"'
a = "alo\n123\""
a = '\97lo\10\04923"'
a = [[alo
123"]]

Like Javascript keyword var, LUA supports local which defines a local variable otherwise, it is global.

To practise our first LUA program, we can choose LUA to solve problems on SPOJ Online Judge. The first problem is TEST

The problem description is [submit your solution to SPOJ now]:

test Coding Exercise - 1. Life, the Universe, and Everything - Learning LUA programming language beginner brute force code implementation LUA programming language programming languages SPOJ online judge

This is relatively simple, loop each input and check for number 42 to quit, otherwise print the number.

1
2
3
4
5
s = io.read() -- read a string
while s ~= '42' do
    print s
    s = io.read()
end
s = io.read() -- read a string
while s ~= '42' do
    print s
    s = io.read()
end

We use io.read() to input a string from console, and we can also use io.write() (the same as print) to print to console. We need to check if input is 42, which should be compared by string type. Otherwise, the condition will not satisfy forever (e.g. s ~= 42). Another method is to use tonumber() function to convert input to numbers and compare it with number 42. Similarly, there is another function tostring()) to convert numbers to string. The not equal sign is represented by ~= instead of !=.

1
2
3
4
5
s = io.read() -- read a string
while tonumber(s) ~= 42 do
    print s
    s = io.read()
end
s = io.read() -- read a string
while tonumber(s) ~= 42 do
    print s
    s = io.read()
end

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
581 words
Last Post: Compare A + B > C (64 bit) - Integer Overflow
Next Post: Split a String in LUA

The Permanent URL is: Coding Exercise – 1. Life, the Universe, and Everything – Learning LUA programming language

Leave a Reply