From fa1cc4a43643fb9475b2432966822a41fb839b83 Mon Sep 17 00:00:00 2001 From: Kristofer Koishigawa Date: Sat, 28 Apr 2018 17:19:06 +0900 Subject: [PATCH] fix(challenges): Edit Description, Add Tests and Solution for Project Euler 41 (#17088) Updated the problem description with tags. Removed a couple of tests because they seemed too restrictive for the different approaches users might take when solving this problem. Also added a solution that user @elliotjz contributed to the fCC Arcade Mode. BREAKING CHANGE: None --- .../project-euler-problems.json | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/seed/challenges/08-coding-interview-questions-and-take-home-assignments/project-euler-problems.json b/seed/challenges/08-coding-interview-questions-and-take-home-assignments/project-euler-problems.json index 43ca6a8f78..d167537724 100644 --- a/seed/challenges/08-coding-interview-questions-and-take-home-assignments/project-euler-problems.json +++ b/seed/challenges/08-coding-interview-questions-and-take-home-assignments/project-euler-problems.json @@ -1491,11 +1491,9 @@ "title": "Problem 41: Pandigital prime", "tests": [ "assert(pandigitalPrime(4) == 4231, 'message: pandigitalPrime(4) should return 4231.');", - "assert(pandigitalPrime(5) == 0, 'message: pandigitalPrime(5) should return 0.');", - "assert(pandigitalPrime(6) == 0, 'message: pandigitalPrime(6) should return 0.');", "assert(pandigitalPrime(7) == 7652413, 'message: pandigitalPrime(7) should return 7652413.');" ], - "solutions": [], + "solutions": ["function pandigitalPrime(n) {\n function isPrime(num) {\n for (let i = 2, s = Math.sqrt(num); i <= s; i++) {\n if (num % i === 0) {\n return false;\n }\n }\n return num !== 1;\n }\n\n function getPermutations(n) {\n if (n === 1) {\n permutations.push(digitsArr.join(''));\n } else {\n for (let i = 0; i < n - 1; i++) {\n getPermutations(n - 1);\n // swap(n % 2 === 0 ? i : 0, n - 1);\n if (n % 2 === 0) {\n swap(i, n - 1);\n } else {\n swap(0, n - 1);\n }\n }\n getPermutations(n - 1);\n }\n }\n function swap(x, y) {\n let temp = digitsArr[x];\n digitsArr[x] = digitsArr[y];\n digitsArr[y] = temp;\n }\n let max = 0;\n let permutations = [];\n let digitsArr;\n let pandigitalNum = '';\n\n for (let max = n; max > 0; max--) {\n pandigitalNum += max;\n }\n\n for (let i = 0; i < pandigitalNum.length; i++) {\n if (max > 0) {\n break;\n } else {\n permutations = [];\n const currMax = pandigitalNum.slice(i);\n digitsArr = currMax.split('');\n getPermutations(digitsArr.length);\n\n // sort permutations in descending order\n permutations.sort(function(a, b) {\n return b - a;\n });\n\n for (let perm of permutations) {\n const thisPerm = parseInt(perm);\n if (isPrime(thisPerm)) {\n max = thisPerm;\n break;\n }\n }\n }\n }\n\n return max;\n}"], "translations": {}, "challengeSeed": [ "function pandigitalPrime(n) {", @@ -1506,8 +1504,8 @@ "pandigitalPrime(7);" ], "description": [ - "We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.", - "What is the largest n-length digit pandigital prime that exists?" + "We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.", + "What is the largest n-length digit pandigital prime that exists?" ] }, {