From 912743172f676b9e044673d3b2fb97a0f58e1d70 Mon Sep 17 00:00:00 2001 From: Chris McDermut Date: Thu, 7 May 2020 20:27:04 -0700 Subject: [PATCH 1/5] saving shit --- .eslintrc.json | 3 +- challenges/contentOrder/contentOrder.js | 39 +++++++++++++++++++ challenges/contentOrder/contentOrder.test.js | 34 ++++++++++++++++ challenges/contentOrder/contentOrderNotes.md | 9 +++++ challenges/contentOrder/contentOrderSpec.md | 13 +++++++ challenges/shortestString/shortestString.js | 3 ++ .../shortestString/shortestString.test.js | 2 +- helpers/generateSolution.js | 6 +-- 8 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 challenges/contentOrder/contentOrder.js create mode 100644 challenges/contentOrder/contentOrder.test.js create mode 100644 challenges/contentOrder/contentOrderNotes.md create mode 100644 challenges/contentOrder/contentOrderSpec.md diff --git a/.eslintrc.json b/.eslintrc.json index 38f7cd40..793d813b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -16,6 +16,7 @@ "ecmaVersion": 2018 }, "rules": { - "no-plusplus": [2, { "allowForLoopAfterthoughts": true }] + "no-plusplus": [2, { "allowForLoopAfterthoughts": true }], + "no-continue": 0 } } diff --git a/challenges/contentOrder/contentOrder.js b/challenges/contentOrder/contentOrder.js new file mode 100644 index 00000000..28d4681c --- /dev/null +++ b/challenges/contentOrder/contentOrder.js @@ -0,0 +1,39 @@ +function isPrereq(nextArticle, previousArticle, articlePrereqs) { + let isPrequisite = false; + if (articlePrereqs[previousArticle].includes(nextArticle)) { + isPrequisite = true; + } + return isPrequisite; +} + +// make sure where it is put +function contentOrder(articleList, articlePrerequisites) { + const potentialOrder = []; + + // 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 currentArticle = articleList[i]; + // last article in finalList + const previousArticle = potentialOrder[potentialOrder.length - 1]; + // if potentialOrder is empty, initialize + if (i === 0) { + potentialOrder.push(articleList.shift()); + continue; + } + + // if next article is prequisite, add to before, else add after + if (isPrereq(currentArticle, previousArticle, articlePrerequisites)) { + // add before + console.log('currentArticle'); + console.log(currentArticle); + potentialOrder.splice(potentialOrder.length, 0, currentArticle); + } else { + potentialOrder.push(currentArticle); + } + } + // return ["C","D","B","A"] //true + // return ["C","D","H","T"] //false + return potentialOrder; +} + +module.exports = contentOrder; diff --git a/challenges/contentOrder/contentOrder.test.js b/challenges/contentOrder/contentOrder.test.js new file mode 100644 index 00000000..4644f68c --- /dev/null +++ b/challenges/contentOrder/contentOrder.test.js @@ -0,0 +1,34 @@ +const contentOrder = 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']], +}; + +describe('contentOrder Test', () => { + test('testOne', () => { + const result = contentOrder(testOne.articleList, testOne.articlePrerequisites); + expect(testOne.potentialOrders.includes(result)).toBe(true); + }); +}); + +// /////////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..42e50109 --- /dev/null +++ b/challenges/contentOrder/contentOrderNotes.md @@ -0,0 +1,9 @@ +//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]){ + + +// } 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/challenges/shortestString/shortestString.js b/challenges/shortestString/shortestString.js index 5fbc8b73..c47c767c 100644 --- a/challenges/shortestString/shortestString.js +++ b/challenges/shortestString/shortestString.js @@ -1,3 +1,6 @@ +/* eslint-disable */ +// TODO: finish this problem + // remove by finding section of length k with // section of K elements with X property // what is that property? diff --git a/challenges/shortestString/shortestString.test.js b/challenges/shortestString/shortestString.test.js index 522785f7..f5b1ce58 100644 --- a/challenges/shortestString/shortestString.test.js +++ b/challenges/shortestString/shortestString.test.js @@ -68,7 +68,7 @@ xdescribe('shortestString condenseString test', () => { }); }); -describe('shortestString removeSection Test', () => { +xdescribe('shortestString removeSection Test', () => { test('test1 removeSection', () => { const result = removeSection(test1.input[0], test1.input[1]); expect(result).toEqual(test1.modifiedString); diff --git a/helpers/generateSolution.js b/helpers/generateSolution.js index 30151046..bb0148c7 100644 --- a/helpers/generateSolution.js +++ b/helpers/generateSolution.js @@ -4,7 +4,7 @@ const fs = require('fs'); const directory = 'challenges'; -const solutionName = 'shortestString'; +const solutionName = 'contentOrder'; const spec = `${solutionName} Spec go here!`; const notes = `${solutionName} Notes go here!`; @@ -46,7 +46,7 @@ module.exports.generateDefaultSolution = function () { // throws an error, you could also catch it here if (err) throw err; // success case, the file was saved - console.log('Spec.md saved!'); + console.log(`${solutionName}Spec.md saved!`); }, ); @@ -58,7 +58,7 @@ module.exports.generateDefaultSolution = function () { // throws an error, you could also catch it here if (err) throw err; // success case, the file was saved - console.log('notes.md saved!'); + console.log(`${solutionName}notes.md saved!`); }, ); From c3c2df64bfbcd71e3020e37b5358f4a8a9938b77 Mon Sep 17 00:00:00 2001 From: Chris McDermut Date: Fri, 8 May 2020 23:17:56 -0700 Subject: [PATCH 2/5] close to success --- challenges/contentOrder/contentOrder.js | 173 ++++++++++++++++--- challenges/contentOrder/contentOrder.test.js | 29 +++- challenges/contentOrder/contentOrderNotes.md | 9 + package-lock.json | 1 - package.json | 2 + 5 files changed, 185 insertions(+), 29 deletions(-) diff --git a/challenges/contentOrder/contentOrder.js b/challenges/contentOrder/contentOrder.js index 28d4681c..5984b7ca 100644 --- a/challenges/contentOrder/contentOrder.js +++ b/challenges/contentOrder/contentOrder.js @@ -1,4 +1,8 @@ -function isPrereq(nextArticle, previousArticle, articlePrereqs) { +/* eslint-disable */ +// TODO: enable linting + +// nextArticle = nextArticle we're checking +function isPostReq(nextArticle, previousArticle, articlePrereqs) { let isPrequisite = false; if (articlePrereqs[previousArticle].includes(nextArticle)) { isPrequisite = true; @@ -6,34 +10,151 @@ function isPrereq(nextArticle, previousArticle, articlePrereqs) { return isPrequisite; } -// make sure where it is put -function contentOrder(articleList, articlePrerequisites) { - const potentialOrder = []; - - // 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 currentArticle = articleList[i]; - // last article in finalList - const previousArticle = potentialOrder[potentialOrder.length - 1]; - // if potentialOrder is empty, initialize - if (i === 0) { - potentialOrder.push(articleList.shift()); - continue; - } +// nextArticle = nextArticle we're checking +function isPrereqCheck(nextArticle, previousArticle, articlePrereqs) { + let isPrereq = false; + if (articlePrereqs[previousArticle].includes(nextArticle)) { + isPrereq = true; + } + return isPrereq; +} + +// nextArticle = nextArticle we're checking +function noPreReqCheck(previousArticle, articlePrereqs) { + let noPreReqs = false; + console.log('articlePrereqs[previousArticle]'); + console.log(articlePrereqs[previousArticle]); + if (articlePrereqs[previousArticle] === []) { + noPreReqs = true; + } + return noPreReqs; +} + +function contentOrder(articleListArg, articlePrerequisites) { + let articleList = articleListArg; + let potentialOrder = [articleList.shift()]; /* initialize potentialOrder with first element */ - // if next article is prequisite, add to before, else add after - if (isPrereq(currentArticle, previousArticle, articlePrerequisites)) { - // add before - console.log('currentArticle'); - console.log(currentArticle); - potentialOrder.splice(potentialOrder.length, 0, currentArticle); - } else { - potentialOrder.push(currentArticle); + while (articleList.length) { + let nextArticle = articleList.splice(0, 1)[0]; + console.log('nextArticle') + console.log(nextArticle) + let previousArticle = potentialOrder[0]; + console.log('previousArticle') + console.log(previousArticle) + + //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 ["C","D","B","A"] //true - // return ["C","D","H","T"] //false + return potentialOrder; } -module.exports = contentOrder; +module.exports = { contentOrder, isPrereqCheck, isPostReq }; + +///////////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; +// } diff --git a/challenges/contentOrder/contentOrder.test.js b/challenges/contentOrder/contentOrder.test.js index 4644f68c..88abe33f 100644 --- a/challenges/contentOrder/contentOrder.test.js +++ b/challenges/contentOrder/contentOrder.test.js @@ -1,4 +1,4 @@ -const contentOrder = require('./contentOrder'); +const { contentOrder, isPrereqCheck } = require('./contentOrder'); const testOne = { articleList: ['A', 'B', 'C', 'D'], @@ -11,13 +11,38 @@ const testOne = { 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, +}; + describe('contentOrder Test', () => { test('testOne', () => { - const result = contentOrder(testOne.articleList, testOne.articlePrerequisites); + const result = contentOrder(testOne.articleList, testOne.articlePrereqs); + console.log('result'); + console.log(result); expect(testOne.potentialOrders.includes(result)).toBe(true); }); }); +describe('preReq Test', () => { + test('testTwo', () => { + const result = isPrereqCheck( + testTwo.inputNextArticle, + testTwo.inputPreviousArticle, + testTwo.articlePrereqs, + ); + expect(result).toBe(true); + }); +}); + // /////////NOTES/////////// // function testCasePasses(testArg) { diff --git a/challenges/contentOrder/contentOrderNotes.md b/challenges/contentOrder/contentOrderNotes.md index 42e50109..981a7f1b 100644 --- a/challenges/contentOrder/contentOrderNotes.md +++ b/challenges/contentOrder/contentOrderNotes.md @@ -7,3 +7,12 @@ // } + +/////// +// function isPrereq(nextArticle, previousArticle, articlePrereqs) { +// let isPrequisite = false; +// if (articlePrereqs[previousArticle].includes(nextArticle)) { +// isPrequisite = true; +// } +// return isPrequisite; +// } diff --git a/package-lock.json b/package-lock.json index 06750012..3857e3fe 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 101cbc96..8001398f 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,8 @@ "scripts": { "pretest": "eslint --ignore-path .gitignore .", "test": "jest", + "testWatch": "jest --watch", + "testWatchAll": "jest --watchAll", "generateSolution": "node -e 'require(\"./helpers/generateSolution\").generateDefaultSolution()'", "reverseWords": "node -e 'require(\"./challenges/reverseWords/reverseWords\").reverseWords()'", "bubbleSort": "node -e 'require(\"./bubbleSort-4-6-19\").bubbleSort()'", From 4ad684c24797b868cb2858916db08bf4142c6047 Mon Sep 17 00:00:00 2001 From: Chris McDermut Date: Sat, 9 May 2020 00:18:19 -0700 Subject: [PATCH 3/5] finally got it working, one of the test cases was wrong, fuckkkkk --- challenges/contentOrder/contentOrder.js | 131 ++----------------- challenges/contentOrder/contentOrder.test.js | 37 +++++- challenges/contentOrder/contentOrderNotes.md | 104 +++++++++++++++ 3 files changed, 146 insertions(+), 126 deletions(-) diff --git a/challenges/contentOrder/contentOrder.js b/challenges/contentOrder/contentOrder.js index 5984b7ca..068d5fde 100644 --- a/challenges/contentOrder/contentOrder.js +++ b/challenges/contentOrder/contentOrder.js @@ -1,6 +1,3 @@ -/* eslint-disable */ -// TODO: enable linting - // nextArticle = nextArticle we're checking function isPostReq(nextArticle, previousArticle, articlePrereqs) { let isPrequisite = false; @@ -22,28 +19,22 @@ function isPrereqCheck(nextArticle, previousArticle, articlePrereqs) { // nextArticle = nextArticle we're checking function noPreReqCheck(previousArticle, articlePrereqs) { let noPreReqs = false; - console.log('articlePrereqs[previousArticle]'); - console.log(articlePrereqs[previousArticle]); - if (articlePrereqs[previousArticle] === []) { + if (articlePrereqs[previousArticle].length === 0) { noPreReqs = true; } return noPreReqs; } function contentOrder(articleListArg, articlePrerequisites) { - let articleList = articleListArg; - let potentialOrder = [articleList.shift()]; /* initialize potentialOrder with first element */ + const articleList = articleListArg; + const potentialOrder = [articleList.shift()]; /* initialize potentialOrder with first element */ while (articleList.length) { - let nextArticle = articleList.splice(0, 1)[0]; - console.log('nextArticle') - console.log(nextArticle) - let previousArticle = potentialOrder[0]; - console.log('previousArticle') - console.log(previousArticle) + 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 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); @@ -53,108 +44,6 @@ function contentOrder(articleListArg, articlePrerequisites) { return potentialOrder; } -module.exports = { contentOrder, isPrereqCheck, isPostReq }; - -///////////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; -// } +module.exports = { + contentOrder, isPrereqCheck, noPreReqCheck, isPostReq, +}; diff --git a/challenges/contentOrder/contentOrder.test.js b/challenges/contentOrder/contentOrder.test.js index 88abe33f..f74d33c0 100644 --- a/challenges/contentOrder/contentOrder.test.js +++ b/challenges/contentOrder/contentOrder.test.js @@ -1,4 +1,4 @@ -const { contentOrder, isPrereqCheck } = require('./contentOrder'); +const { contentOrder, isPrereqCheck, noPreReqCheck } = require('./contentOrder'); const testOne = { articleList: ['A', 'B', 'C', 'D'], @@ -23,15 +23,32 @@ const testTwo = { output: true, }; -describe('contentOrder Test', () => { +const testThree = { + inputNextArticle: 'B', + inputPreviousArticle: 'C', + articlePrereqs: { + A: ['B'], + B: ['C', 'D'], + C: [], + D: [], + }, + output: true, +}; + +xdescribe('contentOrder Test', () => { test('testOne', () => { const result = contentOrder(testOne.articleList, testOne.articlePrereqs); - console.log('result'); - console.log(result); expect(testOne.potentialOrders.includes(result)).toBe(true); }); }); +describe('contentOrder Test', () => { + test('testOne', () => { + const result = contentOrder(testOne.articleList, testOne.articlePrereqs); + expect(testOne.potentialOrders).toContainEqual(result); + }); +}); + describe('preReq Test', () => { test('testTwo', () => { const result = isPrereqCheck( @@ -39,7 +56,17 @@ describe('preReq Test', () => { testTwo.inputPreviousArticle, testTwo.articlePrereqs, ); - expect(result).toBe(true); + expect(result).toBe(testTwo.output); + }); +}); + +describe('noPreReq Test', () => { + test('testThree', () => { + const result = noPreReqCheck( + testThree.inputPreviousArticle, + testThree.articlePrereqs, + ); + expect(result).toBe(testThree.output); }); }); diff --git a/challenges/contentOrder/contentOrderNotes.md b/challenges/contentOrder/contentOrderNotes.md index 981a7f1b..c133022b 100644 --- a/challenges/contentOrder/contentOrderNotes.md +++ b/challenges/contentOrder/contentOrderNotes.md @@ -16,3 +16,107 @@ // } // return isPrequisite; // } + +///////////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; +// } From be6a276e4f33f14b9529f61bc7fe4670f4f3bf68 Mon Sep 17 00:00:00 2001 From: Chris McDermut Date: Sun, 10 May 2020 22:57:45 -0700 Subject: [PATCH 4/5] need to figure out how to work with various orders --- challenges/contentOrder/contentOrder.js | 7 ++- challenges/contentOrder/contentOrder.test.js | 56 ++++++++++++++++++- challenges/contentOrder/contentOrderNotes.md | 58 ++++++++++++++++++++ 3 files changed, 119 insertions(+), 2 deletions(-) diff --git a/challenges/contentOrder/contentOrder.js b/challenges/contentOrder/contentOrder.js index 068d5fde..721f1170 100644 --- a/challenges/contentOrder/contentOrder.js +++ b/challenges/contentOrder/contentOrder.js @@ -27,7 +27,8 @@ function noPreReqCheck(previousArticle, articlePrereqs) { function contentOrder(articleListArg, articlePrerequisites) { const articleList = articleListArg; - const potentialOrder = [articleList.shift()]; /* initialize potentialOrder with first element */ + const potentialOrder = [articleList.shift()]; /* initialize potentialOrder + with first element */ while (articleList.length) { const nextArticle = articleList.splice(0, 1)[0]; @@ -44,6 +45,10 @@ function contentOrder(articleListArg, articlePrerequisites) { return potentialOrder; } +// linked list? +// bubble sort + + module.exports = { contentOrder, isPrereqCheck, noPreReqCheck, isPostReq, }; diff --git a/challenges/contentOrder/contentOrder.test.js b/challenges/contentOrder/contentOrder.test.js index f74d33c0..ebb2b705 100644 --- a/challenges/contentOrder/contentOrder.test.js +++ b/challenges/contentOrder/contentOrder.test.js @@ -11,6 +11,39 @@ const testOne = { 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', @@ -47,9 +80,30 @@ describe('contentOrder Test', () => { 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); + }); + + xtest('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', () => { +xdescribe('preReq Test', () => { test('testTwo', () => { const result = isPrereqCheck( testTwo.inputNextArticle, diff --git a/challenges/contentOrder/contentOrderNotes.md b/challenges/contentOrder/contentOrderNotes.md index c133022b..19acd39e 100644 --- a/challenges/contentOrder/contentOrderNotes.md +++ b/challenges/contentOrder/contentOrderNotes.md @@ -17,6 +17,64 @@ // return isPrequisite; // } + +//////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; From f6f700ad80be4867c23b99150d47126bec941d31 Mon Sep 17 00:00:00 2001 From: Chris McDermut Date: Mon, 11 May 2020 17:28:38 -0700 Subject: [PATCH 5/5] saving work --- challenges/contentOrder/contentOrder.js | 77 +++++++++++------ challenges/contentOrder/contentOrder.test.js | 56 +++++++++++- challenges/contentOrder/contentOrderNotes.md | 89 ++++++++++++++++++++ 3 files changed, 193 insertions(+), 29 deletions(-) diff --git a/challenges/contentOrder/contentOrder.js b/challenges/contentOrder/contentOrder.js index 721f1170..1ef92d15 100644 --- a/challenges/contentOrder/contentOrder.js +++ b/challenges/contentOrder/contentOrder.js @@ -1,13 +1,13 @@ -// nextArticle = nextArticle we're checking -function isPostReq(nextArticle, previousArticle, articlePrereqs) { - let isPrequisite = false; - if (articlePrereqs[previousArticle].includes(nextArticle)) { - isPrequisite = true; +// 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 isPrequisite; + return isPostReq; } -// nextArticle = nextArticle we're checking function isPrereqCheck(nextArticle, previousArticle, articlePrereqs) { let isPrereq = false; if (articlePrereqs[previousArticle].includes(nextArticle)) { @@ -16,7 +16,6 @@ function isPrereqCheck(nextArticle, previousArticle, articlePrereqs) { return isPrereq; } -// nextArticle = nextArticle we're checking function noPreReqCheck(previousArticle, articlePrereqs) { let noPreReqs = false; if (articlePrereqs[previousArticle].length === 0) { @@ -25,30 +24,58 @@ function noPreReqCheck(previousArticle, articlePrereqs) { 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; - 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); + /* 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; } -// linked list? -// bubble sort - - module.exports = { - contentOrder, isPrereqCheck, noPreReqCheck, isPostReq, + contentOrder, isPrereqCheck, noPreReqCheck, isPostReqCheck, noPreReqBothCheck, }; diff --git a/challenges/contentOrder/contentOrder.test.js b/challenges/contentOrder/contentOrder.test.js index ebb2b705..85f6e8e5 100644 --- a/challenges/contentOrder/contentOrder.test.js +++ b/challenges/contentOrder/contentOrder.test.js @@ -1,4 +1,6 @@ -const { contentOrder, isPrereqCheck, noPreReqCheck } = require('./contentOrder'); +const { + contentOrder, isPrereqCheck, noPreReqCheck, isPostReqCheck, noPreReqBothCheck, +} = require('./contentOrder'); const testOne = { articleList: ['A', 'B', 'C', 'D'], @@ -56,6 +58,18 @@ const testTwo = { output: true, }; +const testTwoA = { + inputNextArticle: 'A', + inputPreviousArticle: 'B', + articlePrereqs: { + A: ['B'], + B: ['C', 'D'], + C: [], + D: [], + }, + output: true, +}; + const testThree = { inputNextArticle: 'B', inputPreviousArticle: 'C', @@ -68,6 +82,18 @@ const testThree = { 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); @@ -76,7 +102,7 @@ xdescribe('contentOrder Test', () => { }); describe('contentOrder Test', () => { - test('testOne', () => { + xtest('testOne', () => { const result = contentOrder(testOne.articleList, testOne.articlePrereqs); expect(testOne.potentialOrders).toContainEqual(result); }); @@ -88,7 +114,7 @@ describe('contentOrder Test', () => { expect(testOneA.potentialOrders).toContainEqual(result); }); - xtest('testOneB', () => { + test('testOneB', () => { const result = contentOrder(testOneB.articleList, testOneB.articlePrereqs); console.log('testOneB result'); console.log(result); @@ -103,7 +129,7 @@ describe('contentOrder Test', () => { }); }); -xdescribe('preReq Test', () => { +describe('preReq Test', () => { test('testTwo', () => { const result = isPrereqCheck( testTwo.inputNextArticle, @@ -114,6 +140,17 @@ xdescribe('preReq Test', () => { }); }); +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( @@ -124,6 +161,17 @@ describe('noPreReq Test', () => { }); }); +describe('noPreReqBothCheck Test', () => { + test('testThreeA', () => { + const result = noPreReqBothCheck( + testThreeA.inputNextArticle, + testThreeA.inputPreviousArticle, + testThreeA.articlePrereqs, + ); + expect(result).toBe(testThreeA.output); + }); +}); + // /////////NOTES/////////// // function testCasePasses(testArg) { diff --git a/challenges/contentOrder/contentOrderNotes.md b/challenges/contentOrder/contentOrderNotes.md index 19acd39e..3ddbb5e9 100644 --- a/challenges/contentOrder/contentOrderNotes.md +++ b/challenges/contentOrder/contentOrderNotes.md @@ -17,6 +17,26 @@ // 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) { @@ -178,3 +198,72 @@ function contentOrder(articleListArg, articlePrerequisites) { // 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; +}