Compare commits
4 Commits
45c27ba067
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| ed15159576 | |||
| 0ec0d19e65 | |||
| fa33a7bdf7 | |||
| 2317033e75 |
50
17_prime_factors.lua
Normal file
50
17_prime_factors.lua
Normal file
@@ -0,0 +1,50 @@
|
||||
-- Write a function which determines the prime factors of a given
|
||||
-- number, returning them in a list. You may assume that none of the
|
||||
-- prime factors are above 10000
|
||||
--
|
||||
-- Solution --------------------------------------------------------------------
|
||||
function prime_factors(n)
|
||||
-- Your implementation here
|
||||
end
|
||||
|
||||
-- Tests -----------------------------------------------------------------------
|
||||
|
||||
local luaunit = require("luaunit.luaunit")
|
||||
|
||||
function test_prime_factors_of_42()
|
||||
luaunit.assertItemsEquals(prime_factors(42), {2, 3, 7})
|
||||
end
|
||||
|
||||
function test_prime_factors_of_70()
|
||||
luaunit.assertItemsEquals(prime_factors(70), {2, 5, 7})
|
||||
end
|
||||
|
||||
function test_prime_factors_of_253()
|
||||
luaunit.assertItemsEquals(prime_factors(253), {11, 23})
|
||||
end
|
||||
|
||||
function test_prime_factors_of_245()
|
||||
luaunit.assertItemsEquals(prime_factors(245), {5, 7, 7})
|
||||
end
|
||||
|
||||
function test_prime_factors_of_36()
|
||||
luaunit.assertItemsEquals(prime_factors(36), {2, 2, 3, 3})
|
||||
end
|
||||
|
||||
function test_prime_factors_of_1337()
|
||||
luaunit.assertItemsEquals(prime_factors(1337), {7, 191})
|
||||
end
|
||||
|
||||
function test_prime_factors_of_4321()
|
||||
luaunit.assertItemsEquals(prime_factors(4321), {29, 149})
|
||||
end
|
||||
|
||||
function test_prime_factors_of_57812834()
|
||||
luaunit.assertItemsEquals(prime_factors(57812834), {2, 29, 113, 8821})
|
||||
end
|
||||
|
||||
function test_prime_factors_of_8894220()
|
||||
luaunit.assertItemsEquals(prime_factors(8894220), {2, 2, 3, 5, 271, 547})
|
||||
end
|
||||
|
||||
os.exit(luaunit.LuaUnit.run())
|
||||
105
19_dependencies.lua
Normal file
105
19_dependencies.lua
Normal file
@@ -0,0 +1,105 @@
|
||||
-- Write a function to generate a valid order for a number of jobs
|
||||
-- that depend upon each other. Your input is a jobs table; each key
|
||||
-- is a job number, with its the values being a list jobs it depends
|
||||
-- on. You may assume that there are no circular dependencies.
|
||||
--
|
||||
-- Return a list of job numbers, so that performing the jobs in that
|
||||
-- order ensures that all jobs are exactly once, and all dependencies
|
||||
-- of a job are done before prior to it.
|
||||
--
|
||||
-- Solution --------------------------------------------------------------------
|
||||
function schedule(jobs)
|
||||
-- Your implementation here
|
||||
end
|
||||
|
||||
-- Tests -----------------------------------------------------------------------
|
||||
|
||||
local luaunit = require("luaunit.luaunit")
|
||||
|
||||
local function keys(t)
|
||||
local results = {}
|
||||
for key, _ in pairs(t) do table.insert(results, key) end
|
||||
return results
|
||||
end
|
||||
|
||||
local function contains(list, target)
|
||||
for _, item in ipairs(list) do if item == target then return true end end
|
||||
return false
|
||||
end
|
||||
|
||||
local function deep_copy(original)
|
||||
local copy = {}
|
||||
if type(original) == "table" then
|
||||
for key, value in pairs(original) do
|
||||
local key_copy = deep_copy(key)
|
||||
local value_copy = deep_copy(value)
|
||||
copy[key_copy] = value_copy
|
||||
end
|
||||
else
|
||||
copy = original
|
||||
end
|
||||
return copy
|
||||
end
|
||||
|
||||
local function assert_valid(order, jobs)
|
||||
local done = {}
|
||||
for _, job in ipairs(order) do
|
||||
for _, dependency in ipairs(jobs[job]) do
|
||||
luaunit.assertTrue(contains(done, dependency))
|
||||
end
|
||||
table.insert(done, job)
|
||||
end
|
||||
end
|
||||
|
||||
local function test_case(jobs)
|
||||
local order = schedule(deep_copy(jobs))
|
||||
luaunit.assertItemsEquals(order, keys(jobs))
|
||||
assert_valid(order, jobs)
|
||||
end
|
||||
|
||||
function test_sequential_dependencies()
|
||||
local jobs = {[1] = {2}, [2] = {3}, [3] = {}}
|
||||
test_case(jobs)
|
||||
end
|
||||
|
||||
function test_converging_dependencies()
|
||||
local jobs = {
|
||||
[1] = {5},
|
||||
[2] = {5},
|
||||
[3] = {6},
|
||||
[4] = {6},
|
||||
[5] = {7},
|
||||
[6] = {7},
|
||||
[7] = {},
|
||||
}
|
||||
test_case(jobs)
|
||||
end
|
||||
|
||||
function test_diverging_dependencies()
|
||||
local jobs = {
|
||||
[1] = {2, 3},
|
||||
[2] = {4, 5},
|
||||
[3] = {6, 7},
|
||||
[4] = {},
|
||||
[5] = {},
|
||||
[6] = {},
|
||||
[7] = {}
|
||||
}
|
||||
test_case(jobs)
|
||||
end
|
||||
|
||||
function test_compound_dependencies()
|
||||
local jobs = {
|
||||
[1] = {2, 3, 4},
|
||||
[2] = {5, 6},
|
||||
[3] = {7},
|
||||
[4] = {7},
|
||||
[5] = {},
|
||||
[6] = {8},
|
||||
[7] = {8},
|
||||
[8] = {}
|
||||
}
|
||||
test_case(jobs)
|
||||
end
|
||||
|
||||
os.exit(luaunit.LuaUnit.run())
|
||||
36
README.md
36
README.md
@@ -1,12 +1,36 @@
|
||||
# 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. It's recommended that you roughly stick to
|
||||
the defined order of the exercises, but feel free to deviate if you
|
||||
wish.
|
||||
solutions. Each file contains a description of the problem and an
|
||||
area for a solution as well as the tests.
|
||||
|
||||
## Set Up
|
||||
|
||||
To get started, create a fork of this repo on your own account (with
|
||||
the "Fork" button in the top-left of the Gitea page"), then clone it,
|
||||
passing the `--recursive` flag. If you already cloned it without
|
||||
passing `--recursive`, you can run `git submodule update --init` to
|
||||
fix it.
|
||||
|
||||
```sh
|
||||
git clone --recursive git@git.wip.sh:<your-username>/lua-exercises.git
|
||||
```
|
||||
|
||||
## Doing the Exercises
|
||||
|
||||
It's recommended that you roughly stick to the defined order of the
|
||||
exercises, but feel free to deviate if you wish. Some of the
|
||||
exercises are relatively easy, whereas some of them (especially the
|
||||
last few) are quite challenging; do not be disheartened if solving a
|
||||
particular exercise takes you a long time, and feel free to ask for
|
||||
help in the Discord server if you feel you are stuck.
|
||||
|
||||
You can define as many functions and constants as you need in the
|
||||
solution area. You might find it useful to temporarily comment out
|
||||
some of the tests to work through the cases one by one (especially on
|
||||
the more complex exercises), but do not modify the test code other
|
||||
than this, and make sure all the cases are un-commented again before
|
||||
considering the exercise solved.
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user