Skip to content
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

Ганиева Альбина #31

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
363ae6e
Решение задачи
Albina-G Nov 23, 2016
29b0221
синтаксис
Albina-G Nov 23, 2016
e4dfb80
синтаксис 2
Albina-G Nov 23, 2016
266377f
синтаксис
Albina-G Nov 23, 2016
a1be98b
синтаксис
Albina-G Nov 23, 2016
5b03023
тест несвязность друзей
Albina-G Nov 26, 2016
dc4e618
наследование
Albina-G Nov 26, 2016
7776559
время работы алгоритма
Albina-G Nov 26, 2016
4b1ab77
синтаксис
Albina-G Nov 26, 2016
7cf4531
синтаксис
Albina-G Nov 26, 2016
93deec7
statement
Albina-G Nov 26, 2016
07642bc
синтаксис
Albina-G Nov 26, 2016
3a09b51
level
Albina-G Nov 26, 2016
7f5b4d7
оптимизирован
Albina-G Nov 29, 2016
474c1b9
синтаксис
Albina-G Nov 29, 2016
8ff0512
нехватает скобки
Albina-G Nov 29, 2016
e71fe5a
лишняя переменная
Albina-G Nov 29, 2016
d9f9ee3
добавлено условие
Albina-G Nov 30, 2016
196512d
синтаксис
Albina-G Nov 30, 2016
1fc0e58
проверка
Albina-G Nov 30, 2016
15d4f99
проверка
Albina-G Nov 30, 2016
c8852fe
проверка
Albina-G Nov 30, 2016
d495b40
фильтр по умолчанию
Albina-G Nov 30, 2016
35b6390
корректность
Albina-G Nov 30, 2016
c4432b5
добавлены условия
Albina-G Dec 1, 2016
cd5c5a3
синтаксис
Albina-G Dec 1, 2016
149314d
без проверки ориентированости
Albina-G Dec 1, 2016
3c99256
проверка ориентированости
Albina-G Dec 2, 2016
f15483e
синтаксис
Albina-G Dec 2, 2016
02681f5
условие для ориентированости
Albina-G Dec 2, 2016
16bc352
список друзей
Albina-G Dec 2, 2016
6f9ee54
откат, проверка undefined
Albina-G Dec 2, 2016
cfee520
условие фильтровки
Albina-G Dec 2, 2016
9822b15
xnj
Albina-G Dec 2, 2016
f8bbcde
filter
Albina-G Dec 2, 2016
2abd0f8
итерации
Albina-G Dec 2, 2016
f6b248f
синтаксис
Albina-G Dec 2, 2016
f743663
усовершенствование итерации
Albina-G Dec 2, 2016
f420a84
синтаксис
Albina-G Dec 2, 2016
fddbcd7
итерация
Albina-G Dec 2, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 177 additions & 5 deletions lib.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,159 @@
'use strict';

var levels = {
level: undefined,
friends: undefined,
names: undefined
};

function functionCompareByName(friend, friendNext) {

return friend.name > friendNext.name ? 1 : -1;
}

function onlyConnectedFriends(allFriends) {
var allFriendsFriends = [];
allFriends.forEach(function (item) {
if (item.friends === undefined || item.friends.indexOf(undefined) !== -1) {
throw new TypeError('friends of ungefined');
}
item.friends.forEach(function (friendItem) {
allFriendsFriends.push(friendItem);
});
});

return allFriendsFriends;
}

function findBestFriends(arg, allFriends, noInviteFriends) {
var namesAllPeople = arg[0];
var friendsOnLevel = arg[1];
var friendsFriendsOnLevel = [];
friendsOnLevel.friends = allFriends.filter(function (item) {
if (item.best) {
choiceFriend(item, friendsFriendsOnLevel);

return true;
}
if (namesAllPeople.indexOf(item.name) !== -1) {
noInviteFriends.push(item);
}

return false;

}).sort(functionCompareByName);
friendsOnLevel.names = friendsFriendsOnLevel;
}

function choiceFriendsOnLevel(allFriends, maxLevel, filter) {
if (filter.type !== 'male') {
maxLevel = Infinity;
}
if (maxLevel === undefined || maxLevel === 0) {

return [];
}
var friendsOnLevel = Object.create(levels);
var sortFriends = [];
var noInviteFriends = [];
friendsOnLevel.level = 0;
var namesAllPeople = onlyConnectedFriends(allFriends);
var argument1 = [namesAllPeople, friendsOnLevel];
findBestFriends(argument1, allFriends, noInviteFriends);
sortFriends.push(friendsOnLevel);
var argument2 = [noInviteFriends, sortFriends];
findFriends(argument2, maxLevel);

return sortFriends;
}

