-
-
Notifications
You must be signed in to change notification settings - Fork 60
Sync perfect-numbers #628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Sync perfect-numbers #628
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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' | ||
|
|
@@ -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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests no longer test |
||
15 changes: 15 additions & 0 deletions
15
exercises/practice/perfect-numbers/.meta/spec_generator.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
74
exercises/practice/perfect-numbers/perfect-numbers_spec.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
pairsdidn't iterate overself.dbin 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.