Compare commits

...

3 Commits

14 changed files with 62 additions and 19 deletions

View File

@ -1,3 +1,5 @@
-- Description
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function f() function f()
-- Your implementation here -- Your implementation here

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# Lua Exercises
This repo contains some Lua exercises along with automated tests for
their solutions. Each file contains a description of the problem and
an area for your solution as well as the tests. Feel free to define
as many functions and constants as you need in the solution area, but
do not modify the tests.
To run the tests and check your solution for a given exercise, simply
run `lua <the-file>`. For example, if you were working on the
anagrams exercise, you would run `lua xxx_anagrams.lua`. For each
exercise, the tests should run in well under a tenth of a second; if
it takes longer than this you should try to speed up your solution.

View File

@ -1,3 +1,7 @@
-- Implement the is_anagram() function below, so that it returns true if
-- the two passed strings are anagrams of each other. Any non-
-- alphabetical characters in the string should be ignored.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function is_anagram(str1, str2) function is_anagram(str1, str2)
-- Your implementation here -- Your implementation here

View File

@ -1,3 +1,7 @@
-- Implement is_balanced(), which should return true or false depending
-- on whether the brackets in the input string are balanced,
-- i.e. there is a closing bracket for each opening one.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function is_balanced(input) function is_balanced(input)
-- Your implementation here -- Your implementation here

View File

@ -1,3 +1,5 @@
-- Write a function to calculate the nth fibonacci number.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function nth_fibonacci_number(n) function nth_fibonacci_number(n)
-- Your implementation here -- Your implementation here

View File

@ -1,3 +1,7 @@
-- The greatest common divisor (GCD) of two numbers X and Y is, as the
-- name suggests, the largest number which evenly divides both X and
-- Y. Implement the function below to calculate this.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function greatest_common_divisor(x, y) function greatest_common_divisor(x, y)
-- Your implementation here -- Your implementation here

View File

@ -1,3 +1,8 @@
-- The hamming distance is the number of positions in which two
-- strings differ, and is used to measure how similar strings are.
-- Implement the hamming_distance() function below to calculate this;
-- you may assume that the strings are of equal length.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function hamming_distance(str1, str2) function hamming_distance(str1, str2)
-- Your implementation here -- Your implementation here

View File

@ -1,3 +1,6 @@
-- Write a function to return the hexadecimal representation (in a
-- string) of a number. Use lowercase a to f.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function hexadecimal(x) function hexadecimal(x)
-- Your implementation here -- Your implementation here

View File

@ -1,3 +1,6 @@
-- Implement is_leap_year() below, returning whether the given year is
-- a leap year according to the Gregorian calendar.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function is_leap_year(year) function is_leap_year(year)
-- Your implementation here -- Your implementation here

View File

@ -1,3 +1,5 @@
-- Implement a function to return the largest value in a list.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function maximum(list) function maximum(list)
-- Your implementation here -- Your implementation here

View File

@ -1,3 +1,7 @@
-- Implement is_palindrome(), which should return whether the passed
-- string is palindromic (i.e the same forwards as it is backwards).
-- Ignore non-letters and case.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function is_palindrome(str) function is_palindrome(str)
-- Your implementation here -- Your implementation here

View File

@ -1,3 +1,5 @@
-- Write a function to determine whether a given number is prime.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function is_prime(x) function is_prime(x)
-- Your implementation here -- Your implementation here

View File

@ -1,3 +1,8 @@
-- Implement the function is_queen_threat() below. It should return
-- true if the queen is threatening the pawn and false otherwise. The
-- coordinates are as given as file (A-H), rank (1-8) pairs such as B3
-- and D7.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
function is_queen_threat(queen_position, pawn_position) function is_queen_threat(queen_position, pawn_position)
-- Your implementation here -- Your implementation here

View File

@ -1,18 +1,8 @@
-- Write a function to determine the resistance (in ohms) of a given
-- three-part resistor colour code. The colour code is passed as a
-- list of strings.
--
-- Solution -------------------------------------------------------------------- -- Solution --------------------------------------------------------------------
-- Give the colours whatever values are convenient
black = undefined
brown = undefined
red = undefined
orange = undefined
yellow = undefined
green = undefined
blue = undefined
violet = undefined
grey = undefined
white = undefined
gold = undefined
silver = undefined
function decode_resistance(colours) function decode_resistance(colours)
-- Your implementation here -- Your implementation here
end end
@ -22,27 +12,27 @@ end
local luaunit = require("luaunit.luaunit") local luaunit = require("luaunit.luaunit")
function test_violet_orange_black_is_73_ohms() function test_violet_orange_black_is_73_ohms()
local resistance = decode_resistance({violet, orange, black}) local resistance = decode_resistance({"violet", "orange", "black"})
luaunit.assertEquals(resistance, 73) luaunit.assertEquals(resistance, 73)
end end
function test_brown_green_black_is_15_ohms() function test_brown_green_black_is_15_ohms()
local resistance = decode_resistance({brown, green, black}) local resistance = decode_resistance({"brown", "green", "black"})
luaunit.assertEquals(resistance, 15) luaunit.assertEquals(resistance, 15)
end end
function test_green_blue_orange_is_56_kiloohms() function test_green_blue_orange_is_56_kiloohms()
local resistance = decode_resistance({green, blue, orange}) local resistance = decode_resistance({"green", "blue", "orange"})
luaunit.assertEquals(resistance, 56000) luaunit.assertEquals(resistance, 56000)
end end
function test_white_yellow_yellow_is_940_kiloohms() function test_white_yellow_yellow_is_940_kiloohms()
local resistance = decode_resistance({white, yellow, yellow}) local resistance = decode_resistance({"white", "yellow", "yellow"})
luaunit.assertEquals(resistance, 940000) luaunit.assertEquals(resistance, 940000)
end end
function test_orange_red_silver_is_320_milliohms() function test_orange_red_silver_is_320_milliohms()
local resistance = decode_resistance({orange, red, silver}) local resistance = decode_resistance({"orange", "red", "silver"})
luaunit.assertEquals(resistance, 0.32) luaunit.assertEquals(resistance, 0.32)
end end