-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Козлова Валерия #57
base: master
Are you sure you want to change the base?
Козлова Валерия #57
Changes from 30 commits
99678f0
e5d2f85
2b51aa0
3061156
b10d369
af12ee9
79354b5
d4f8f54
fdea8b6
332cf19
146978c
51d9e5e
a753198
22966d1
4ef3f09
a6ffd79
0784f5d
fb0caa2
db69cec
d019062
a53fafd
a4a8844
4faba5e
123f6c2
8d4a634
f06c1c8
18aaff8
42465df
2df9932
1712fe0
9a9bfed
5f83296
5d40043
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,189 @@ | |
* Сделано задание на звездочку | ||
* Реализовано оба метода и tryLater | ||
*/ | ||
exports.isStar = true; | ||
exports.isStar = false; | ||
|
||
var WEEK = ['', 'ПН', 'ВТ', 'СР', 'ЧТ', 'ПТ', 'СБ', 'ВС']; | ||
var YEAR = 2016; | ||
var MONTH = 9; | ||
var BEGIN_OF_WEEK = new Date (YEAR, MONTH, 1, 0, 0); | ||
var END_OF_WEEK = new Date (YEAR, MONTH, 7, 23, 59); | ||
|
||
function toTypeDate(date, bankTimeZone) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Название функции должно начинаться с глагола. Так оно ответит на вопрос, что функция делает |
||
var dayOfWeek = date.slice(0, 2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше бы все эти даты строковые в объекты перегонять функцией-хелпером, чтобы потом не |
||
var currentDay = WEEK.indexOf(dayOfWeek); | ||
var hours = parseInt (date.slice(3, 5), 10) - parseInt (date.slice(8), 10) + bankTimeZone; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше |
||
var minutes = parseInt (date.slice(6, 8), 10); | ||
|
||
return new Date (YEAR, MONTH, currentDay, hours, minutes); | ||
} | ||
|
||
function toNewSchedule(schedule, bankTimeZone) { | ||
var newSchedule = {}; | ||
var keys = Object.keys(schedule); | ||
keys.forEach(function (key) { | ||
var n = (schedule[key]).length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Название не говорит о том, что лежит в этой переменной |
||
if (n === 0) { | ||
newSchedule[key] = [{ | ||
from: BEGIN_OF_WEEK, | ||
to: BEGIN_OF_WEEK | ||
}]; | ||
} else { | ||
newSchedule[key] = []; | ||
for (var i = 0; i < n; i++) { | ||
newSchedule[key].push({ | ||
from: toTypeDate (schedule[key][i].from, bankTimeZone), | ||
to: toTypeDate (schedule[key][i].to, bankTimeZone) | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
return newSchedule; | ||
} | ||
|
||
// minOrMax = 1 если нужно найти максимум и | ||
// -1 если нужно найти минимум | ||
function compareDate(minOrMax) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ой, а не проще ли вместо этой функции использовать There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. И функция возвращает не результат сравнения (обычно компараторы возвращают 1, 0 или -1), а саму дату. Название должно соответствовать |
||
var dates = [].slice.call(arguments, 1); | ||
dates.sort (function (date1, date2) { | ||
return (date2 - date1) * minOrMax; | ||
}); | ||
|
||
return dates[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. и тут давай сделаем |
||
} | ||
|
||
function toFreeSchedule(schedule) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Наверное, здесь функция должна называться |
||
var freeSchedule = {}; | ||
var keys = Object.keys(schedule); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. зачем Object.keys(schedule).forEach(/* ... */); |
||
keys.forEach(function (key) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
freeSchedule[key] = []; | ||
var n = (schedule[key]).length; | ||
freeSchedule[key][0] = { | ||
from: BEGIN_OF_WEEK, | ||
to: schedule[key][0].from | ||
}; | ||
for (var i = 1; i < n; i++) { | ||
freeSchedule[key][i] = { | ||
from: schedule[key][i - 1].to, | ||
to: schedule[key][i].from | ||
}; | ||
} | ||
freeSchedule[key][n] = { | ||
from: schedule[key][n - 1].to, | ||
to: END_OF_WEEK | ||
}; | ||
freeSchedule[key].len = n + 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Зачем?) |
||
}); | ||
|
||
return freeSchedule; | ||
} | ||
|
||
function findGangFreeTime(schedule) { | ||
var freeTime = []; | ||
var begin; | ||
var end; | ||
schedule.Danny.forEach(function (i) { | ||
schedule.Rusty.forEach(function (j) { | ||
schedule.Linus.forEach(function (k) { | ||
begin = compareDate (1, i.from, j.from, k.from); | ||
end = compareDate (-1, i.to, j.to, k.to); | ||
if (end > begin) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
freeTime.push({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ты много где пушишь объект с этими полями. Можно вынести в отдельную функцию |
||
from: begin, | ||
to: end | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. чисто математически алгоритм верный - да, надо получить пересечение "свободных" отрезков всех бандитов |
||
freeTime = formateFreeTime (freeTime); | ||
|
||
return freeTime; | ||
} | ||
|
||
function formateFreeTime(freeTime) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. судя по смыслу, аргумент лучше назвать There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а почему такое название функции? |
||
var res = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше названия не сокращать |
||
freeTime.forEach(function (interval) { | ||
var day1 = interval.from.getDate(); | ||
var day2 = interval.to.getDate(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. хм, возможно, лучше назвать |
||
for (var i = day1; i <= Math.min(day2, 3); i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. типа не дальше среды? |
||
res.push({ | ||
from: compareDate (1, new Date (YEAR, MONTH, i, 0, 0), interval.from), | ||
to: compareDate (-1, new Date (YEAR, MONTH, i, 23, 59), interval.to) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Привязка к конкретной дате |
||
}); | ||
} | ||
}); | ||
|
||
return res; | ||
} | ||
|
||
function dateToObject(date) { | ||
return { | ||
hours: parseInt(date.slice(0, 2), 10), | ||
minutes: parseInt(date.slice(3, 5), 10) | ||
}; | ||
} | ||
|
||
function findFreeIntervals(freeTime, workingHours, res) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Здесь больше подойдет глагол |
||
var startBankWork = dateToObject(workingHours.from); | ||
var endBankWork = dateToObject(workingHours.to); | ||
var bank = {}; | ||
freeTime.forEach(function (interval) { | ||
var day = interval.from.getDate(); | ||
bank.start = new Date (YEAR, MONTH, day, startBankWork.hours, startBankWork.minutes); | ||
bank.end = new Date (YEAR, MONTH, day, endBankWork.hours, endBankWork.minutes); | ||
var begin = compareDate (1, interval.from, bank.start); | ||
var end = compareDate (-1, interval.to, bank.end); | ||
if (end > begin) { | ||
res.push({ | ||
from: begin, | ||
to: end | ||
}); | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. опять по смыслу |
||
|
||
return res; | ||
} | ||
|
||
function mainFunction(timeToRobbery, msInDuration) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это название функции тоже не годится) |
||
var res = []; | ||
var n = timeToRobbery.length; | ||
var c = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Однобуквенных переменных вообще быть не должно, только если в крайних случаях это не цикл по индексам. |
||
for (var i = 0; i < n; i++) { | ||
var a = (timeToRobbery[i].to - timeToRobbery[i].from); | ||
if (a >= msInDuration) { | ||
res[c] = {}; | ||
res[c] = timeToRobbery[i]; | ||
c++; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. опять var intervals = timeToRobbery.reduce(function(res, interval) {
if (interval.to - interval.from >= msInDuration) {
res.push(interval);
}
return res;
}, []); и в конце можно написать тернарник: |
||
if (c === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно записать в 1 строку тернарными операторами) |
||
return null; | ||
} | ||
|
||
return res; // время для ограбления | ||
} | ||
|
||
function toString(number) { | ||
if (number < 10) { | ||
number = '0' + number; | ||
} | ||
number = String(number); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Есть метод |
||
|
||
return number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше короче: |
||
} | ||
|
||
function formateDate(date) { | ||
var day = date.getDate(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не проще ли сделать так: |
||
var hours = date.getHours(); | ||
var minutes = date.getMinutes(); | ||
day = WEEK[day]; | ||
hours = toString (hours); | ||
minutes = toString(minutes); | ||
|
||
return [day, hours, minutes]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нехорошо писать так: var x = rawData;
x = f(x);
x = g(x);
return [..., x, ...]; Типа перекладывать в одну и туже переменную разное содержимое. Сначала часы у тебя просто число, потом форматированная строка. Тут можно переделать так: var day = date.getDate();
// ...
return [WEEK[day], ...]; |
||
} | ||
|
||
/** | ||
* @param {Object} schedule – Расписание Банды | ||
|
@@ -14,8 +196,16 @@ exports.isStar = true; | |
* @param {String} workingHours.to – Время закрытия, например, "18:00+5" | ||
* @returns {Object} | ||
*/ | ||
|
||
exports.getAppropriateMoment = function (schedule, duration, workingHours) { | ||
console.info(schedule, duration, workingHours); | ||
var newSchedule = {}; | ||
var gmt = parseInt (workingHours.from.slice(5), 10); | ||
var msInDuration = 60 * 1000 * duration; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ай-ай-ай, константы! |
||
newSchedule = toNewSchedule (schedule, gmt); | ||
var freeSchedule = toFreeSchedule (newSchedule); | ||
var freeTime = findGangFreeTime (freeSchedule); | ||
var timeForRobbery = findFreeIntervals (freeTime, workingHours, []); | ||
timeForRobbery = mainFunction (timeForRobbery, msInDuration); | ||
|
||
return { | ||
|
||
|
@@ -24,7 +214,8 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { | |
* @returns {Boolean} | ||
*/ | ||
exists: function () { | ||
return false; | ||
|
||
return (timeForRobbery !== null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. можно без скобок |
||
}, | ||
|
||
/** | ||
|
@@ -35,16 +226,16 @@ exports.getAppropriateMoment = function (schedule, duration, workingHours) { | |
* @returns {String} | ||
*/ | ||
format: function (template) { | ||
return template; | ||
}, | ||
if (!this.exists()) { | ||
return ''; | ||
} | ||
var format = formateDate(timeForRobbery[0].from); | ||
var newTemplate = template | ||
.replace('%DD', format[0]) | ||
.replace('%HH', format[1]) | ||
.replace('%MM', format[2]); | ||
|
||
/** | ||
* Попробовать найти часы для ограбления позже [*] | ||
* @star | ||
* @returns {Boolean} | ||
*/ | ||
tryLater: function () { | ||
return false; | ||
return newTemplate; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. переменная |
||
} | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Вероятно, я не совсем так выразилась в прошлый раз)
В этой задаче к конкретным датам вообще привязываться не надо.