Skip to content

Commit

Permalink
Add palindrome-products practice exercise (#521)
Browse files Browse the repository at this point in the history
* Add palindrome products practice exercise

* Suggestion from keiravillekode
  • Loading branch information
ryanplusplus authored Sep 29, 2024
1 parent 8822a00 commit 7fc95d1
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,14 @@
"text_parsing"
]
},
{
"slug": "palindrome-products",
"name": "Palindrome Products",
"uuid": "928d3505-7780-4d2d-8fd8-7c8f0aac4033",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "anagram",
"name": "Anagram",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/palindrome-products/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
36 changes: 36 additions & 0 deletions exercises/practice/palindrome-products/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Instructions

Detect palindrome products in a given range.

A palindromic number is a number that remains the same when its digits are reversed.
For example, `121` is a palindromic number but `112` is not.

Given a range of numbers, find the largest and smallest palindromes which
are products of two numbers within that range.

Your solution should return the largest and smallest palindromes, along with the factors of each within the range.
If the largest or smallest palindrome has more than one pair of factors within the range, then return all the pairs.

## Example 1

Given the range `[1, 9]` (both inclusive)...

And given the list of all possible products within this range:
`[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 21, 24, 27, 20, 28, 32, 36, 25, 30, 35, 40, 45, 42, 48, 54, 49, 56, 63, 64, 72, 81]`

The palindrome products are all single digit numbers (in this case):
`[1, 2, 3, 4, 5, 6, 7, 8, 9]`

The smallest palindrome product is `1`.
Its factors are `(1, 1)`.
The largest palindrome product is `9`.
Its factors are `(1, 9)` and `(3, 3)`.

## Example 2

Given the range `[10, 99]` (both inclusive)...

The smallest palindrome product is `121`.
Its factors are `(11, 11)`.
The largest palindrome product is `9009`.
Its factors are `(91, 99)`.
19 changes: 19 additions & 0 deletions exercises/practice/palindrome-products/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"ryanplusplus"
],
"files": {
"solution": [
"palindrome-products.lua"
],
"test": [
"palindrome-products_spec.lua"
],
"example": [
".meta/example.lua"
]
},
"blurb": "Detect palindrome products in a given range.",
"source": "Problem 4 at Project Euler",
"source_url": "https://projecteuler.net/problem=4"
}
72 changes: 72 additions & 0 deletions exercises/practice/palindrome-products/.meta/example.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
local function reversed(s)
local reversed = ''
for c in s:gmatch('.') do
reversed = c .. reversed
end
return reversed
end

local function is_palindrome(n)
local s = tostring(n)
return s == reversed(s)
end

local function validate_inputs(min, max)
assert(min <= max, 'min must be <= max')
end

local function smallest(min, max)
validate_inputs(min, max)

local value = math.huge
local factors = {}

for i = min, max do
for j = i, max do
local n = i * j

if n > value then
break
end

if is_palindrome(n) then
if n ~= value then
value = n
factors = {}
end
table.insert(factors, { i, j })
end
end
end

