Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions exercises/practice/grade-school/.meta/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ function School:new()
end

function School:roster()
local grades = {}
for grade, _ in pairs(self.db) do
table.insert(grades, grade)
end
table.sort(grades)

local roster = {}
for _, grade in pairs(self.db) do
for _, student in ipairs(grade) do
for _, grade in ipairs(grades) do
for _, student in ipairs(self.db[grade]) do
table.insert(roster, student)
end
end
Comment on lines +9 to 20
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous solution could fail if pairs didn't iterate over self.db in the right order (which isn't guaranteed). This addresses an intermittent failure related to sensitivity in the iteration order by guaranteeing that we will always iterate over all grades in ascending order.

Expand Down
4 changes: 3 additions & 1 deletion exercises/practice/perfect-numbers/.meta/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ local function aliquot_sum(n)
end

local function classify(n)
assert(n > 0)

local sum = aliquot_sum(n)
if sum < n then
return 'deficient'
Expand All @@ -19,4 +21,4 @@ local function classify(n)
return 'perfect'
end

return { aliquot_sum = aliquot_sum, classify = classify }
return { classify = classify }
Comment on lines -22 to +24
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests no longer test aliquot_sum directly

15 changes: 15 additions & 0 deletions exercises/practice/perfect-numbers/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
return {
module_name = 'perfect_numbers',

generate_test = function(case)
if case.expected.error then
local template = [[
assert.has_error(function() perfect_numbers.classify(%s) end)]]
return template:format(case.input.number)
else
local template = [[
assert.equal('%s', perfect_numbers.classify(%s))]]
return template:format(case.expected, case.input.number)
end
end
}
42 changes: 26 additions & 16 deletions exercises/practice/perfect-numbers/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[163e8e86-7bfd-4ee2-bd68-d083dc3381a3]
description = "Smallest perfect number is classified correctly"
description = "Perfect numbers -> Smallest perfect number is classified correctly"

[169a7854-0431-4ae0-9815-c3b6d967436d]
description = "Medium perfect number is classified correctly"
description = "Perfect numbers -> Medium perfect number is classified correctly"

[ee3627c4-7b36-4245-ba7c-8727d585f402]
description = "Large perfect number is classified correctly"
description = "Perfect numbers -> Large perfect number is classified correctly"

[80ef7cf8-9ea8-49b9-8b2d-d9cb3db3ed7e]
description = "Smallest abundant number is classified correctly"
description = "Abundant numbers -> Smallest abundant number is classified correctly"

[3e300e0d-1a12-4f11-8c48-d1027165ab60]
description = "Medium abundant number is classified correctly"
description = "Abundant numbers -> Medium abundant number is classified correctly"

[ec7792e6-8786-449c-b005-ce6dd89a772b]
description = "Large abundant number is classified correctly"
description = "Abundant numbers -> Large abundant number is classified correctly"

[05f15b93-849c-45e9-9c7d-1ea131ef7d10]
description = "Abundant numbers -> Perfect square abundant number is classified correctly"

[e610fdc7-2b6e-43c3-a51c-b70fb37413ba]
description = "Smallest prime deficient number is classified correctly"
description = "Deficient numbers -> Smallest prime deficient number is classified correctly"

[0beb7f66-753a-443f-8075-ad7fbd9018f3]
description = "Smallest non-prime deficient number is classified correctly"
description = "Deficient numbers -> Smallest non-prime deficient number is classified correctly"

[1c802e45-b4c6-4962-93d7-1cad245821ef]
description = "Medium deficient number is classified correctly"
description = "Deficient numbers -> Medium deficient number is classified correctly"

[47dd569f-9e5a-4a11-9a47-a4e91c8c28aa]
description = "Large deficient number is classified correctly"
description = "Deficient numbers -> Large deficient number is classified correctly"

[a696dec8-6147-4d68-afad-d38de5476a56]
description = "Edge case (no factors other than itself) is classified correctly"
description = "Deficient numbers -> Edge case (no factors other than itself) is classified correctly"

[72445cee-660c-4d75-8506-6c40089dc302]
description = "Zero is rejected (not a natural number)"
description = "Invalid inputs -> Zero is rejected (as it is not a positive integer)"

[2d72ce2c-6802-49ac-8ece-c790ba3dae13]
description = "Negative integer is rejected (not a natural number)"
description = "Invalid inputs -> Negative integer is rejected (as it is not a positive integer)"
74 changes: 57 additions & 17 deletions exercises/practice/perfect-numbers/perfect-numbers_spec.lua
Original file line number Diff line number Diff line change
@@ -1,31 +1,71 @@
local perfect_numbers = require('perfect-numbers')

describe('perfect-numbers', function()
it('should be able to calculate the Aliquot sum of a number with no divisors', function()
assert.equal(0, perfect_numbers.aliquot_sum(1))
end)
describe('perfect numbers', function()
it('smallest perfect number is classified correctly', function()
assert.equal('perfect', perfect_numbers.classify(6))
end)

it('should be able to calculate the Aliquot sum of a number with a single divisor', function()
assert.equal(1, perfect_numbers.aliquot_sum(2))
end)
it('medium perfect number is classified correctly', function()
assert.equal('perfect', perfect_numbers.classify(28))
end)

it('should be able to calculate the Aliquot sum of a number with a multiple divisors', function()
assert.equal(15, perfect_numbers.aliquot_sum(16))
it('large perfect number is classified correctly', function()
assert.equal('perfect', perfect_numbers.classify(33550336))
end)
end)

it('should be able to calculate the Aliquot sum of a large number', function()
assert.equal(229, perfect_numbers.aliquot_sum(1115))
end)
describe('abundant numbers', function()
it('smallest abundant number is classified correctly', function()
assert.equal('abundant', perfect_numbers.classify(12))
end)

it('medium abundant number is classified correctly', function()
assert.equal('abundant', perfect_numbers.classify(30))
end)

it('large abundant number is classified correctly', function()
assert.equal('abundant', perfect_numbers.classify(33550335))
end)

it('should classify numbers whose Aliquot sum is less than itself as deficient', function()
assert.equal('deficient', perfect_numbers.classify(13))
it('perfect square abundant number is classified correctly', function()
assert.equal('abundant', perfect_numbers.classify(196))
end)
end)

it('should classify numbers whose Aliquot sum is equal to itself as perfect', function()
assert.equal('perfect', perfect_numbers.classify(28))
describe('deficient numbers', function()
it('smallest prime deficient number is classified correctly', function()
assert.equal('deficient', perfect_numbers.classify(2))
end)

it('smallest non-prime deficient number is classified correctly', function()
assert.equal('deficient', perfect_numbers.classify(4))
end)

it('medium deficient number is classified correctly', function()
assert.equal('deficient', perfect_numbers.classify(32))
end)

it('large deficient number is classified correctly', function()
assert.equal('deficient', perfect_numbers.classify(33550337))
end)

it('edge case (no factors other than itself) is classified correctly', function()
assert.equal('deficient', perfect_numbers.classify(1))
end)
end)

it('should classify numbers whose Aliquot sum is greater than itself as abundant', function()
assert.equal('abundant', perfect_numbers.classify(12))
describe('invalid inputs', function()
it('zero is rejected (as it is not a positive integer)', function()
assert.has_error(function()
perfect_numbers.classify(0)
end)
end)

it('negative integer is rejected (as it is not a positive integer)', function()
assert.has_error(function()
perfect_numbers.classify(-1)
end)
end)
end)
end)
Loading