diff --git a/challenges/contentOrder/contentOrder.js b/challenges/contentOrder/contentOrder.js new file mode 100644 index 00000000..1ef92d15 --- /dev/null +++ b/challenges/contentOrder/contentOrder.js @@ -0,0 +1,81 @@ +// nextArticle is article we're looking at next +// previousArticle is already in potentialOrder +function isPostReqCheck(nextArticle, previousArticle, articlePrereqs) { + let isPostReq = false; + if (articlePrereqs[nextArticle].includes(previousArticle)) { + isPostReq = true; + } + return isPostReq; +} + +function isPrereqCheck(nextArticle, previousArticle, articlePrereqs) { + let isPrereq = false; + if (articlePrereqs[previousArticle].includes(nextArticle)) { + isPrereq = true; + } + return isPrereq; +} + +function noPreReqCheck(previousArticle, articlePrereqs) { + let noPreReqs = false; + if (articlePrereqs[previousArticle].length === 0) { + noPreReqs = true; + } + return noPreReqs; +} + +function noPreReqBothCheck(nextArticle, previousArticle, articlePrereqs) { + let noPreReqs = false; + if (articlePrereqs[nextArticle].length === 0 && articlePrereqs[previousArticle].length === 0) { + noPreReqs = true; + } + return noPreReqs; +} + +// function determinePreOrPostReqIndex(nextArticle, previousArticle, articlePrereqs) { +// let indexShift = 0; +// console.log('nextArticleArg'); +// console.log(nextArticle); +// console.log('previousArticle'); +// console.log(previousArticle); +// // nextArticle is preReq +// if (articlePrereqs[previousArticle].includes(nextArticle)) { +// console.log('// nextArticle is preReq'); +// indexShift = 0; +// } else if (articlePrereqs[nextArticle].includes(previousArticle)) { +// /* nextArticle is postReq */ +// console.log('// nextArticle is postReq'); +// indexShift = 1; +// } +// console.log(`AFTERSWITCH ${indexShift}`); +// return indexShift; +// } + +// //////////////// + +// linked list? +// bubble sort +function contentOrder(articleListArg, articlePrerequisites) { + const articleList = articleListArg; + /* initialize potentialOrder with first element */ + const potentialOrder = [articleList.shift()]; + for (let i = 0; i < potentialOrder.length; i++) { + const previousArticle = potentialOrder[i]; + for (let j = 0; j < articleList.length; j++) { + const nextArticle = articleList[j]; + if (isPostReqCheck(nextArticle, previousArticle, articlePrerequisites) + || noPreReqBothCheck(nextArticle, previousArticle, articlePrerequisites) + ) { + const removedArticle = articleList.splice(j, 1)[0]; + potentialOrder.splice(i + 1, 0, removedArticle); + i += 1; + } + } + } + + return potentialOrder; +} + +module.exports = { + contentOrder, isPrereqCheck, noPreReqCheck, isPostReqCheck, noPreReqBothCheck, +}; diff --git a/challenges/contentOrder/contentOrder.test.js b/challenges/contentOrder/contentOrder.test.js new file mode 100644 index 00000000..85f6e8e5 --- /dev/null +++ b/challenges/contentOrder/contentOrder.test.js @@ -0,0 +1,188 @@ +const { + contentOrder, isPrereqCheck, noPreReqCheck, isPostReqCheck, noPreReqBothCheck, +} = require('./contentOrder'); + +const testOne = { + articleList: ['A', 'B', 'C', 'D'], + articlePrereqs: { + A: ['B'], + B: ['C', 'D'], + C: [], + D: [], + }, + potentialOrders: [['C', 'D', 'B', 'A'], ['D', 'C', 'B', 'A']], +}; + +const testOneA = { + articleList: ['C', 'D', 'A', 'B'], + articlePrereqs: { + A: ['B'], + B: ['C', 'D'], + C: [], + D: [], + }, + potentialOrders: [['C', 'D', 'B', 'A'], ['D', 'C', 'B', 'A']], +}; + +const testOneB = { + articleList: ['D', 'A', 'C', 'B'], + articlePrereqs: { + A: ['B'], + B: ['C', 'D'], + C: [], + D: [], + }, + potentialOrders: [['C', 'D', 'B', 'A'], ['D', 'C', 'B', 'A']], +}; + +const testOneC = { + articleList: ['B', 'D', 'A', 'C'], + articlePrereqs: { + A: ['B'], + B: ['C', 'D'], + C: [], + D: [], + }, + potentialOrders: [['C', 'D', 'B', 'A'], ['D', 'C', 'B', 'A']], +}; + +const testTwo = { + inputNextArticle: 'B', + inputPreviousArticle: 'A', + articlePrereqs: { + A: ['B'], + B: ['C', 'D'], + C: [], + D: [], + }, + output: true, +}; + +const testTwoA = { + inputNextArticle: 'A', + inputPreviousArticle: 'B', + articlePrereqs: { + A: ['B'], + B: ['C', 'D'], + C: [], + D: [], + }, + output: true, +}; + +const testThree = { + inputNextArticle: 'B', + inputPreviousArticle: 'C', + articlePrereqs: { + A: ['B'], + B: ['C', 'D'], + C: [], + D: [], + }, + output: true, +}; + +const testThreeA = { + inputNextArticle: 'C', + inputPreviousArticle: 'D', + articlePrereqs: { + A: ['B'], + B: ['C', 'D'], + C: [], + D: [], + }, + output: true, +}; + +xdescribe('contentOrder Test', () => { + test('testOne', () => { + const result = contentOrder(testOne.articleList, testOne.articlePrereqs); + expect(testOne.potentialOrders.includes(result)).toBe(true); + }); +}); + +describe('contentOrder Test', () => { + xtest('testOne', () => { + const result = contentOrder(testOne.articleList, testOne.articlePrereqs); + expect(testOne.potentialOrders).toContainEqual(result); + }); + + xtest('testOneA', () => { + const result = contentOrder(testOneA.articleList, testOneA.articlePrereqs); + console.log('testOneA result'); + console.log(result); + expect(testOneA.potentialOrders).toContainEqual(result); + }); + + test('testOneB', () => { + const result = contentOrder(testOneB.articleList, testOneB.articlePrereqs); + console.log('testOneB result'); + console.log(result); + expect(testOneB.potentialOrders).toContainEqual(result); + }); + + xtest('testOneC', () => { + const result = contentOrder(testOneC.articleList, testOneC.articlePrereqs); + console.log('testOneB result'); + console.log(result); + expect(testOneC.potentialOrders).toContainEqual(result); + }); +}); + +describe('preReq Test', () => { + test('testTwo', () => { + const result = isPrereqCheck( + testTwo.inputNextArticle, + testTwo.inputPreviousArticle, + testTwo.articlePrereqs, + ); + expect(result).toBe(testTwo.output); + }); +}); + +describe('postReq Test', () => { + test('testTwoA', () => { + const result = isPostReqCheck( + testTwoA.inputNextArticle, + testTwoA.inputPreviousArticle, + testTwoA.articlePrereqs, + ); + expect(result).toBe(testTwoA.output); + }); +}); + +describe('noPreReq Test', () => { + test('testThree', () => { + const result = noPreReqCheck( + testThree.inputPreviousArticle, + testThree.articlePrereqs, + ); + expect(result).toBe(testThree.output); + }); +}); + +describe('noPreReqBothCheck Test', () => { + test('testThreeA', () => { + const result = noPreReqBothCheck( + testThreeA.inputNextArticle, + testThreeA.inputPreviousArticle, + testThreeA.articlePrereqs, + ); + expect(result).toBe(testThreeA.output); + }); +}); + +// /////////NOTES/////////// + +// function testCasePasses(testArg) { +// const potentialOrder = [['C', 'D', 'B', 'A'], ['D', 'C', 'B', 'A']]; +// const orderFound = articleOrder(testArg.articleList, testArg.articlePrereqs); +// console.log('orderFound'); +// console.log(orderFound); +// potentialOrder.forEach((possibleAnswer) => { +// if (orderFound === possibleAnswer) { +// return true; +// } +// }); +// return false; +// } diff --git a/challenges/contentOrder/contentOrderNotes.md b/challenges/contentOrder/contentOrderNotes.md new file mode 100644 index 00000000..3ddbb5e9 --- /dev/null +++ b/challenges/contentOrder/contentOrderNotes.md @@ -0,0 +1,269 @@ +//contentis important +// coach is sellingpoint + +//////Notes/////// + //if potentialOrder last element is prereq than add after, if it is post req, add before, +// if(potentialOrder[potentialOrder.length-1]){ + + +// } + +/////// +// function isPrereq(nextArticle, previousArticle, articlePrereqs) { +// let isPrequisite = false; +// if (articlePrereqs[previousArticle].includes(nextArticle)) { +// isPrequisite = true; +// } +// return isPrequisite; +// } + +/////Working in one case +function contentOrder(articleListArg, articlePrerequisites) { + const articleList = articleListArg; + const potentialOrder = [articleList.shift()]; /* initialize potentialOrder + with first element */ + + while (articleList.length) { + const nextArticle = articleList.splice(0, 1)[0]; + const previousArticle = potentialOrder[0]; + + /* if the next article is a prereq or the previous article has no prereq + add the next article ot beginning of potentialOrder */ + if (noPreReqCheck(previousArticle, articlePrerequisites) + || isPrereqCheck(nextArticle, previousArticle, articlePrerequisites)) { + potentialOrder.splice(0, 0, nextArticle); + } + } + + return potentialOrder; +} + +//////Iteration6///////// +function contentOrder(articleListArg, articlePrerequisites) { + const articleList = articleListArg; + // const potentialOrder = []; + for (let i = (articleList.length - 1); i > -1; i--) { + const nextArticle = articleList.splice(i, 1)[0]; + // console.log('nextArticle'); + // console.log(nextArticle); + console.log('i outer loop'); + console.log(i); + console.log('articleList outer loop'); + console.log(articleList); + for (let j = i - 1; j > -1; j--) { + const currentArticle = articleList[j]; + console.log('j inside loop'); + console.log(j); + // console.log('currentArticle'); + // console.log(currentArticle); + // if next article is preReq put it in front and start over + if (isPrereqCheck(nextArticle, currentArticle, articlePrerequisites)) { + articleList.splice(j, 0, nextArticle); + // potentialOrder.splice(j, 0, nextArticle); + console.log('articleList inside loop'); + console.log(articleList); + // console.log('potentialOrder inside loop'); + // console.log(potentialOrder); + } + } + } + // console.log('articleList before return'); + // console.log(articleList); + // console.log('potentialOrder before return'); + // console.log(potentialOrder); + return articleList; +} + + +///////////////Iteration5///////////// +function contentOrder(articleListArg, articlePrerequisites) { + const articleList = articleListArg; + const potentialOrder = [articleList.shift()]; /* initialize potentialOrder with first element */ + + while (articleList.length) { + const nextArticle = articleList.splice(0, 1)[0]; + const previousArticle = potentialOrder[0]; + + /* if the next article is a prereq or the previous article has no prereq + add the next article ot beginning of potentialOrder */ + if (noPreReqCheck(previousArticle, articlePrerequisites) + || isPrereqCheck(nextArticle, previousArticle, articlePrerequisites)) { + potentialOrder.splice(0, 0, nextArticle); + } + } + + return potentialOrder; +} + +///////////Iteration4///////////////// +// function contentOrder(articleListArg, articlePrerequisites) { +// let articleList = articleListArg; +// let potentialOrder = [articleList.shift()]; /* initialize potentialOrder with first element */ +// +// while (articleList.length) { +// // let nextArticle = articleList[0]; +// // let removedArticle = articleList.splice(0, 1)[0]; +// let nextArticle = articleList.splice(0, 1)[0]; +// let previousArticle = potentialOrder[0]; +// +// //if the next article is a prereq or the previous article has no prereq +// //add the next article ot beginning of potentialOrder +// if (noPreReqCheck(previousArticle, articlePrerequisites) +// || isPrereqCheck(nextArticle, previousArticle, articlePrerequisites)) { +// potentialOrder.splice(0, 0, nextArticle); +// } +// } +// +// console.log('potentialOrder'); +// console.log(potentialOrder); +// return potentialOrder; +// } + +///////////Iteration3///////////////// +// while (articleList.length > 0) { +// // for (let i = 0; i < articleList.length; i++) { /* find prereq */ +// console.log('articleList BEFORE chek'); +// console.log(articleList); +// // console.log('articleList i'); +// // console.log(i); +// const nextArticle = articleList[0]; +// console.log('nextArticle'); +// console.log(nextArticle); +// const previousArticle = potentialOrder[0]; +// if (noPreReqCheck(previousArticle, articlePrerequisites) +// || isPrereqCheck(nextArticle, previousArticle, articlePrerequisites)) { +// const removedArticle = articleList.splice(0, 1)[0]; +// potentialOrder.splice(0, 0, removedArticle); +// // after putting the article in, restart and look at +// // i = 0; +// // console.log(`Right after setting i to :${i}`); +// // continue; +// } +// console.log('articleList AFTER chek'); +// console.log(articleList); +// // } +// } + +// //////Iteration 2//////// + +// function contentOrder(articleList, articlePrerequisites) { +// const potentialOrder = [articleList.shift()]; +// +// while(articleList.length) { +// // const nextArticle = articleList.shift(); +// const nextArticle = articleList[0]; +// for(let i=(articleList.length-1);i>0;i--){ +// const previousArticle = potentialOrder[0]; +// +// if (isPrereqCheck(nextArticle, previousArticle, articlePrerequisites)) { +// console.log('nextArticle'); +// console.log(nextArticle); +// potentialOrder.splice(i, 0, articleList.shift()); +// continue; +// } +// // potentialOrder.splice((i + 1), 0, nextArticle); +// continue +// } +// } +// +// console.log('potentialOrder') +// console.log(potentialOrder) +// return potentialOrder; +// } + +// //////Iteration 1//////// + +// function contentOrder(articleList, articlePrerequisites) { +// const potentialOrder = [articleList.shift()]; +// +// // look at article list, put article item in potential order +// // if the article meets order condition +// for (let i = 0; i < articleList.length; i++) { +// const nextArticle = articleList[i]; +// const previousArticle = potentialOrder[potentialOrder.length - 1]; +// +// for (let j = 0; j < potentialOrder.length; j++) { +// // if next article is prequisite, add to before, else add after +// if (isPrereqCheck(nextArticle, previousArticle, articlePrerequisites)) { +// console.log('nextArticle'); +// console.log(nextArticle); +// potentialOrder.splice(j, 0, nextArticle); +// // continue; +// } else { +// potentialOrder.splice((j + 1), 0, nextArticle); +// } +// } +// } +// console.log('potentialOrder'); +// console.log(potentialOrder); +// return potentialOrder; +// } + +//////////Rando Notes + +// nextArticle is article we're looking at next +// previousArticle is already in potentialOrder +function determinePreOrPostReqIndex(nextArticle, previousArticle, articlePrereqs) { + let indexShift = 0; + console.log('BEFORESWITCH'); + console.log('BEFORESWITCH'); + console.log('nextArticleArg'); + console.log(nextArticle); + console.log('previousArticle'); + console.log(previousArticle); + console.log('articlePrereqs[nextArticleArg].includes(previousArticle)'); + console.log(articlePrereqs[nextArticle].includes(previousArticle)); + console.log('articlePrereqs[previousArticle].includes(nextArticle)'); + console.log(articlePrereqs[previousArticle].includes(nextArticle)); + switch (nextArticle) { + // console.log('INSWITCH'); + // nextArticle is preReq + case 'A': + console.log('NEXTARTICLE===A'); + break; + case articlePrereqs[previousArticle].includes(nextArticle): + console.log('// nextArticle is preReq'); + indexShift = -1; + break; + // nextArticle is postReq + case articlePrereqs[nextArticle].includes(previousArticle): + console.log('// nextArticle is postReq'); + indexShift = 1; + break; + default: + console.log('default:'); + indexShift = 0; + } + // }(nextArticleArg); + console.log(`AFTERSWITCH ${indexShift}`); + return indexShift; +} + +function contentOrder(articleListArg, articlePrerequisites) { + const articleList = articleListArg; + /* initialize potentialOrder with first element */ + const potentialOrder = [articleList.shift()]; + // let doItAgain = true + for (let i = 0; i < potentialOrder.length; i++) { + for (let j = 0; j < articleList.length; j++) { + // check if + const preOrPostReqIndex = determinePreOrPostReqIndex( + potentialOrder[i], + articleList[j], + articlePrerequisites, + ); + console.log('preOrPostReqIndex'); + console.log(preOrPostReqIndex); + if (preOrPostReqIndex === 0 || preOrPostReqIndex === 1) { + console.log('IS PRE OR POST'); + const removedArticle = articleList.splice(j, 1)[0]; + console.log('potentialOrderBEFORE', potentialOrder); + potentialOrder.splice(i + preOrPostReqIndex, 0, removedArticle); + console.log('potentialOrderAFTER', potentialOrder); + // let doItAgain = true + } + } + } + + return potentialOrder; +} diff --git a/challenges/contentOrder/contentOrderSpec.md b/challenges/contentOrder/contentOrderSpec.md new file mode 100644 index 00000000..74e9cad6 --- /dev/null +++ b/challenges/contentOrder/contentOrderSpec.md @@ -0,0 +1,13 @@ +//1 unique content,should notberepeated,willnotshow morethanonce +//2 order cannot break on article level +//3 +//get articles, find valid curriculum + +input: + - "A" "B" "C" "D" + - "A": ["B"], "B": ["C", "D"], "C": [], "D": [] + +output: + - "C" "D" "B" "A" + - "D" "C" "B" "A" + */ diff --git a/helpers/generateSolution.js b/helpers/generateSolution.js new file mode 100644 index 00000000..bb0148c7 --- /dev/null +++ b/helpers/generateSolution.js @@ -0,0 +1,88 @@ +/* eslint-disable */ +// TODO: enable linting + +const fs = require('fs'); + +const directory = 'challenges'; +const solutionName = 'contentOrder'; + +const spec = `${solutionName} Spec go here!`; +const notes = `${solutionName} Notes go here!`; +const solutionJS = `function ${solutionName}(input) { + +} + +module.exports = ${solutionName} +`; +const solutionTestJS = `const ${solutionName} = require('./${solutionName}') + +const testOne = { + input: '', + output: '' +} + +describe('${solutionName} Test', () => { + + test('testOne', ()=>{ + let result = ${solutionName}(testOne.input) + expect(result).toEqual(testOne.output); + }) + +}) +`; + +module.exports.generateDefaultSolution = function () { + // make folder with solution name + // Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist. + fs.mkdir(`./${directory}/${solutionName}`, { recursive: true }, (err) => { + if (err) throw err; + }); + + // make spec.md file -> //make ${solution}Spec.md file + fs.writeFile( + `./${directory}/${solutionName}/${solutionName}Spec.md`, + spec, + (err) => { + // throws an error, you could also catch it here + if (err) throw err; + // success case, the file was saved + console.log(`${solutionName}Spec.md saved!`); + }, + ); + + // make notes.md file -> //make ${solution}notes.md file + fs.writeFile( + `./${directory}/${solutionName}/${solutionName}Notes.md`, + notes, + (err) => { + // throws an error, you could also catch it here + if (err) throw err; + // success case, the file was saved + console.log(`${solutionName}notes.md saved!`); + }, + ); + + // make solution.js file -> //make ${solution}solution.js file + fs.writeFile( + `./${directory}/${solutionName}/${solutionName}.js`, + solutionJS, + (err) => { + // throws an error, you could also catch it here + if (err) throw err; + // success case, the file was saved + console.log(`${solutionName}.js saved!`); + }, + ); + + // make solution.test.js file -> //make ${solution}solution.test.js file + fs.writeFile( + `./${directory}/${solutionName}/${solutionName}.test.js`, + solutionTestJS, + (err) => { + // throws an error, you could also catch it here + if (err) throw err; + // success case, the file was saved + console.log(`${solutionName}.test.js saved!`); + }, + ); +}; diff --git a/package-lock.json b/package-lock.json index 98da7a61..7c9498db 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2,7 +2,6 @@ "name": "codepractice", "version": "1.0.0", "lockfileVersion": 1, - "requires": true, "dependencies": { "@babel/code-frame": { "version": "7.8.3", diff --git a/package.json b/package.json index 822221a5..3db7a89b 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,9 @@ "main": "algorithm.js", "scripts": { "pretest": "eslint --ignore-path .gitignore .", + "testWatch": "jest --watch", + "testWatchAll": "jest --watchAll", + "generateSolution": "node -e 'require(\"./helpers/generateSolution\").generateDefaultSolution()'", "test": "jest --config jest.config.js", "binarySearch": "node -e 'require(\"./algorithms/binarySearch/binarySearch\").runBinarySearch()'", "bubbleSort": "node -e 'require(\"./algorithms/bubbleSort/bubbleSort\").runBubbleSort()'",