From b7f9dcb0aa803f3ed6e72fb0b411f5d686fb210d Mon Sep 17 00:00:00 2001 From: Aleksandr Chernyshev Date: Wed, 26 Oct 2016 04:23:07 +0500 Subject: [PATCH 1/6] first --- robbery.js | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 166 insertions(+), 4 deletions(-) diff --git a/robbery.js b/robbery.js index 4a8309d..1c6c168 100644 --- a/robbery.js +++ b/robbery.js @@ -15,7 +15,139 @@ exports.isStar = true; * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - console.info(schedule, duration, workingHours); + // console.info(schedule, duration, workingHours); + + // var arrayOfTimeDanny = getArrayOfTimes(schedule.Danny); + + // var arrayOfTimeRusty = getArrayOfTimes(schedule.Rusty); + + // var arrayOfTimeLinus = getArrayOfTimes(schedule.Linus); + + // var bankStartWork = getIntOfTimeWithMainUTC(workingHours.from); + // var bankFinishWork = getIntOfTimeWithMainUTC(workingHours.to); + + // var arrayOfAllTime = arrayOfTimeDanny.concat(arrayOfTimeRusty).concat(arrayOfTimeLinus); + // arrayOfAllTime.push([0, bankStartWork], [bankFinishWork, bankStartWork + 1440], + // [bankFinishWork + 1440, bankStartWork + 1440 * 2], + // [bankFinishWork + 1440 * 2, 1440 * 3 - 1]); + + var arrayOfAllTime = getAllArray(); + + var sortAndMergeArray = mergeRange(arrayOfAllTime); + + var answerArray = getAnswer(sortAndMergeArray); + + var countOfTryLater = 0; + getCountOfTryLater(); + + function getAllArray() { + var arrayOfTimeDanny = getArrayOfTimes(schedule.Danny); + + var arrayOfTimeRusty = getArrayOfTimes(schedule.Rusty); + + var arrayOfTimeLinus = getArrayOfTimes(schedule.Linus); + var bankStartWork = getIntOfTimeWithMainUTC(workingHours.from); + var bankFinishWork = getIntOfTimeWithMainUTC(workingHours.to); + var array = arrayOfTimeDanny.concat(arrayOfTimeRusty).concat(arrayOfTimeLinus); + array.push([0, bankStartWork], [bankFinishWork, bankStartWork + 1440], + [bankFinishWork + 1440, bankStartWork + 1440 * 2], + [bankFinishWork + 1440 * 2, 1440 * 3 - 1]); + + return array; + } + + function getCountOfTryLater() { + var start; + for (var i = 0; i < answerArray.length; i++) { + start = answerArray[i][0]; + if (i === 0) { + start = answerArray[0][0] + 30; + } + if (start + duration <= answerArray[i][1]) { + countOfTryLater++; + start += 30; + } + } + } + + function getAnswer(arrayOfTime) { + + var start = arrayOfTime[0][1]; + var finish = 0; + var arrayOfGoodTime = []; + for (var i = 1; i < arrayOfTime.length; i++) { + finish = arrayOfTime[i][0]; + if (finish - start >= duration) { + arrayOfGoodTime.push([start, finish]); + } + start = arrayOfTime[i][1]; + } + + return arrayOfGoodTime; + } + + + function getArrayOfTimes(scheduleArray) { + var timeArray = []; + for (var i = 0; i < scheduleArray.length; i++) { + timeArray.push( + [getIntOfTimeWithMainUTC(scheduleArray[i].from), + getIntOfTimeWithMainUTC(scheduleArray[i].to) + ]); + } + + return timeArray; + } + + function mergeRange(array) { + var sortedArray = array.sort(function compareNumbers(a, b) { + return a[0] - b[0]; + }); + var resultArray = []; + var prevStart = sortedArray[0][0]; + + var prevFinish = sortedArray[0][1]; + for (var e = 1; e < sortedArray.length; e++) { + if (sortedArray[e][0] <= prevFinish) { + prevFinish = Math.max(sortedArray[e][1], prevFinish); + } else { + resultArray.push([prevStart, prevFinish]); + prevStart = sortedArray[e][0]; + prevFinish = sortedArray[e][1]; + } + } + resultArray.push([prevStart, prevFinish]); + + return resultArray; + + } + + function getUTC(stringTime) { + return Number(stringTime.substring(stringTime.length - 1)); + } + + function getIntOfTimeWithMainUTC(stringTime) { + var mainUTC = getUTC(workingHours.from); + if (stringTime.length === 7) { + var h = Number(stringTime.substring(0, 2)); + var m = Number(stringTime.substring(3, 5)); + + return m + 60 * h; + } + var day = 0; + if (stringTime.substring(0, 2) === 'ВТ') { + day = 1440; + } else if (stringTime.substring(0, 2) === 'СР') { + day = 2880; + } + var UTC = Number(stringTime.substring(stringTime.length - 1)); + var hours = Number(stringTime.substring(3, 5)); + var minuts = Number(stringTime.substring(6, 8)); + + return minuts + 60 * hours + 60 * (mainUTC - UTC) + day; + + } + return { @@ -24,7 +156,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return false; + return answerArray.length !== 0; }, /** @@ -35,7 +167,29 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - return template; + if (answerArray.length === 0) { + return ''; + } + var DD = Math.floor(answerArray[0][0] / 1440); + var HH = Math.floor((answerArray[0][0] - DD * 1440) / 60); + var MM = answerArray[0][0] - DD * 1440 - HH * 60; + + var day = 'ПН'; + if (DD === 1) { + day = 'ВТ'; + } else if (DD === 2) { + day = 'СР'; + } + if (MM < 10) { + MM = '0' + MM; + } + if (HH < 10) { + HH = '0' + HH; + } + + return template.replace(/%DD/, day) + .replace(/%HH/, HH) + .replace(/%MM/, MM); }, /** @@ -44,7 +198,15 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { - return false; + + if (countOfTryLater < 0) { + return false; + } + countOfTryLater--; + + return true; } }; + + }; From f8112ecce75b6ae4e7b0f7b72686d1edc06e6df9 Mon Sep 17 00:00:00 2001 From: Aleksandr Chernyshev Date: Wed, 26 Oct 2016 04:26:23 +0500 Subject: [PATCH 2/6] first isStar false --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 1c6c168..c7f52b0 100644 --- a/robbery.js +++ b/robbery.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализовано оба метода и tryLater */ -exports.isStar = true; +exports.isStar = false; /** * @param {Object} schedule – Расписание Банды From 5239dbb6f3fe0dec74394af5aeeaf50c439a7483 Mon Sep 17 00:00:00 2001 From: Aleksandr Chernyshev Date: Wed, 26 Oct 2016 23:36:53 +0500 Subject: [PATCH 3/6] add start --- robbery.js | 85 ++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 47 deletions(-) diff --git a/robbery.js b/robbery.js index c7f52b0..ba25457 100644 --- a/robbery.js +++ b/robbery.js @@ -4,7 +4,7 @@ * Сделано задание на звездочку * Реализовано оба метода и tryLater */ -exports.isStar = false; +exports.isStar = true; /** * @param {Object} schedule – Расписание Банды @@ -15,30 +15,38 @@ exports.isStar = false; * @returns {Object} */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - // console.info(schedule, duration, workingHours); - // var arrayOfTimeDanny = getArrayOfTimes(schedule.Danny); + var arrayOfAllTime = getAllArray(); - // var arrayOfTimeRusty = getArrayOfTimes(schedule.Rusty); + var sortAndMergeArray = mergeRange(arrayOfAllTime); - // var arrayOfTimeLinus = getArrayOfTimes(schedule.Linus); + var answerArray = getAnswerArray(sortAndMergeArray); - // var bankStartWork = getIntOfTimeWithMainUTC(workingHours.from); - // var bankFinishWork = getIntOfTimeWithMainUTC(workingHours.to); + var answer = answerArray.length === 0 ? -1 : answerArray[0][0]; - // var arrayOfAllTime = arrayOfTimeDanny.concat(arrayOfTimeRusty).concat(arrayOfTimeLinus); - // arrayOfAllTime.push([0, bankStartWork], [bankFinishWork, bankStartWork + 1440], - // [bankFinishWork + 1440, bankStartWork + 1440 * 2], - // [bankFinishWork + 1440 * 2, 1440 * 3 - 1]); + var arrayOfGoodTime = getArrOfGoodTime(); - var arrayOfAllTime = getAllArray(); - - var sortAndMergeArray = mergeRange(arrayOfAllTime); + var tryLaterCounter = 0; - var answerArray = getAnswer(sortAndMergeArray); + function getArrOfGoodTime() { + if (answer < 0) { + return []; + } + var resultArray = []; + var arr = answerArray; + var ans = answer; + for (var i = 0; i < arr.length; i ++) { + ans = arr[i][0]; + while (ans + duration <= arr[i][1]) { + resultArray.push(ans); + ans += 30; + } + } + resultArray.splice(0, 1); + // resultArray.push(-1); - var countOfTryLater = 0; - getCountOfTryLater(); + return resultArray; + } function getAllArray() { var arrayOfTimeDanny = getArrayOfTimes(schedule.Danny); @@ -56,34 +64,20 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { return array; } - function getCountOfTryLater() { - var start; - for (var i = 0; i < answerArray.length; i++) { - start = answerArray[i][0]; - if (i === 0) { - start = answerArray[0][0] + 30; - } - if (start + duration <= answerArray[i][1]) { - countOfTryLater++; - start += 30; - } - } - } - - function getAnswer(arrayOfTime) { + function getAnswerArray(arrayOfTime) { var start = arrayOfTime[0][1]; var finish = 0; - var arrayOfGoodTime = []; + var resArray = []; for (var i = 1; i < arrayOfTime.length; i++) { finish = arrayOfTime[i][0]; if (finish - start >= duration) { - arrayOfGoodTime.push([start, finish]); + resArray.push([start, finish]); } start = arrayOfTime[i][1]; } - return arrayOfGoodTime; + return resArray; } @@ -100,12 +94,11 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { } function mergeRange(array) { - var sortedArray = array.sort(function compareNumbers(a, b) { + var sortedArray = array.sort(function compareArrays(a, b) { return a[0] - b[0]; }); var resultArray = []; var prevStart = sortedArray[0][0]; - var prevFinish = sortedArray[0][1]; for (var e = 1; e < sortedArray.length; e++) { if (sortedArray[e][0] <= prevFinish) { @@ -156,7 +149,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ exists: function () { - return answerArray.length !== 0; + return answer >= 0; }, /** @@ -167,12 +160,12 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {String} */ format: function (template) { - if (answerArray.length === 0) { + if (!this.exists()) { return ''; } - var DD = Math.floor(answerArray[0][0] / 1440); - var HH = Math.floor((answerArray[0][0] - DD * 1440) / 60); - var MM = answerArray[0][0] - DD * 1440 - HH * 60; + var DD = Math.floor(answer / 1440); + var HH = Math.floor((answer - DD * 1440) / 60); + var MM = answer - DD * 1440 - HH * 60; var day = 'ПН'; if (DD === 1) { @@ -198,15 +191,13 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { * @returns {Boolean} */ tryLater: function () { - - if (countOfTryLater < 0) { + if (!this.exists() || arrayOfGoodTime.length === tryLaterCounter) { return false; } - countOfTryLater--; + answer = arrayOfGoodTime[tryLaterCounter]; + tryLaterCounter++; return true; } }; - - }; From 89f3b86113d6d61e9813de67fe584ed6d30042b5 Mon Sep 17 00:00:00 2001 From: Aleksandr Chernyshev Date: Sun, 30 Oct 2016 20:03:55 +0500 Subject: [PATCH 4/6] fix style --- robbery.js | 113 ++++++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 54 deletions(-) diff --git a/robbery.js b/robbery.js index ba25457..7bdda0d 100644 --- a/robbery.js +++ b/robbery.js @@ -16,11 +16,10 @@ exports.isStar = true; */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - var arrayOfAllTime = getAllArray(); + var minPerDay = 1400; + var minPerHour = 60; - var sortAndMergeArray = mergeRange(arrayOfAllTime); - - var answerArray = getAnswerArray(sortAndMergeArray); + var answerArray = getAnswerArray(); var answer = answerArray.length === 0 ? -1 : answerArray[0][0]; @@ -43,70 +42,79 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { } } resultArray.splice(0, 1); - // resultArray.push(-1); return resultArray; } - function getAllArray() { - var arrayOfTimeDanny = getArrayOfTimes(schedule.Danny); + function getBusyIntervals() { + + var arrIntervals = []; - var arrayOfTimeRusty = getArrayOfTimes(schedule.Rusty); + for (var friend in schedule) { + if ({}.hasOwnProperty.call(schedule, friend)) { + arrIntervals = arrIntervals.concat(getArrayOfBusyInterval(schedule[friend])); + } + } - var arrayOfTimeLinus = getArrayOfTimes(schedule.Linus); - var bankStartWork = getIntOfTimeWithMainUTC(workingHours.from); - var bankFinishWork = getIntOfTimeWithMainUTC(workingHours.to); - var array = arrayOfTimeDanny.concat(arrayOfTimeRusty).concat(arrayOfTimeLinus); - array.push([0, bankStartWork], [bankFinishWork, bankStartWork + 1440], - [bankFinishWork + 1440, bankStartWork + 1440 * 2], - [bankFinishWork + 1440 * 2, 1440 * 3 - 1]); + var bankStartWork = getTimestamp(workingHours.from); + var bankFinishWork = getTimestamp(workingHours.to); + arrIntervals.push([0, bankStartWork], [bankFinishWork, bankStartWork + minPerDay], + [bankFinishWork + minPerDay, bankStartWork + minPerDay * 2], + [bankFinishWork + minPerDay * 2, minPerDay * 3 - 1]); - return array; + return arrIntervals; } - function getAnswerArray(arrayOfTime) { + function getAnswerArray() { + var busyIntervals = getBusyIntervals(); + + var sortAndMergeArray = mergeRange(busyIntervals); - var start = arrayOfTime[0][1]; + var start = sortAndMergeArray[0][1]; var finish = 0; var resArray = []; - for (var i = 1; i < arrayOfTime.length; i++) { - finish = arrayOfTime[i][0]; + for (var i = 1; i < sortAndMergeArray.length; i++) { + finish = sortAndMergeArray[i][0]; if (finish - start >= duration) { resArray.push([start, finish]); } - start = arrayOfTime[i][1]; + start = sortAndMergeArray[i][1]; } return resArray; } - function getArrayOfTimes(scheduleArray) { + function getArrayOfBusyInterval(scheduleArray) { var timeArray = []; for (var i = 0; i < scheduleArray.length; i++) { timeArray.push( - [getIntOfTimeWithMainUTC(scheduleArray[i].from), - getIntOfTimeWithMainUTC(scheduleArray[i].to) + [getTimestamp(scheduleArray[i].from), + getTimestamp(scheduleArray[i].to) ]); } return timeArray; } - function mergeRange(array) { - var sortedArray = array.sort(function compareArrays(a, b) { + function sortArray(array) { + return array.sort(function compareArrays(a, b) { return a[0] - b[0]; }); + } + + function mergeRange(array) { + var sortedArray = sortArray(array); var resultArray = []; var prevStart = sortedArray[0][0]; var prevFinish = sortedArray[0][1]; - for (var e = 1; e < sortedArray.length; e++) { - if (sortedArray[e][0] <= prevFinish) { - prevFinish = Math.max(sortedArray[e][1], prevFinish); + for (var i = 1; i < sortedArray.length; i++) { + if (sortedArray[i][0] <= prevFinish) { + prevFinish = Math.max(sortedArray[i][1], prevFinish); } else { resultArray.push([prevStart, prevFinish]); - prevStart = sortedArray[e][0]; - prevFinish = sortedArray[e][1]; + prevStart = sortedArray[i][0]; + prevFinish = sortedArray[i][1]; } } resultArray.push([prevStart, prevFinish]); @@ -116,28 +124,28 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { } function getUTC(stringTime) { - return Number(stringTime.substring(stringTime.length - 1)); + return Number(stringTime.match(/[0-9]$/)); } - function getIntOfTimeWithMainUTC(stringTime) { + function getTimestamp(stringTime) { var mainUTC = getUTC(workingHours.from); if (stringTime.length === 7) { var h = Number(stringTime.substring(0, 2)); var m = Number(stringTime.substring(3, 5)); - return m + 60 * h; + return m + minPerHour * h; } var day = 0; if (stringTime.substring(0, 2) === 'ВТ') { - day = 1440; + day = minPerDay; } else if (stringTime.substring(0, 2) === 'СР') { - day = 2880; + day = minPerDay * 2; } var UTC = Number(stringTime.substring(stringTime.length - 1)); var hours = Number(stringTime.substring(3, 5)); var minuts = Number(stringTime.substring(6, 8)); - return minuts + 60 * hours + 60 * (mainUTC - UTC) + day; + return minuts + minPerHour * hours + minPerHour * (mainUTC - UTC) + day; } @@ -163,26 +171,23 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { if (!this.exists()) { return ''; } - var DD = Math.floor(answer / 1440); - var HH = Math.floor((answer - DD * 1440) / 60); - var MM = answer - DD * 1440 - HH * 60; - - var day = 'ПН'; - if (DD === 1) { - day = 'ВТ'; - } else if (DD === 2) { - day = 'СР'; - } - if (MM < 10) { - MM = '0' + MM; + var day = Math.floor(answer / minPerDay); + var hours = Math.floor((answer - day * minPerDay) / minPerHour); + var minutes = answer - day * minPerDay - hours * minPerHour; + + var days = ['ПН', 'ВТ', 'СР']; + var dayStr = days[day]; + + if (minutes < 10) { + minutes = '0' + minutes; } - if (HH < 10) { - HH = '0' + HH; + if (hours < 10) { + hours = '0' + hours; } - return template.replace(/%DD/, day) - .replace(/%HH/, HH) - .replace(/%MM/, MM); + return template.replace(/%DD/, dayStr) + .replace(/%HH/, hours) + .replace(/%MM/, minutes); }, /** From 7e19e9f6a40c2732db229bedb5b646179d545190 Mon Sep 17 00:00:00 2001 From: Aleksandr Chernyshev Date: Sun, 30 Oct 2016 20:07:34 +0500 Subject: [PATCH 5/6] fix style --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 7bdda0d..2c71c80 100644 --- a/robbery.js +++ b/robbery.js @@ -124,7 +124,7 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { } function getUTC(stringTime) { - return Number(stringTime.match(/[0-9]$/)); + return Number(stringTime.substring(stringTime.length - 1)); } function getTimestamp(stringTime) { From 61a7853750890307bb0cba870931da66e7a9769f Mon Sep 17 00:00:00 2001 From: Aleksandr Chernyshev Date: Sun, 30 Oct 2016 20:29:38 +0500 Subject: [PATCH 6/6] minPerDay fix --- robbery.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robbery.js b/robbery.js index 2c71c80..9c68805 100644 --- a/robbery.js +++ b/robbery.js @@ -16,7 +16,7 @@ exports.isStar = true; */ exports.getAppropriateMoment = function (schedule, duration, workingHours) { - var minPerDay = 1400; + var minPerDay = 1440; var minPerHour = 60; var answerArray = getAnswerArray();