-
-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating tests and proof solution for palindrome-products
- Loading branch information
1 parent
a0862fe
commit 66eaa86
Showing
4 changed files
with
63 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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
114 changes: 32 additions & 82 deletions
114
exercises/practice/palindrome-products/.meta/proof.ci.js
This file contains 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,88 +1,38 @@ | ||
const reverseString = (str) => str.split('').reverse().join(''); | ||
|
||
class Palindrome { | ||
constructor(factor1, factor2) { | ||
this.value = factor1 * factor2; | ||
this.factors = [[factor1, factor2]]; | ||
} | ||
|
||
withFactors(factors) { | ||
this.factors.push(factors); | ||
return this; | ||
} | ||
|
||
valid() { | ||
const s = `${this.value}`; | ||
return s === reverseString(s); | ||
} | ||
|
||
merge(other) { | ||
other.factors.forEach((f) => { | ||
this.factors.push(f); | ||
}); | ||
return this; | ||
} | ||
} | ||
|
||
export class Palindromes { | ||
constructor(maxFactor, minFactor = 1) { | ||
this.maxFactor = maxFactor; | ||
this.minFactor = minFactor; | ||
} | ||
|
||
get largest() { | ||
let left = this.maxFactor, | ||
right = this.maxFactor, | ||
best = new Palindrome(this.minFactor, this.minFactor); | ||
|
||
while (right >= this.minFactor) { | ||
let p = new Palindrome(left, right); | ||
|
||
if (best.value && p.value < best.value) { | ||
right--; | ||
left = right; | ||
continue; | ||
} | ||
|
||
if (p.valid()) { | ||
if (best.value < p.value) { | ||
best = p; | ||
} else if (best.value === p.value) { | ||
best = p.merge(best); | ||
static generate({ minFactor, maxFactor }) { | ||
if (minFactor > maxFactor) throw new Error('min must be <= max'); | ||
let isPalindrome = (n) => | ||
[...n.toString()].reverse().join('') === n.toString(); | ||
let search = (n, pred, fn) => { | ||
while (pred(n)) { | ||
if (!isPalindrome(n)) { | ||
n = fn(n); | ||
continue; | ||
} | ||
} | ||
|
||
if (left <= this.minFactor) { | ||
right--; | ||
left = right; | ||
} else { | ||
left--; | ||
} | ||
} | ||
|
||
if (best.valid()) { | ||
return best; | ||
} | ||
|
||
return { value: null, factors: [] }; | ||
} | ||
|
||
get smallest() { | ||
for (let m = this.minFactor; m <= this.maxFactor; m += 1) { | ||
for (let n = m; n <= this.maxFactor; n += 1) { | ||
const p = new Palindrome(m, n); | ||
if (p.valid()) { | ||
return p; | ||
let factors = []; | ||
for (let p = minFactor; p <= n / p; p++) { | ||
if (n % p === 0) { | ||
let q = n / p; | ||
if (q <= maxFactor) factors.push([p, q]); | ||
} | ||
} | ||
if (factors.length > 0) return { value: n, factors }; | ||
n = fn(n); | ||
} | ||
} | ||
return { value: null, factors: [] }; | ||
} | ||
|
||
static generate(params) { | ||
if ((params.minFactor || 1) > params.maxFactor) { | ||
throw new Error('min must be <= max'); | ||
} | ||
return new Palindromes(params.maxFactor, params.minFactor || 1); | ||
return { value: null, factors: [] }; | ||
}; | ||
let [lower, upper] = [minFactor * minFactor, maxFactor * maxFactor]; | ||
return { | ||
largest: search( | ||
upper, | ||
(n) => n >= lower, | ||
(x) => x - 1, | ||
), | ||
smallest: search( | ||
lower, | ||
(n) => n <= upper, | ||
(x) => x + 1, | ||
), | ||
}; | ||
} | ||
} |
This file contains 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 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