Skip to content

Commit

Permalink
Example solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanplusplus committed Dec 3, 2024
1 parent b4cd2a6 commit 6a93c0a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 33 deletions.
47 changes: 47 additions & 0 deletions exercises/practice/complex-numbers/.meta/example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
local Complex

local function close_to(a, b)
return math.abs(a - b) < 1e-15
end

local function eq(c1, c2)
return close_to(c1.r, c2.r) and close_to(c1.i, c2.i)
end

local function add(c1, c2)
return Complex(c1.r + c2.r, c1.i + c2.i)
end

local function sub(c1, c2)
return Complex(c1.r - c2.r, c1.i - c2.i)
end

local function mul(c1, c2)
return Complex(c1.r * c2.r - c1.i * c2.i, c1.r * c2.i + c1.i * c2.r)
end

local function div(c1, c2)
local d = c2.r * c2.r + c2.i * c2.i
return Complex((c1.r * c2.r + c1.i * c2.i) / d, (c1.i * c2.r - c1.r * c2.i) / d)
end

Complex = function(r, i)
local c = { r = r, i = i or 0 }

function c.abs()
return math.sqrt(c.r * c.r + c.i * c.i)
end

function c.conj()
return Complex(c.r, -c.i)
end

function c.exp()
local e = math.exp(c.r)
return Complex(e * math.cos(c.i), e * math.sin(c.i))
end

return setmetatable(c, { __eq = eq, __add = add, __sub = sub, __mul = mul, __div = div })
end

return Complex
34 changes: 15 additions & 19 deletions exercises/practice/complex-numbers/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
local binary_operator = {
sub = '-',
add = '+',
mul = '*',
div = '/'
}
local unary_operator = { real = 'r', imaginary = 'i', abs = 'abs()', conjugate = 'conj()', exp = 'exp()' }

local binary_operator = { sub = '-', add = '+', mul = '*', div = '/' }

local function value(input)
local function complex(input)
if type(input) ~= 'table' then
input = { input }
end
return 'Complex(' .. table.concat(input, ', ') .. ')'
end

local function scalar(input)
return tostring(input)
end

local unary_result_type = { real = scalar, imaginary = scalar, abs = scalar, conjugate = complex, exp = complex }

return {
module_name = 'Complex',

Expand All @@ -24,23 +27,16 @@ return {
generate_test = function(case)
if case.input.z then
local template = [[
assert.equal(%s, %s.%s())]]
assert.equal(%s, %s.%s)]]

return template:format(
value(case.expected),
value(case.input.z),
case.property
)
return template:format(unary_result_type[case.property](case.expected), complex(case.input.z),
unary_operator[case.property])
else
local template = [[
assert.equal(%s, %s %s %s)]]

return template:format(
value(case.expected),
value(case.input.z1),
binary_operator[case.property],
value(case.input.z2)
)
return template:format(complex(case.expected), complex(case.input.z1), binary_operator[case.property],
complex(case.input.z2))
end
end
}
13 changes: 13 additions & 0 deletions exercises/practice/complex-numbers/complex-numbers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local Complex

Complex = function(r, i)
local c = {
--
}

return setmetatable(c, {
--
})
end

return Complex
28 changes: 14 additions & 14 deletions exercises/practice/complex-numbers/complex-numbers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ describe('complex-numbers', function()

describe('real part', function()
it('real part of a purely real number', function()
assert.equal(Complex(1), Complex(1, 0).real())
assert.equal(1, Complex(1, 0).r)
end)

it('real part of a purely imaginary number', function()
assert.equal(Complex(0), Complex(0, 1).real())
assert.equal(0, Complex(0, 1).r)
end)

it('real part of a number with real and imaginary part', function()
assert.equal(Complex(1), Complex(1, 2).real())
assert.equal(1, Complex(1, 2).r)
end)
end)

describe('imaginary part', function()
it('imaginary part of a purely real number', function()
assert.equal(Complex(0), Complex(1, 0).imaginary())
assert.equal(0, Complex(1, 0).i)
end)

it('imaginary part of a purely imaginary number', function()
assert.equal(Complex(1), Complex(0, 1).imaginary())
assert.equal(1, Complex(0, 1).i)
end)

it('imaginary part of a number with real and imaginary part', function()
assert.equal(Complex(2), Complex(1, 2).imaginary())
assert.equal(2, Complex(1, 2).i)
end)
end)

Expand Down Expand Up @@ -97,37 +97,37 @@ describe('complex-numbers', function()

describe('absolute value', function()
it('absolute value of a positive purely real number', function()
assert.equal(Complex(5), Complex(5, 0).abs())
assert.equal(5, Complex(5, 0).abs())
end)

it('absolute value of a negative purely real number', function()
assert.equal(Complex(5), Complex(-5, 0).abs())
assert.equal(5, Complex(-5, 0).abs())
end)

it('absolute value of a purely imaginary number with positive imaginary part', function()
assert.equal(Complex(5), Complex(0, 5).abs())
assert.equal(5, Complex(0, 5).abs())
end)

it('absolute value of a purely imaginary number with negative imaginary part', function()
assert.equal(Complex(5), Complex(0, -5).abs())
assert.equal(5, Complex(0, -5).abs())
end)

it('absolute value of a number with real and imaginary part', function()
assert.equal(Complex(5), Complex(3, 4).abs())
assert.equal(5, Complex(3, 4).abs())
end)
end)

describe('complex conjugate', function()
it('conjugate a purely real number', function()
assert.equal(Complex(5, 0), Complex(5, 0).conjugate())
assert.equal(Complex(5, 0), Complex(5, 0).conj())
end)

it('conjugate a purely imaginary number', function()
assert.equal(Complex(0, -5), Complex(0, 5).conjugate())
assert.equal(Complex(0, -5), Complex(0, 5).conj())
end)

it('conjugate a number with real and imaginary part', function()
assert.equal(Complex(1, -1), Complex(1, 1).conjugate())
assert.equal(Complex(1, -1), Complex(1, 1).conj())
end)
end)

Expand Down

0 comments on commit 6a93c0a

Please sign in to comment.