function findFriends(arg, maxLevel) {
var noInviteFriends = arg[0];
var sortFriends = arg[1];
var iteration = 1;
while (noInviteFriends.length !== 0) {
if (iteration === maxLevel) {
break;
}
var friendsLevel = Object.create(levels);
friendsLevel.level = iteration;
var choiceFriends = [];
var argument = [noInviteFriends, sortFriends, friendsLevel];
inspection(argument, iteration, choiceFriends);
friendsLevel.friends = choiceFriends.sort(functionCompareByName);
sortFriends.push(friendsLevel);
iteration++;
}
}

function inspection(arg, iteration, choiceFriends) {
var noInviteFriends = arg[0];
var sortFriends = arg[1];
var friendsLevel = arg[2];
var namesFriends = [];
for (var i = 0; i < noInviteFriends.length; i++) {
var indexNamePeople = sortFriends[iteration - 1].names.indexOf(noInviteFriends[i].name);
if (indexNamePeople !== -1) {
choiceFriends.push(noInviteFriends[i]);
choiceFriend(noInviteFriends[i], namesFriends);
noInviteFriends.splice(i, 1);
i--;
}
}
friendsLevel.names = namesFriends;
}

function choiceFriend(item, friendsFriendsOnLevel) {
item.friends.forEach(function (nameFriendItem) {
if (friendsFriendsOnLevel.indexOf(item.name) === -1) {
friendsFriendsOnLevel.push(nameFriendItem);
}
});
}

/**
* Итератор по друзьям
* @constructor
* @param {Object[]} friends
* @param {Filter} filter
*/
function Iterator(friends, filter) {
console.info(friends, filter);
if (!(filter instanceof Filter)) {
throw new TypeError('Filter не является прототипом filter');
}
var workWithFriends = choiceFriendsOnLevel(friends, arguments[2], filter);
this.inviteFriends = filterFriendsByGender(workWithFriends, filter);
this.indexFriend = 0;
}

function filterFriendsByGender(friends, filter) {
var friendsFilter = [];
friends.forEach(function (item) {
item.friends.forEach(function (friend) {
if (filter.field(friend)) {
friendsFilter.push(friend);
}
});
});

return friendsFilter;
}

Iterator.prototype.done = function () {

return this.indexFriend === this.inviteFriends.length;
};

Iterator.prototype.next = function () {
if (this.done()) {

return null;
}
this.indexFriend++;

return this.inviteFriends[this.indexFriend - 1];
};

/**
* Итератор по друзям с ограничением по кругу
* @extends Iterator
Expand All @@ -19,35 +163,63 @@ function Iterator(friends, filter) {
* @param {Number} maxLevel – максимальный круг друзей
*/
function LimitedIterator(friends, filter, maxLevel) {
console.info(friends, filter, maxLevel);
Iterator.call(this, friends, filter, maxLevel);
}

LimitedIterator.prototype = Object.create(Iterator.prototype);

var allFilters = {
aFilter: function () {

return true;
},
aMaleFilter: function (friend) {

return friend.gender === 'male';
},
aFemaleFilter: function (friend) {

return friend.gender === 'female';
}
};

/**
* Фильтр друзей
* @constructor
*/
function Filter() {
console.info('Filter');
this.field = allFilters.aFilter;
}

Filter.prototype.apply = function () {

return this.field.apply(null, arguments);
};

/**
* Фильтр друзей
* @extends Filter
* @constructor
*/
function MaleFilter() {
console.info('MaleFilter');
this.field = allFilters.aMaleFilter;
this.type = 'male';
}

MaleFilter.prototype = Object.create(Filter.prototype);

/**
* Фильтр друзей-девушек
* @extends Filter
* @constructor
*/
function FemaleFilter() {
console.info('FemaleFilter');
this.field = allFilters.aFemaleFilter;
this.type = 'female';
}

FemaleFilter.prototype = Object.create(Filter.prototype);

exports.Iterator = Iterator;
exports.LimitedIterator = LimitedIterator;

Expand Down