diff --git a/challenges/bookingManagement/BookManager.js b/challenges/bookingManagement/BookManager.js new file mode 100644 index 0000000..543a366 --- /dev/null +++ b/challenges/bookingManagement/BookManager.js @@ -0,0 +1,61 @@ +class BookManager { + constructor() { + this.books = []; + } + + createBook(idArg, title) { + const id = parseInt(idArg, 10); + const foundBook = this.books.find((book) => book.id === id); + if (!foundBook) { + const book = {}; + book.id = id; + book.title = title; + this.books = [...this.books, book]; + return true; + } + return false; + } + + updateBook(idArg, title) { + const id = parseInt(idArg, 10); + const foundBook = this.books.find((book) => book.id === id); + if (!foundBook) { + return false; + } + const foundBookIndex = this.books.indexOf(foundBook); + + const book = {}; + book.id = id; + book.title = title; + + const currentBookList = this.books; + currentBookList.splice(foundBookIndex, 1, book); + this.books = [...currentBookList]; + return true; + } + + deleteBook(idArg) { + const id = parseInt(idArg, 10); + const foundBook = this.books.find((book) => book.id === id); + const foundBookIndex = this.books.indexOf(foundBook); + if (!foundBook) { + return false; + } + const newBookArray = this.books.splice(foundBookIndex, 1); + this.books = newBookArray; + return true; + } + + findBookById(idArg) { + const id = parseInt(idArg, 10); + const foundBook = this.books.find((book) => book.id === id); + return foundBook || null; + } + + findBookByTitle(title) { + const foundBook = this.books.find((book) => book.title === title); + return foundBook || null; + } +} + +module.exports = BookManager; diff --git a/challenges/bookingManagement/bookingManagement.test.js b/challenges/bookingManagement/bookingManagement.test.js new file mode 100644 index 0000000..9434c82 --- /dev/null +++ b/challenges/bookingManagement/bookingManagement.test.js @@ -0,0 +1,157 @@ +const BookManager = require('./BookManager'); + +// Do NOT edit the code below this comment. +// You should be able to complete this test without editing below this comment. + +function bookManagementRefactor(operations) { + const bookManager = new BookManager(); + // Calls corresponding methods of bookManager based on the input + return operations.map((operation) => { + const [methodName, ...params] = operation; + const result = bookManager[methodName].call(bookManager, ...params); + return (result === undefined || result === null) ? null : JSON.stringify(result); + }); +} + +describe('bookingManagement createBook Test', () => { + test('testOne', () => { + const bookManager = new BookManager(); + bookManager.createBook('10', 'Book_10'); + const result = bookManager.books; + const answer = [{ id: 10, title: 'Book_10' }]; + expect(result).toEqual(answer); + }); +}); + +describe('bookingManagement updateBook Test', () => { + test('testOne', () => { + const bookManager = new BookManager(); + bookManager.createBook('10', 'Book_10'); + bookManager.updateBook('10', 'Updated_Book_10'); + const result = bookManager.books; + const answer = [{ id: 10, title: 'Updated_Book_10' }]; + expect(result).toEqual(answer); + }); +}); + +describe('bookingManagement Test', () => { + test('testOne', () => { + const testOne = { + input: [['createBook', '10', 'Book_10'], + ['createBook', '10', 'Book_10'], + ['updateBook', '10', 'New_Book_10'], + ['deleteBook', '9'], + ['findBookById', '9'], + ['findBookById', '10'], + ['findBookByTitle', 'Book_10'], + ['findBookByTitle', 'New_Book_10']], + output: ['true', + 'false', + 'true', + 'false', + null, + '{"id":10,"title":"New_Book_10"}', + null, + '{"id":10,"title":"New_Book_10"}'], + }; + const result = bookManagementRefactor(testOne.input); + expect(result).toEqual(testOne.output); + }); + + test('testTwo', () => { + const testOne = { + input: [['createBook', '10', 'Book 10']], + output: ['true'], + }; + + const result = bookManagementRefactor(testOne.input); + expect(result).toEqual(testOne.output); + }); + + test('testThree', () => { + const testOne = { + input: [['updateBook', '462', 'Book 462']], + output: ['false'], + }; + + const result = bookManagementRefactor(testOne.input); + expect(result).toEqual(testOne.output); + }); + + + test('testFour', () => { + const testOne = { + input: [['createBook', '23', 'The Greeen Book'], + ['createBook', '23', 'The Red Book'], + ['createBook', '22', 'The Green Book'], + ['updateBook', '23', 'The Green Book'], + ['findBookById', '23'], + ['findBookById', '22'], + ['updateBook', '22', 'The Red Book'], + ['findBookByTitle', 'The Red Book'], + ['findBookByTitle', 'The Green Book']], + output: ['true', + 'false', + 'true', + 'true', + '{"id":23,"title":"The Green Book"}', + '{"id":22,"title":"The Green Book"}', + 'true', + '{"id":22,"title":"The Red Book"}', + '{"id":23,"title":"The Green Book"}'], + }; + + const result = bookManagementRefactor(testOne.input); + expect(result).toEqual(testOne.output); + }); + + test('testFive', () => { + const testOne = { + input: [['createBook', '733', 'Book 733'], + ['findBookById', '236']], + output: ['true', + null], + }; + + const result = bookManagementRefactor(testOne.input); + expect(result).toEqual(testOne.output); + }); + + test('testSix', () => { + const testOne = { + input: [['createBook', '733', 'Book 733'], + ['findBookById', '236']], + output: ['true', + null], + }; + + const result = bookManagementRefactor(testOne.input); + expect(result).toEqual(testOne.output); + }); + + test('testSeven', () => { + const testOne = { + input: [['createBook', '733', 'Book 733'], + ['findBookById', '733']], + output: ['true', + '{"id":733,"title":"Book 733"}'], + }; + + const result = bookManagementRefactor(testOne.input); + expect(result).toEqual(testOne.output); + }); + + test('testEight', () => { + const testOne = { + input: [['createBook', '269', 'Book 269'], + ['updateBook', '454', 'Book 454'], + ['findBookByTitle', 'Book 269']], + output: ['true', + 'false', + '{"id":269,"title":"Book 269"}'], + }; + + const result = bookManagementRefactor(testOne.input); + expect(result).toEqual(testOne.output); + }); +}); diff --git a/challenges/bookingManagement/bookingManagementNotes.md b/challenges/bookingManagement/bookingManagementNotes.md new file mode 100644 index 0000000..51387dd --- /dev/null +++ b/challenges/bookingManagement/bookingManagementNotes.md @@ -0,0 +1,124 @@ +bookingManagement Notes go here! + + +///////////////////// +function BookManager() { + this.books = []; + + this.createBook = function (id, title) { + // TODO: return false if the book id already exists + + id = parseInt(id); + let book = new Object(); + book.id = id; + book.title = title; + this.books = [...this.books, book]; + return true; + }; + + this.updateBook = function (id, title) { + // TODO: return false if the book doesn't exist + + id = parseInt(id); + this.books = this.books.filter(book => { + return { + id: id, + title: title + }; + }); + return true; + }; + + this.deleteBook = function (id) { + // TODO: return false if the book doesn't exist + + id = parseInt(id); + const book = this.books.find(book => book.id === id); + delete book; + return true; + }; + + this.findBookById = function (id) { + return this.books.find(book => book.id === parseInt(id)); + }; + + this.findBookByTitle = function (title) { + return this.books.find(book => book.title === title); + }; +} + +// Do NOT edit the code below this comment. +// You should be able to complete this test without editing below this comment. + +const bookManager = new BookManager(); + +function bookManagementRefactor(operations) { + // Calls corresponding methods of bookManager based on the input + return operations.map(operation => { + const [methodName, ...params] = operation; + let result = bookManager[methodName].call(bookManager, ...params); + return result === undefined ? "null" : JSON.stringify(result); + }); +} + + +//////////////FINAL +function BookManager() { + this.books = {}; + + this.createBook = function (id, title) { + idl = parseInt(id); /*book id*/ + let bookCreated = false + + if(!this.books.hasOwnProperty(idl)){ + let book = new Object(); + book.id = idl; + book.title = title; + this.books[idl] = book + bookCreated = true + } + return bookCreated + }; + + this.updateBook = function (id, title) { + id = parseInt(id); + let bookUpdated = false; + if(this.books[id]){ + this.books[id]['title'] = title + bookUpdated = true + } + return bookUpdated; + }; + + this.deleteBook = function (id) { + id = parseInt(id); + let bookDeleted = false + if(this.books[id]){ + delete this.books[id] + bookDeleted = true + } + return bookDeleted; + }; + + this.findBookById = function (id) { + return this.books[id] ? this.books[id] : null + }; + + this.findBookByTitle = function (title) { + return Object.values(this.books).find(book => book.title === title); + }; +} + +// Do NOT edit the code below this comment. +// You should be able to complete this test without editing below this comment. + +const bookManager = new BookManager(); + +function bookManagementRefactor(operations) { + // Calls corresponding methods of bookManager based on the input + return operations.map(operation => { + const [methodName, ...params] = operation; + let result = bookManager[methodName].call(bookManager, ...params); + return result === undefined ? "null" : JSON.stringify(result); + }); +} diff --git a/challenges/bookingManagement/bookingManagementSpec.md b/challenges/bookingManagement/bookingManagementSpec.md new file mode 100644 index 0000000..5af8eb3 --- /dev/null +++ b/challenges/bookingManagement/bookingManagementSpec.md @@ -0,0 +1,45 @@ +bookingManagement Spec go here! + +You are given a class that handles operations with books, however it is not complete and might have some issues, your task is to refactor it, fix the issues and add the missing parts. + +Each book is described by 2 fields: id and title. + +The methods that should be supported are listed below: + +createBook(id, title) - creates new book. Returns false if the book with such id already exists, and true otherwise; +updateBook(id, title) - updates provided book. Returns false if the book with such id does not exist, and true otherwise. +deleteBook(id) - deletes provided book. Returns false if the book with such id does not exist, and true otherwise. +findBookById(id) - find book by id. Returns the book, if the book with such id exists, and null otherwise. +findBookByTitle(title) - find book by title. Returns the book, if the book with such title exists, and null otherwise. It is guaranteed that at any time there are no two books with the same title. +Implement all these methods. + +Example + +For operations = [["createBook", "10", "Book_10"], ["createBook", "10", "Book_10"], ["updateBook", "10", "New_Book_10"], ["deleteBook", "9"],["findBookById", "9"], ["findBookById", "10"], ["findBookByTitle", "Book_10"], ["findBookByTitle", "New_Book_10"]], the output should be +bookManagementRefactor(operations) = ["true", "false", "true", "false", "null", "{"id":10,"title":"New_Book_10"}", "null", "{"id":10,"title":"New_Book_10"}"]. + +//////////////////////////NOTHING IMPORTANT + +Input/Output + +[execution time limit] 4 seconds (js) + +[input] array.array.string operations + +An array of operations desribed above. + +Guaranteed constraints: +1 ≤ operations.length ≤ 103, +2 ≤ operations[i].length ≤ 3. + +[output] array.string + +An array with ith element equal to the result of the ith operation. +[JavaScript (ES6)] Syntax Tips + +// Prints help message to the console +// Returns a string +function helloWorld(name) { + console.log("This prints to the console when you Run Tests"); + return "Hello, " + name; +} diff --git a/challenges/dataWork/dataWork.js b/challenges/dataWork/dataWork.js new file mode 100644 index 0000000..32f1cae --- /dev/null +++ b/challenges/dataWork/dataWork.js @@ -0,0 +1,5 @@ +function dataWork(input) { + +} + +module.exports = dataWork; diff --git a/challenges/dataWork/dataWork.test.js b/challenges/dataWork/dataWork.test.js new file mode 100644 index 0000000..e74cbf0 --- /dev/null +++ b/challenges/dataWork/dataWork.test.js @@ -0,0 +1,13 @@ +const dataWork = require('./dataWork'); + +const testOne = { + input: '', + output: '', +}; + +describe('dataWork Test', () => { + test('testOne', () => { + const result = dataWork(testOne.input); + expect(result).toEqual(testOne.output); + }); +}); diff --git a/challenges/dataWork/dataWorkNotes.md b/challenges/dataWork/dataWorkNotes.md new file mode 100644 index 0000000..af31013 --- /dev/null +++ b/challenges/dataWork/dataWorkNotes.md @@ -0,0 +1,143 @@ +dataWork Notes go here! +//////// +dataMeans +( +0:00:53 +) +Codewriting + +You are given the files data.json and data.csv in the /root/devops/ directory. + +data.json contains a JSON array of objects with the following format: + +{ + "id": string, + "id2": string, + "key": string, + "created": number, // UNIX timestamp (UTC) in milliseconds + "district": string +} +while data.csv has the following columns + +id,sla,overdue,x,y,type +string,number,number,number,number,number +where id corresponds to id in data.json. + +Find out which day of the week has the lowest mean overdue value, and output it to the console. + +Next, determine which district's mean location (x and y coordinates) is closest to the mean location of all data, ignoring data from district Unknown. Print this district's name on the next line. + +Example + +For the following data.json + +[ + {"id": "1", "id2": "01", "key": "A", "created": 1518066000000, "district": "A"}, + {"id": "2", "id2": "02", "key": "B", "created": 1518066010000, "district": "Z"}, + {"id": "3", "id2": "03", "key": "C", "created": 1358517124000, "district": "C"}, + {"id": "4", "id2": "15", "key": "D", "created": 1358517113000, "district": "A"}, + {"id": "0", "id2": "10", "key": "D", "created": 1358517114000, "district": "Z"}, + {"id": "7", "id2": "20", "key": "Z", "created": 1517793152000, "district": "F"} +] +and data.csv + +id,sla,overdue,x,y,type +4,10.0,-8.98,3105443.57,13832598.28,3.0 +1,14.0,-7.79,3047695.62,13826869.96,3.0 +3,10.0,-8.59,3078170.34,13820368.28,3.0 +2,10.0,-8.98,3105443.57,13832598.28,3.0 +7,14.0,-7.79,3047695.62,13826869.96,3.0 +0,10.0,-8.59,3078170.34,13820368.28,3.0 +the output for this should be: + +Friday +C +[execution time limit] 25 seconds (js) +JavaScript (ES6) +12 +console.log('Hello world'); + +TESTS +Test 1 +Input: +View the setup script +Expected Output: +Friday +C +Click the "Run Tests" button to see output and console logs. +Test 2 +Test 3 +Test 4 +Test 5 +Test 6 +Formatting +Editor Mode +VS Code +Theme +Dark +Tab Size +Auto +Font Size +14px +Auto-brackets +Minimap +Code Completion +Error Highlighting +Hotkeys +CTRL/CMD + Enter +Submit +CTRL/CMD + R +Run +CTRL/CMD + S +Save +0/300 + +resetTrack + +You are given the files data.json and data.csv in the /root/devops/ directory. + +data.json contains a JSON array of objects with the following format: + +{ + "id": string, + "id2": string, + "key": string, + "created": number, // UNIX timestamp (UTC) in milliseconds + "district": string +} +while data.csv has the following columns + +id,sla,overdue,x,y,type +string,number,number,number,number,number +where id corresponds to id in data.json. + +Find out which day of the week has the lowest mean overdue value, and output it to the console. + +Next, determine which district's mean location (x and y coordinates) is closest to the mean location of all data, ignoring data from district Unknown. Print this district's name on the next line. + +Example + +For the following data.json + +[ + {"id": "1", "id2": "01", "key": "A", "created": 1518066000000, "district": "A"}, + {"id": "2", "id2": "02", "key": "B", "created": 1518066010000, "district": "Z"}, + {"id": "3", "id2": "03", "key": "C", "created": 1358517124000, "district": "C"}, + {"id": "4", "id2": "15", "key": "D", "created": 1358517113000, "district": "A"}, + {"id": "0", "id2": "10", "key": "D", "created": 1358517114000, "district": "Z"}, + {"id": "7", "id2": "20", "key": "Z", "created": 1517793152000, "district": "F"} +] +and data.csv + +id,sla,overdue,x,y,type +4,10.0,-8.98,3105443.57,13832598.28,3.0 +1,14.0,-7.79,3047695.62,13826869.96,3.0 +3,10.0,-8.59,3078170.34,13820368.28,3.0 +2,10.0,-8.98,3105443.57,13832598.28,3.0 +7,14.0,-7.79,3047695.62,13826869.96,3.0 +0,10.0,-8.59,3078170.34,13820368.28,3.0 +the output for this should be: + +Friday +C +[execution time limit] 25 seconds (js) diff --git a/challenges/dataWork/dataWorkSpec.md b/challenges/dataWork/dataWorkSpec.md new file mode 100644 index 0000000..9101a8d --- /dev/null +++ b/challenges/dataWork/dataWorkSpec.md @@ -0,0 +1 @@ +dataWork Spec goes here! \ No newline at end of file diff --git a/challenges/playlistRefactor/playlistRefactor.js b/challenges/playlistRefactor/playlistRefactor.js new file mode 100644 index 0000000..5f1abd7 --- /dev/null +++ b/challenges/playlistRefactor/playlistRefactor.js @@ -0,0 +1,129 @@ +function playlistRefactor() { + +} + +class Track { + constructor(name, duration) { + this.name = name; + this.duration = duration; + this.pausedOn = 0; + } + + play(time) { + console.log('PLAY CALLED FOR TIME'); + console.log(time); + // TODO: implement this method + // console.log('this.pausedOn') + // console.log(this.pausedOn) + // console.log('time') + // console.log(time) + if ((this.pausedOn + time) < this.duration) { + this.pausedOn += time; + } else { + this.pausedOn = ((this.pausedOn + time) - this.duration); + } + // console.log('AFTER this.pausedOn') + // console.log(this.pausedOn) + } + + reset() { + this.pausedOn = 0; + } + + toString() { + return `Track(name = ${this.name}, duration = ${this.duration}, pausedOn = ${this.pausedOn})`; + } +} + +class Playlist { + constructor() { + this.trackList = []; + } + + addTrack(name, duration) { + const newTrack = new Track(name, duration); + this.trackList.push(newTrack); + } + + deleteTrack(name) { + this.trackList = this.trackList.filter((track) => track.name !== name); + } + + playTrack(name, time) { + this.trackList.forEach((track, i) => { + if (track.name === name) { + track.play(time); + } + }); + } + + resetTrack(name) { + // TODO: implement this method + if (name) { + this.trackList.forEach((track, i) => { + if (track.name === name) { + this.trackList[i].reset(); + } + }); + } else { + this.trackList.forEach((track, i) => { + track.reset(); + }); + } + } + + trackIndexByName(name) { + // TODO: implement this method + return findWithAttr(this.trackList, 'name', name); + } + + moveTrack(name, toIndex) { + const index = this.trackIndexByName(name); + if (index === toIndex) { + return; + } + this.trackList.splice(toIndex, 0, this.trackList[index]); + } + + toString() { + return `[${this.trackList.map((track) => track.toString()).join(', ')}]`; + } +} + +function findWithAttr(array, attr, value) { + for (let i = 0; i < array.length; i += 1) { + if (array[i][attr] === value) { + return i; + } + } + return -1; +} + +function playlistRefactoring(commands, names, parameters) { + const playlist = new Playlist(); + const result = []; + + for (let i = 0; i < commands.length; i++) { + if (commands[i] === 'add') { + playlist.addTrack(names[i], parameters[i]); + } else if (commands[i] === 'delete') { + playlist.deleteTrack(names[i]); + } else if (commands[i] === 'play') { + playlist.playTrack(names[i], parameters[i]); + } else if (commands[i] === 'reset') { + if (names[i] === '') { + playlist.resetTrack(); + } else { + playlist.resetTrack(names[i]); + } + } else if (commands[i] === 'move') { + playlist.moveTrack(names[i], parameters[i]); + } else { // commands[i] === 'get' + result.push(playlist.toString()); + } + } + + return result; +} + +module.exports = playlistRefactor; diff --git a/challenges/playlistRefactor/playlistRefactor.test.js b/challenges/playlistRefactor/playlistRefactor.test.js new file mode 100644 index 0000000..0073d76 --- /dev/null +++ b/challenges/playlistRefactor/playlistRefactor.test.js @@ -0,0 +1,107 @@ +const playlistRefactor = require('./playlistRefactor'); + +describe('playlistRefactor Test', () => { + test('testOne', () => { + const testOne = { + commands: + ['add', + 'play', + 'get', + 'add', + 'play', + 'play', + 'get', + 'play', + 'get'], + names: + ['I Still Miss Someone', + 'I Still Miss Someone', + '', + 'Toxicity', + 'I Still Miss Someone', + 'I Still Miss Someone', + '', + 'I Still Miss Someone', + ''], + parameters: [1, 751, 0, 584, 1, 824, 0, 220, 0], + Output: + ['[Track(name = I Still Miss Someone, duration = 1, pausedOn = 750)]', + '[Track(name = I Still Miss Someone, duration = 1, pausedOn = 1573), Track(name = Toxicity, duration = 584, pausedOn = 0)]', + '[Track(name = I Still Miss Someone, duration = 1, pausedOn = 1792), Track(name = Toxicity, duration = 584, pausedOn = 0)]'], + ExpectedOutput: + ['[Track(name = I Still Miss Someone, duration = 1, pausedOn = 0)]', + '[Track(name = I Still Miss Someone, duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 584, pausedOn = 0)]', + '[Track(name = I Still Miss Someone, duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 584, pausedOn = 0)]'], + ConsoleOutput: [ + 'PLAY CALLED FOR TIME', + '751', + 'PLAY CALLED FOR TIME', + '1', + 'PLAY CALLED FOR TIME', + '824', + 'PLAY CALLED FOR TIME', + '220', + ], + }; + const result = playlistRefactor(testOne.input); + expect(result).toEqual(testOne.output); + }); + + test('testOne', () => { + const testOne = { + commands: + ['add', + 'add', + 'add', + 'add', + 'get', + 'move', + 'get', + 'move', + 'get', + 'add', + 'add', + 'get', + 'move', + 'get', + 'move', + 'get'], + names: + ['"Long Gone(From the Bowlin Green)"', + 'Symphony #9 in D minor("Choral"),Op. 125', + 'I Walk the Line', + 'Jackie Cane', + '', + 'Symphony #9 in D minor("Choral"),Op. 125', + '', + 'I Walk the Line', + '', + 'OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo', + 'Toxicity', + '', + 'Toxicity', + '', + 'Toxicity', + ''], + parameters: [1, 509, 1, 568, 0, 0, 0, 2, 0, 1, 1000, 0, 3, 0, 2, 0], + Output: + ['[Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0)]', + '[Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0)]', + '[Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0)]', + '[Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0), Track(name = OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo, duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 1000, pausedOn = 0)]', + '[Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 1000, pausedOn = 0), Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0), Track(name = OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo, duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 1000, pausedOn = 0)]', + '[Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 1000, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 1000, pausedOn = 0), Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0), Track(name = OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo, duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 1000, pausedOn = 0)]'], + ExpectedOutput: + ['[Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0)]', + '[Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0)]', + '[Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0)]', + '[Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0), Track(name = OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo, duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 1000, pausedOn = 0)]', + '[Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 1000, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0), Track(name = OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo, duration = 1, pausedOn = 0)]', + '[Track(name = Symphony #9 in D minor("Choral"),Op. 125, duration = 509, pausedOn = 0), Track(name = "Long Gone(From the Bowlin Green)", duration = 1, pausedOn = 0), Track(name = Toxicity, duration = 1000, pausedOn = 0), Track(name = I Walk the Line, duration = 1, pausedOn = 0), Track(name = Jackie Cane, duration = 568, pausedOn = 0), Track(name = OoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOo, duration = 1, pausedOn = 0)]'], + ConsoleOutput: /* Empty */ + [], + }; + const result = playlistRefactor(testOne.input); + expect(result).toEqual(testOne.output); + }); +}); diff --git a/challenges/playlistRefactor/playlistRefactorNotes.md b/challenges/playlistRefactor/playlistRefactorNotes.md new file mode 100644 index 0000000..cdb358b --- /dev/null +++ b/challenges/playlistRefactor/playlistRefactorNotes.md @@ -0,0 +1,334 @@ +playlistRefactor Notes go here! + +//////////// +class Track { + constructor(name, duration) { + this.name = name; + this.duration = duration; + this.pausedOn = 0; + } + + play(time) { + // TODO: implement this method + } + + reset() { + // TODO: implement this method + } + + toString() { + return `Track(name = ${this.name}, duration = ${this.duration}, pausedOn = ${this.pausedOn})`; + } +} + +class Playlist { + constructor() { + this.trackList = []; + } + + addTrack(name, duration) { + // TODO: implement this method + } + + deleteTrack(name) { + // TODO: implement this method + } + + playTrack(name, time) { + // TODO: implement this method + } + + resetTrack(name) { + // TODO: implement this method + } + + trackIndexByName(name) { + // TODO: implement this method + return -1; + } + + moveTrack(name, toIndex) { + const index = this.trackIndexByName(name); + if (index === toIndex) { + return; + } + // TODO: complete the implementation of this method + } + + toString() { + return '[' + this.trackList.map(track => track.toString()).join(', ') + ']'; + } +} + +function playlistRefactoring(commands, names, parameters) { + const playlist = new Playlist(); + const result = []; + + for (let i = 0; i < commands.length; i++) { + if (commands[i] === 'add') { + playlist.addTrack(names[i], parameters[i]); + } else if (commands[i] === 'delete') { + playlist.deleteTrack(names[i]); + } else if (commands[i] === 'play') { + playlist.playTrack(names[i], parameters[i]); + } else if (commands[i] === 'reset') { + if (names[i] === '') { + playlist.resetTrack(); + } else { + playlist.resetTrack(names[i]); + } + } else if (commands[i] === 'move') { + playlist.moveTrack(names[i], parameters[i]); + } else { // commands[i] === 'get' + result.push(playlist.toString()); + } + } + + return result; +} + + +//////////////////////////// +class Track { + constructor(name, duration) { + this.name = name; + this.duration = duration; + this.pausedOn = 0; + } + + play(time) { + console.log('PLAY CALLED FOR TIME') + console.log(time) + // TODO: implement this method + // console.log('this.pausedOn') + // console.log(this.pausedOn) + // console.log('time') + // console.log(time) + if((this.pausedOn+time)<=this.duration){ + this.pausedOn+=time + } else { + this.pausedOn = ((this.pausedOn+time)-this.duration) + } + // console.log('AFTER this.pausedOn') + // console.log(this.pausedOn) + } + + reset() { + this.pausedOn = 0 + } + + toString() { + return `Track(name = ${this.name}, duration = ${this.duration}, pausedOn = ${this.pausedOn})`; + } +} + +class Playlist { + constructor() { + this.trackList = []; + } + + addTrack(name, duration) { + let newTrack = new Track(name,duration); + this.trackList.push(newTrack) + } + + deleteTrack(name) { + this.trackList = this.trackList.filter((track)=>{ + return track.name !== name + }) + } + + playTrack(name, time) { + // TODO: implement this method + this.trackList.forEach((track,i)=>{ + if(track.name === name){ + this.trackList[i].play(time) + } + }) + } + + resetTrack(name) { + // TODO: implement this method + if(name){ + this.trackList.forEach((track,i)=>{ + if(track.name === name){ + this.trackList[i].reset() + } + }) + } else {this.trackList.forEach((track,i)=>{ + track.reset() + }) + } + } + + trackIndexByName(name) { + // TODO: implement this method + return -1; + } + + moveTrack(name, toIndex) { + const index = this.trackIndexByName(name); + if (index === toIndex) { + return; + } + // TODO: complete the implementation of this method + } + + toString() { + return '[' + this.trackList.map(track => track.toString()).join(', ') + ']'; + } +} + +function playlistRefactoring(commands, names, parameters) { + const playlist = new Playlist(); + const result = []; + + for (let i = 0; i < commands.length; i++) { + if (commands[i] === 'add') { + playlist.addTrack(names[i], parameters[i]); + } else if (commands[i] === 'delete') { + playlist.deleteTrack(names[i]); + } else if (commands[i] === 'play') { + playlist.playTrack(names[i], parameters[i]); + } else if (commands[i] === 'reset') { + if (names[i] === '') { + playlist.resetTrack(); + } else { + playlist.resetTrack(names[i]); + } + } else if (commands[i] === 'move') { + playlist.moveTrack(names[i], parameters[i]); + } else { // commands[i] === 'get' + result.push(playlist.toString()); + } + } + + return result; +} + + +//////////////////////// +class Track { + constructor(name, duration) { + this.name = name; + this.duration = duration; + this.pausedOn = 0; + } + + play(time) { + console.log('PLAY CALLED FOR TIME') + console.log(time) + // TODO: implement this method + // console.log('this.pausedOn') + // console.log(this.pausedOn) + // console.log('time') + // console.log(time) + if((this.pausedOn+time){ + return track.name !== name + }) + } + + playTrack(name, time) { + this.trackList.forEach((track,i)=>{ + if(track.name === name){ + track.play(time) + } + }) + } + + resetTrack(name) { + // TODO: implement this method + if(name){ + this.trackList.forEach((track,i)=>{ + if(track.name === name){ + this.trackList[i].reset() + } + }) + } else {this.trackList.forEach((track,i)=>{ + track.reset() + }) + } + } + + trackIndexByName(name) { + // TODO: implement this method + return findWithAttr(this.trackList,'name',name) + } + + moveTrack(name, toIndex) { + const index = this.trackIndexByName(name); + if (index === toIndex) { + return; + } + this.trackList.splice(toIndex, 0, this.trackList[index]) + } + + toString() { + return '[' + this.trackList.map(track => track.toString()).join(', ') + ']'; + } +} + +function findWithAttr(array, attr, value) { + for(var i = 0; i < array.length; i += 1) { + if(array[i][attr] === value) { + return i; + } + } + return -1; +} + +function playlistRefactoring(commands, names, parameters) { + const playlist = new Playlist(); + const result = []; + + for (let i = 0; i < commands.length; i++) { + if (commands[i] === 'add') { + playlist.addTrack(names[i], parameters[i]); + } else if (commands[i] === 'delete') { + playlist.deleteTrack(names[i]); + } else if (commands[i] === 'play') { + playlist.playTrack(names[i], parameters[i]); + } else if (commands[i] === 'reset') { + if (names[i] === '') { + playlist.resetTrack(); + } else { + playlist.resetTrack(names[i]); + } + } else if (commands[i] === 'move') { + playlist.moveTrack(names[i], parameters[i]); + } else { // commands[i] === 'get' + result.push(playlist.toString()); + } + } + + return result; +} diff --git a/challenges/playlistRefactor/playlistRefactorSpec.md b/challenges/playlistRefactor/playlistRefactorSpec.md new file mode 100644 index 0000000..f8c09f3 --- /dev/null +++ b/challenges/playlistRefactor/playlistRefactorSpec.md @@ -0,0 +1,73 @@ +playlistRefactor Spec go here! + +You are given the structure of a class that handles basic music playlist operations. Each playlist consists of several tracks. Your task is to complete the implementation of the missing methods. + +The class Track has 3 properties: + +name - the name of the track represented as a non-empty string consisting of no more then 100 ASCII-characters. +duration - an integer representing the duration of the track, in seconds. +pausedOn - the second on which the track was paused the last time it was played. +You must complete the implementation of the following methods: + +play(time) - plays the track starting from moment pausedOn for time seconds. If the track ends before time seconds are left, it starts over. +reset() - resets the track (sets pausedOn to 0). +The main class Playlist contains only the property trackList - the list of tracks in the playlist. Note that the order of the tracks in the playlist is important. + +You must complete the implementation of the following methods: + +addTrack(name, duration) - adds a track with the given name and duration to the end of the playlist. It is guaranteed that there is no track with the given name when this method is called. +deleteTrack(name) - deletes the track with the given name. It is guaranteed that there is exactly 1 track with the given name in the playlist when this method is called. +playTrack(name, time) - plays the track with the given name for the given number of seconds. It is guaranteed that there is exactly 1 track with the given name in the playlist when this method is called. +resetTrack(name) - resets the track with the given name. If no name is given, this method resets all the tracks in the playlist. It is guaranteed that there is exactly 1 track with the given name (if a name is given) in the playlist when this method is called. +moveTrack(name, toIndex) - moves the track with the given name to the given index. Does not change the order of the other tracks in the playlist. It is guaranteed that there is exactly 1 track with the given name in the playlist when this method is called. +Please do not edit the rest of the existing methods. However, feel free to add any other new methods to the classes if they will help you to solve the problem + +Example + +For commands = ["add", "play", "get", "add", "get"], names = ["Clair de Lune", "Clair de Lune", "", "Toxicity", ""], and parameters = [303, 603, 0, 283, 0], the output should be + +playlistRefactoring(commands, names, parameters) = [ + "[Track(name = Clair de Lune, duration = 303, pausedOn = 300)]", + "[Track(name = Clair de Lune, duration = 303, pausedOn = 300), Track(name = Toxicity, duration = 283, pausedOn = 0)]" +] +Input/Output + +[execution time limit] 4 seconds (js) + +[input] array.string commands + +commands[i] is one of the following strings: + +add; +delete; +play; +reset; +move; +get. +Guaranteed constraints: +4 ≤ commands.length ≤ 100. + +[input] array.string names + +names[i] can have length 0 only for reset and get commands, in all other cases the name of the track is guaranteed to be a non-empty string. + +Guaranteed constraints: +names.length = commands.length, +0 ≤ names[i].length ≤ 100. + +[input] array.integer parameters + +Guaranteed constraints: +parameters.length = commands.length, +0 ≤ parameters[i] ≤ 1000. + +[output] array.string + +[JavaScript (ES6)] Syntax Tips + +// Prints help message to the console +// Returns a string +function helloWorld(name) { + console.log("This prints to the console when you Run Tests"); + return "Hello, " + name; +} diff --git a/helpers/generateBoilerPlate.js b/helpers/generateBoilerPlate.js index 3d9a776..5405764 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 = 'dataWork'; // TODO: look into setting up map or enum for this /* challenge || dataStructure || algorithm || designPattern || concept */ const codeChallengeType = 'challenge';