Lua Cheatsheet
53 essential methods for coding interviews
Showing 53 of 53 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
table.insert | table.insert(t, [pos,] value) | Inserts value at position pos (default: end of table). | O(n) when inserting at position, O(1) at end | essential |
table.remove | table.remove(t [, pos]) | Removes and returns element at position pos (default: last element). | O(n) when removing from middle, O(1) from end | essential |
table.concat | table.concat(t [, sep [, i [, j]]]) | Concatenates table elements into a string with optional separator. | O(n) | essential |
table.sort | table.sort(t [, comp]) | Sorts table in place. Optional comparison function returns true if first arg should come first. | O(n log n) | essential |
#table (length) | #t | Returns the length of a sequence (table with consecutive integer keys starting at 1). | O(1) for arrays, O(log n) for sparse tables | essential |
ipairs | for i, v in ipairs(t) do ... end | Iterates over array part of table (consecutive integer keys from 1). | O(n) | essential |
pairs | for k, v in pairs(t) do ... end | Iterates over all key-value pairs in a table. | O(n) | essential |
string.sub | string.sub(s, i [, j]) | Returns substring from index i to j (default: end). Negative indices count from end. | O(j - i) | essential |
string.len / #string | string.len(s) or #s | Returns the length of a string in bytes. | O(1) | essential |
string.find | string.find(s, pattern [, init [, plain]]) | Finds first match of pattern. Returns start and end indices, plus captures. | O(n * m) | essential |
string.match | string.match(s, pattern [, init]) | Returns captures from pattern match, or whole match if no captures. | O(n * m) | essential |
string.gmatch | string.gmatch(s, pattern) | Returns iterator over all pattern matches in string. | O(n * m) total | essential |
string.gsub | string.gsub(s, pattern, repl [, n]) | Returns string with pattern matches replaced. repl can be string, table, or function. | O(n * m) | essential |
math.max / math.min | math.max(...) / math.min(...) | Returns maximum/minimum of arguments. | O(n) | essential |
math.floor / math.ceil | math.floor(x) / math.ceil(x) | Rounds down/up to nearest integer. | O(1) | essential |
math.abs | math.abs(x) | Returns absolute value of x. | O(1) | essential |
math.huge | math.huge | Represents positive infinity. | O(1) | essential |
tonumber | tonumber(e [, base]) | Converts value to number. Returns nil if conversion fails. | O(n) | essential |
tostring | tostring(v) | Converts value to string representation. | O(n) | essential |
type | type(v) | Returns type of value as string. | O(1) | essential |
print | print(...) | Prints values to stdout separated by tabs, with newline. | O(n) | essential |
Pattern: Deep Copy | function deepcopy(t) ... end | Recursively copies table and all nested tables. | O(n) total elements | essential |
Pattern: Table Contains | function contains(t, value) ... end | Checks if array-like table contains a value. | O(n) | essential |
Pattern: String Split | function split(s, sep) ... end | Splits string by separator into array of substrings. | O(n) | essential |
table.unpack | table.unpack(t [, i [, j]]) | Returns elements from table as multiple values. | O(n) | common |
next | next(t [, index]) | Returns next key-value pair after index, or first pair if index is nil. | O(1) | common |
string.format | string.format(formatstring, ...) | Returns formatted string (similar to C printf). | O(n) | common |
string.byte | string.byte(s [, i [, j]]) | Returns numeric byte values of characters s[i] through s[j]. | O(j - i) | common |
string.char | string.char(...) | Returns string from numeric byte values. | O(n) | common |
string.rep | string.rep(s, n [, sep]) | Returns string s repeated n times, with optional separator. | O(n * len(s)) | common |
string.reverse | string.reverse(s) | Returns reversed string. | O(n) | common |
string.upper / string.lower | string.upper(s) / string.lower(s) | Returns uppercase/lowercase version of string. | O(n) | common |
math.sqrt | math.sqrt(x) | Returns square root of x. | O(1) | common |
math.random | math.random([m [, n]]) | Returns random number. No args: [0,1). One arg: [1,m]. Two args: [m,n]. | O(1) | common |
math.randomseed | math.randomseed(x) | Sets seed for random number generator. | O(1) | common |
math.pow / ^ operator | math.pow(x, y) or x ^ y | Returns x raised to power y. | O(1) | common |
setmetatable / getmetatable | setmetatable(t, mt) / getmetatable(t) | Sets/gets metatable for table. Metatables control table behavior. | O(1) | common |
select | select(index, ...) or select("#", ...) | Returns arguments after index, or count of arguments with "#". | O(n) | common |
pcall | pcall(f, arg1, ...) | Calls function in protected mode. Returns success boolean and results/error. | O(1) + function time | common |
assert | assert(v [, message]) | Raises error if v is false or nil, otherwise returns v. | O(1) | common |
error | error(message [, level]) | Raises an error with given message. Level affects error position in traceback. | O(1) | common |
io.read | io.read([format1, ...]) | Reads from stdin. "*l" for line, "*n" for number, "*a" for all. | O(n) | common |
Pattern: Table Keys | function keys(t) ... end | Returns array of all keys in a table. | O(n) | common |
Pattern: Table Values | function values(t) ... end | Returns array of all values in a table. | O(n) | common |
table.move | table.move(a1, f, e, t [, a2]) | Moves elements from a1[f..e] to a2[t..], returns destination table. | O(n) | useful |
math.fmod | math.fmod(x, y) | Returns remainder of x/y (same sign as x). | O(1) | useful |
math.modf | math.modf(x) | Returns integral and fractional parts of x. | O(1) | useful |
math.log | math.log(x [, base]) | Returns logarithm of x. Default base is e (natural log). | O(1) | useful |
rawget / rawset | rawget(t, k) / rawset(t, k, v) | Gets/sets table value bypassing metamethods. | O(1) | useful |
collectgarbage | collectgarbage([opt [, arg]]) | Controls garbage collector. "collect" runs full GC cycle. | O(n) heap size | useful |
load / loadstring | load(chunk [, chunkname [, mode [, env]]]) | Compiles string/function as Lua chunk, returns function or nil+error. | O(n) chunk size | useful |
os.time / os.date | os.time([table]) / os.date([format [, time]]) | os.time returns Unix timestamp. os.date formats time. | O(1) | useful |
os.clock | os.clock() | Returns CPU time used by program in seconds. | O(1) | useful |