From 1b67b7bcbb011d35eca9f4f721eca69819a8cabc Mon Sep 17 00:00:00 2001 From: Chris McDermut Date: Wed, 9 Dec 2020 21:22:27 -0700 Subject: [PATCH] saving work for course order --- .../determineCourseOrder.js | 52 +++++++++++ .../determineCourseOrder.test.js | 92 +++++++++++++++++++ .../determineCourseOrderNotes.md | 1 + .../determineCourseOrderSpec.md | 53 +++++++++++ helpers/generateBoilerPlate.js | 2 +- 5 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 challenges/determineCourseOrder/determineCourseOrder.js create mode 100644 challenges/determineCourseOrder/determineCourseOrder.test.js create mode 100644 challenges/determineCourseOrder/determineCourseOrderNotes.md create mode 100644 challenges/determineCourseOrder/determineCourseOrderSpec.md diff --git a/challenges/determineCourseOrder/determineCourseOrder.js b/challenges/determineCourseOrder/determineCourseOrder.js new file mode 100644 index 0000000..941dfef --- /dev/null +++ b/challenges/determineCourseOrder/determineCourseOrder.js @@ -0,0 +1,52 @@ +// nextCourse= course we're checking +// currentCourse is example below +// ["Prereq", "NextLevelCourse"], +// ["Data Structures", "Algorithms"] +function determineIsPrereq(nextCourse, currentCourse) { + let isPrereq = false; + if (nextCourse === currentCourse[0]) { + isPrereq = true; + } + return isPrereq; +} + +function determineCourseOrder(courseList) { + const finalCourseList = [courseList[0][0]]; + + for (let i = 0; i < courseList.length; i++) { + // insert forwards or backwards base on prereq + for (let j = 0; j < finalCourseList.length; j++) { + if (determineIsPrereq(courseList[i], finalCourseList[j])) { + finalCourseList.splice(j, 0, courseList[i]); + break; + } else { + finalCourseList.splice(j + 1, 0, courseList[i]); + break; + } + } + } + console.log(finalCourseList); + return finalCourseList; +} + +/* works */ +function findHalfwayCourse(finalCourseList) { + const halfway = (finalCourseList.length) / 2; + const halfwayIndex = Math.floor(halfway); + const halfwayCourse = finalCourseList[halfwayIndex]; + return halfwayCourse; +} + + +function finalFunction(courses) { + const finalCourseList = determineCourseOrder(courses); + + let finalCourse = ''; + finalCourse = findHalfwayCourse(finalCourseList); + + return finalCourse; +} + +module.exports = { + determineIsPrereq, determineCourseOrder, findHalfwayCourse, finalFunction, +}; diff --git a/challenges/determineCourseOrder/determineCourseOrder.test.js b/challenges/determineCourseOrder/determineCourseOrder.test.js new file mode 100644 index 0000000..b3ac3b1 --- /dev/null +++ b/challenges/determineCourseOrder/determineCourseOrder.test.js @@ -0,0 +1,92 @@ +const { + isPrereq, determineCourseOrder, findHalfwayCourse, finalFunction, +} = require('./determineCourseOrder'); + +describe('determineCourseOrder Test', () => { + test('testOne', () => { + const testCase1 = { + input: [ + ['Data Structures', 'Algorithms'], + ['Foundations of Computer Science', 'Operating Systems'], + ['Computer Networks', 'Computer Architecture'], + ['Algorithms', 'Foundations of Computer Science'], + ['Computer Architecture', 'Data Structures'], + ['Software Design', 'Computer Networks'], + ], + order: ['Software Design', + 'Computer Networks', + 'Computer Architecture', + 'Data Structures', + 'Algorithms', + 'Foundations of Computer Science', + 'Operating Systems', + ], + output: 'Data Structures', + }; + const result = determineCourseOrder(testCase1.input); + expect(result).toEqual(testCase1.output); + }); + + test('testOne', () => { + const testCase2 = { + input: [ + ['Data Structures', 'Algorithms'], + ['Algorithms', 'Foundations of Computer Science'], + ['Foundations of Computer Science', 'Logic'], + ], + output: 'Algorithms', + }; + const result = determineCourseOrder(testCase2.input); + expect(result).toEqual(testCase2.output); + }); + + test('testOne', () => { + const testCase3 = { + input: [ + ['Data Structures', 'Algorithms'], + ], + output: 'Data Structures', + }; + const result = determineCourseOrder(testCase3.input); + expect(result).toEqual(testCase3.output); + }); +}); + +describe('isPrereq Test', () => { + test('testOne', () => { + const testCase3 = { + input: [ + ['Data Structures', 'Algorithms'], + ], + output: 'Data Structures', + }; + const result = isPrereq(testCase3.input); + expect(result).toEqual(testCase3.output); + }); +}); + +describe('findHalfwayCourse Test', () => { + test('testOne', () => { + const testCase3 = { + input: [ + ['Data Structures', 'Algorithms'], + ], + output: 'Data Structures', + }; + const result = findHalfwayCourse(testCase3.input); + expect(result).toEqual(testCase3.output); + }); +}); + +describe('finalFunction Test', () => { + test('testOne', () => { + const testCase3 = { + input: [ + ['Data Structures', 'Algorithms'], + ], + output: 'Data Structures', + }; + const result = finalFunction(testCase3.input); + expect(result).toEqual(testCase3.output); + }); +}); diff --git a/challenges/determineCourseOrder/determineCourseOrderNotes.md b/challenges/determineCourseOrder/determineCourseOrderNotes.md new file mode 100644 index 0000000..c296b4c --- /dev/null +++ b/challenges/determineCourseOrder/determineCourseOrderNotes.md @@ -0,0 +1 @@ +determineCourseOrder Notes go here! diff --git a/challenges/determineCourseOrder/determineCourseOrderSpec.md b/challenges/determineCourseOrder/determineCourseOrderSpec.md new file mode 100644 index 0000000..f680908 --- /dev/null +++ b/challenges/determineCourseOrder/determineCourseOrderSpec.md @@ -0,0 +1,53 @@ +determineCourseOrder Spec goes here! + +/* +You're developing a system for scheduling advising meetings with students in a Computer Science program. Each meeting should be scheduled when a student has completed 50% of their academic program. + +Each course at our university has at most one prerequisite that must be taken first. No two courses share a prerequisite. There is only one path through the program. + +Write a function that takes a list of (prerequisite, course) pairs, and returns the name of the course that the student will be taking when they are halfway through their program. (If a track has an even number of courses, and therefore has two "middle" courses, you should return the first one.) + +Sample input 1: (arbitrarily ordered) +prereqs_courses1 = [ + ["Data Structures", "Algorithms"], + ["Foundations of Computer Science", "Operating Systems"], + ["Computer Networks", "Computer Architecture"], + ["Algorithms", "Foundations of Computer Science"], + ["Computer Architecture", "Data Structures"], + ["Software Design", "Computer Networks"] +] + +//my notes ["PREREQ","NEXTREQ"] + +In this case, the order of the courses in the program is: + Software Design + Computer Networks + Computer Architecture + Data Structures + Algorithms + Foundations of Computer Science + Operating Systems + +Sample output 1: + "Data Structures" + + +Sample input 2: +prereqs_courses2 = [ + ["Data Structures", "Algorithms"], + ["Algorithms", "Foundations of Computer Science"], + ["Foundations of Computer Science", "Logic"] +] + + +Sample output 2: + "Algorithms" +Sample input 3: +prereqs_courses3 = [ + ["Data Structures", "Algorithms"], +] + + +Sample output 3: + "Data Structures" +*/ diff --git a/helpers/generateBoilerPlate.js b/helpers/generateBoilerPlate.js index 3d9a776..d2caa12 100644 --- a/helpers/generateBoilerPlate.js +++ b/helpers/generateBoilerPlate.js @@ -5,7 +5,7 @@ const fs = require('fs'); // TODO: split up challenges // TODO: add appDesignSection? // ////SETUP HERE////// -const solutionName = 'findArrayIntersect'; +const solutionName = 'determineCourseOrder'; // TODO: look into setting up map or enum for this /* challenge || dataStructure || algorithm || designPattern || concept */ const codeChallengeType = 'challenge';