return { value = #factors > 0 and value or nil, factors = factors }
end

local function largest(min, max)
validate_inputs(min, max)

local value = -math.huge
local factors = {}

for i = max, min, -1 do
for j = i, min, -1 do
local n = i * j

if n < value then
break
end

if is_palindrome(n) then
if n ~= value then
value = n
factors = {}
end
table.insert(factors, { j, i })
end
end
end

return { value = #factors > 0 and value or nil, factors = factors }
end

return { smallest = smallest, largest = largest }
32 changes: 32 additions & 0 deletions exercises/practice/palindrome-products/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
local function render_factors(factors)
local rendered = {}
for _, factor in ipairs(factors) do
table.insert(rendered, string.format('{ %d, %d }', factor[1], factor[2]))
end
return '{ ' .. table.concat(rendered, ', ') .. ' }'
end

return {
module_name = 'palindrome_products',

generate_test = function(case)
if case.expected.error then
local template = [[
assert.has_error(function()
palindrome_products.%s(%d, %d)
end, '%s')]]

return template:format(case.property, case.input.min, case.input.max, case.expected.error)
else
local template = [[
local expected = {
value = %s, --
factors = %s
}
assert.are.same(expected, palindrome_products.%s(%d, %d))]]

return template:format(case.expected.value or 'nil', render_factors(case.expected.factors), case.property,
case.input.min, case.input.max)
end
end
}
49 changes: 49 additions & 0 deletions exercises/practice/palindrome-products/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.

[5cff78fe-cf02-459d-85c2-ce584679f887]
description = "find the smallest palindrome from single digit factors"

[0853f82c-5fc4-44ae-be38-fadb2cced92d]
description = "find the largest palindrome from single digit factors"

[66c3b496-bdec-4103-9129-3fcb5a9063e1]
description = "find the smallest palindrome from double digit factors"

[a10682ae-530a-4e56-b89d-69664feafe53]
description = "find the largest palindrome from double digit factors"

[cecb5a35-46d1-4666-9719-fa2c3af7499d]
description = "find the smallest palindrome from triple digit factors"

[edab43e1-c35f-4ea3-8c55-2f31dddd92e5]
description = "find the largest palindrome from triple digit factors"

[4f802b5a-9d74-4026-a70f-b53ff9234e4e]
description = "find the smallest palindrome from four digit factors"

[787525e0-a5f9-40f3-8cb2-23b52cf5d0be]
description = "find the largest palindrome from four digit factors"

[58fb1d63-fddb-4409-ab84-a7a8e58d9ea0]
description = "empty result for smallest if no palindrome in the range"

[9de9e9da-f1d9-49a5-8bfc-3d322efbdd02]
description = "empty result for largest if no palindrome in the range"

[12e73aac-d7ee-4877-b8aa-2aa3dcdb9f8a]
description = "error result for smallest if min is more than max"

[eeeb5bff-3f47-4b1e-892f-05829277bd74]
description = "error result for largest if min is more than max"

[16481711-26c4-42e0-9180-e2e4e8b29c23]
description = "smallest product does not use the smallest factor"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
local function smallest(min, max)

end

local function largest(min, max)

end

return { smallest = smallest, largest = largest }
103 changes: 103 additions & 0 deletions exercises/practice/palindrome-products/palindrome-products_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
local palindrome_products = require('palindrome-products')

describe('palindrome-products', function()
it('find the smallest palindrome from single digit factors', function()
local expected = {
value = 1, --
factors = { { 1, 1 } }
}
assert.are.same(expected, palindrome_products.smallest(1, 9))
end)

it('find the largest palindrome from single digit factors', function()
local expected = {
value = 9, --
factors = { { 1, 9 }, { 3, 3 } }
}
assert.are.same(expected, palindrome_products.largest(1, 9))
end)

it('find the smallest palindrome from double digit factors', function()
local expected = {
value = 121, --
factors = { { 11, 11 } }
}
assert.are.same(expected, palindrome_products.smallest(10, 99))
end)

it('find the largest palindrome from double digit factors', function()
local expected = {
value = 9009, --
factors = { { 91, 99 } }
}
assert.are.same(expected, palindrome_products.largest(10, 99))
end)

it('find the smallest palindrome from triple digit factors', function()
local expected = {
value = 10201, --
factors = { { 101, 101 } }
}
assert.are.same(expected, palindrome_products.smallest(100, 999))
end)

it('find the largest palindrome from triple digit factors', function()
local expected = {
value = 906609, --
factors = { { 913, 993 } }
}
assert.are.same(expected, palindrome_products.largest(100, 999))
end)

it('find the smallest palindrome from four digit factors', function()
local expected = {
value = 1002001, --
factors = { { 1001, 1001 } }
}
assert.are.same(expected, palindrome_products.smallest(1000, 9999))
end)

it('find the largest palindrome from four digit factors', function()
local expected = {
value = 99000099, --
factors = { { 9901, 9999 } }
}
assert.are.same(expected, palindrome_products.largest(1000, 9999))
end)

it('empty result for smallest if no palindrome in the range', function()
local expected = {
value = nil, --
factors = {}
}
assert.are.same(expected, palindrome_products.smallest(1002, 1003))
end)

it('empty result for largest if no palindrome in the range', function()
local expected = {
value = nil, --
factors = {}
}
assert.are.same(expected, palindrome_products.largest(15, 15))
end)

it('error result for smallest if min is more than max', function()
assert.has_error(function()
palindrome_products.smallest(10000, 1)
end, 'min must be <= max')
end)

it('error result for largest if min is more than max', function()
assert.has_error(function()
palindrome_products.largest(2, 1)
end, 'min must be <= max')
end)

it('smallest product does not use the smallest factor', function()
local expected = {
value = 10988901, --
factors = { { 3297, 3333 } }
}
assert.are.same(expected, palindrome_products.smallest(3215, 4000))
end)
end)

0 comments on commit 7fc95d1

Please sign in to comment.