Last active
May 10, 2021 21:09
-
-
Save neetigyachahar/55d79ac3bdbb124754353351f71a0d99 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Calculating intersection between two unsorted | |
// arrays with proper code writting guidelines | |
// Example | |
// Input: | |
// 2 1 10 11 12 13 14 4 5 67 7 | |
// 23 71 10 11 12 13 14 34 57 267 79 | |
// Output: | |
// Intersected Array: [ 10, 11, 12, 13, 14 ] | |
const _ = require("underscore"); // General utility-belt library | |
const utils = require("./utils"); // Basic custom utility functions | |
const variables = require('./variables'); // Hard coded strings and data | |
// Helper functions | |
const getArraysFromUser = async () => { | |
array1 = await utils.readConsoleInput(variables.ENTER_FIRST_ARRAY_QUESTION); | |
array2 = await utils.readConsoleInput(variables.ENTER_SECOND_ARRAY_QUESTION); | |
if (!array1.length || !array2.length) throw new Error(variables.EMPTY_INPUT_ERROR); | |
array1 = utils.parseArrayFromString(array1); | |
array2 = utils.parseArrayFromString(array2); | |
return [array1, array2]; | |
} | |
const calculateIntersection = (array1, array2) => { | |
try { | |
return _.intersection(array1, array2); | |
} catch (error) { | |
throw error; | |
} | |
} | |
// Driver code | |
const main = async () => { | |
let intersectedArray; | |
try { | |
// Reading arrays from console and destructuring them | |
[array1, array2] = await getArraysFromUser(); | |
// Calculating intersection | |
intersectedArray = calculateIntersection(array1, array2); | |
//Displaying result | |
console.log(variables.INTERSECTED_ARRAY_RESULT_MESSAGE, intersectedArray); | |
} catch (error) { | |
console.error(error); // or an error logger | |
} | |
} | |
// Run the driver function | |
main(); |
This file contains hidden or 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
// Calculating union between two unsorted | |
// arrays with proper code writting guidelines | |
// Example | |
// Input: | |
// 2 1 10 11 12 13 14 4 5 67 7 | |
// 23 71 10 11 12 13 14 34 57 267 79 | |
// Output: | |
// Union Array: [ 2, 1, 10, 11, 12, 13, 14, 4, 5, 67, 7, 23, 71, 34, 57, 267, 79 ] | |
const _ = require("underscore"); // General utility-belt library | |
const utils = require("./utils"); // Basic custom utility functions | |
const variables = require('./variables'); // Hard coded strings and data | |
// Helper functions | |
const getArraysFromUser = async () => { | |
array1 = await utils.readConsoleInput(variables.ENTER_FIRST_ARRAY_QUESTION); | |
array2 = await utils.readConsoleInput(variables.ENTER_SECOND_ARRAY_QUESTION); | |
if (!array1.length || !array2.length) throw new Error(variables.EMPTY_INPUT_ERROR); | |
array1 = utils.parseArrayFromString(array1); | |
array2 = utils.parseArrayFromString(array2); | |
return [array1, array2]; | |
} | |
const calculateUnion = (array1, array2) => { | |
try { | |
return _.union(array1, array2); | |
} catch (error) { | |
throw error; | |
} | |
} | |
// Driver code | |
const main = async () => { | |
let unionArray; | |
try { | |
// Reading arrays from console and destructuring them | |
[array1, array2] = await getArraysFromUser(); | |
// Calculating union | |
unionArray = calculateUnion(array1, array2); | |
//Displaying result | |
console.log(variables.UNION_ARRAY_RESULT_MESSAGE, unionArray); | |
} catch (error) { | |
console.error(error); // or an error logger | |
} | |
} | |
// Run the driver function | |
main(); |
This file contains hidden or 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
// Calculating overlaps between two slots | |
// with proper code writting guidelines | |
// Example | |
// Input: | |
// {{1, 2}, {2, 5}, {5, 6}, {3, 7}, {4,5}} | |
// Output: | |
// The slot: [2,5] overlaps with these upcoming slots: [[3,7],[4,5]] | |
// The slot: [3,7] overlaps with these upcoming slots: [[4,5],[5,6]] | |
const _ = require("underscore"); // General utility-belt library | |
const utils = require("./utils"); // Basic custom utility functions | |
const variables = require('./variables'); // Hard coded strings and data | |
// Helper functions | |
const parseSlotsStringToArray = slotsString => { | |
let slots; | |
let parsedSlots = []; | |
let numbersRegex = variables.NUMBERS_REGEX; | |
// Abstracts all the numbers in the string without changing the order | |
slots = slotsString.match(numbersRegex); | |
if (!slots.length || slots.length % 2 != 0) throw new Error(variables.INVALID_INPUT_ERROR); | |
// Grouping the array to times to subarray pairs to form an array of slots | |
for (let i = 0; i < slots.length; i += 2) { | |
let slot = [Number(slots[i]), Number(slots[i + 1])]; | |
if (slot.includes(NaN)) throw new Error(variables.INVALID_INPUT_ERROR); | |
parsedSlots.push(slot); | |
} | |
return parsedSlots; | |
} | |
const calculateOverlap = slots => { | |
let allSlotsOverlaps = []; | |
try { | |
// Sorting the slots based on their starting time | |
slots = slots.sort((slot1, slot2) => slot1[0] - slot2[0]); | |
// Omit checking last slot as there's no upcoming slot to it | |
for (let i = 0; i < slots.length - 1; i++) { | |
let slotOverlaps = []; | |
for (let j = i + 1; j < slots.length; j++) { | |
// For each slot, checking if an upcoming slot's starting time is | |
// before the ending time of current slot to confirm an overlap | |
if (slots[i][1] > slots[j][0]) slotOverlaps.push([...slots[j]]); | |
// Note: We don't break loop after an overlap is detected as we want all the overlaps | |
} | |
if (slotOverlaps.length) { | |
// Finally we push all the overlaps detected for a slot | |
allSlotsOverlaps.push({ | |
slot: slots[i], | |
slotOverlaps | |
}); | |
} | |
}; | |
return allSlotsOverlaps; | |
} catch (error) { | |
throw error; | |
} | |
} | |
// Driver code | |
const main = async () => { | |
let slots; | |
let allSlotsOverlaps; | |
try { | |
// Reading slots from console and parsing them | |
slots = await utils.readConsoleInput(variables.ENTER_SLOT_QUESTION); | |
if (!slots.length) throw new Error(variables.EMPTY_INPUT_ERROR); | |
slots = parseSlotsStringToArray(slots); | |
// Calculating overlap | |
allSlotsOverlaps = calculateOverlap(slots); | |
//Displaying result | |
allSlotsOverlaps.forEach( | |
slotOverlaps => console.log(variables.overlap_slots_result_message(slotOverlaps)) | |
); | |
} catch (error) { | |
console.error(error); // or an error logger | |
} | |
} | |
// Run the driver function | |
main(); |
This file contains hidden or 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
const readline = require('readline-promise').default; // Reads standard input from terminal | |
const variables = require('./variables'); // Hard coded strings and data | |
// Initial configurations | |
const input = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
terminal: true | |
}); | |
// Exported functions | |
const parseArrayFromString = arrayString => { | |
try { | |
let array; | |
// spliting array with spaces and typecasting each element of resultant array to Number | |
array = arrayString.trim().split(variables.SPACE); | |
array = array.map(arrayElement => Number(arrayElement)); | |
if (array.includes(NaN)) throw new Error(variables.ONLY_NUMBERS_ALLOWED_ERROR); | |
return array; | |
} catch (error) { | |
throw error; | |
} | |
} | |
const readConsoleInput = async inputQuestion => { | |
try { | |
let inputDataFromConsole; | |
inputDataFromConsole = await input.questionAsync(inputQuestion); | |
return inputDataFromConsole; | |
} catch (error) { | |
throw new Error(variables.STANDARD_INPUT_READ_FAIL_ERROR); //forwading the error with custom message | |
} | |
} | |
module.exports = { | |
parseArrayFromString, | |
readConsoleInput | |
} |
This file contains hidden or 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
// Tasks input questions | |
exports.ENTER_FIRST_ARRAY_QUESTION = "Enter the first array. (Space seperated numbers): "; | |
exports.ENTER_SECOND_ARRAY_QUESTION = "Enter the second array. (Space seperated numbers): "; | |
exports.ENTER_SLOT_QUESTION = "Enter the slots: "; | |
// Tasks output messages | |
exports.INTERSECTED_ARRAY_RESULT_MESSAGE = "Intersected Array: "; | |
exports.UNION_ARRAY_RESULT_MESSAGE = "Union Array: "; | |
exports.overlap_slots_result_message = slotOverlaps => | |
`The slot: ${JSON.stringify(slotOverlaps.slot)} overlaps with these upcoming slots: ${JSON.stringify(slotOverlaps.slotOverlaps)}`; | |
// Miscellaneous | |
exports.SPACE = " "; | |
exports.NUMBERS_REGEX = /([0-9]+)/g; | |
// Error messages | |
exports.EMPTY_INPUT_ERROR = "Empty input!"; | |
exports.INVALID_INPUT_ERROR = "Invalid input!"; | |
exports.ONLY_NUMBERS_ALLOWED_ERROR = "Invalid input! Only digits allowed!"; | |
exports.STANDARD_INPUT_READ_FAIL_ERROR = "Something broke while reading user input from console!"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment