This is just a continuation of several past questions.
My function should return std::vector<std::set<std::string>>
A group of names should be classified into teams for a game. Teams should be the same size, but this is not always possible unless n is exactly divisible by k. Therefore, they decided that the first mode (n, k) teams have n / k + 1 members, and the remaining teams have n / k members.
Vector of strings which function accepts must be converted to list of strings.
I need to move through list elements and add them to the set. However, while moving through list I need to skip elements from list that are already added to set. First element of list is added to set before moving through list.
Moving is calculated based on length of last inserted member. For example if Damir is inserted, then move (shift) will be 5.
EXPLANATION:
UPDATE [after @stefaanv's suggestion]:
#include <iostream>
#include <list>
#include <set>
#include <string>
#include <vector>
typedef std::vector<std::set<std::string>> vek;
vek Distribution(const std::vector<std::string>&names, int k) {
vek teams(k);
int num = names.size();
int number_of_first = num % k;
int number_of_members_first = num / k + 1;
int number_of_members_remaining = num / k;
std::list<std::string> lista;
for (int i = 0; i < num; i++)
lista.push_back(names[i]);
auto it = lista.begin();
auto temp = it;
int n = num, new_member = 0, index_list = 0;
for (int i = 0; i < k; i++) {
if (i <= number_of_first) {
int number_of_members_in_team = 0;
while (number_of_members_in_team < number_of_members_first) {
for (int i = 0; i < new_member; i++)
index_list++;
if (index_list > n - 1)
index_list = index_list - n;
while (it != lista.begin())
it--;
for (int i = 0; i < index_list; i++)
it++;
teams[i].insert(*it);
number_of_members_in_team++;
new_member = it->length();
it = lista.erase(it);
n--;
}
} else {
int number_of_members_in_team = 0;
while (number_of_members_in_team < number_of_members_remaining) {
for (int i = 0; i < new_member; i++)
index_list++;
if (index_list > n - 1)
index_list = index_list - n;
while (it != lista.begin())
it--;
for (int i = 0; i < index_list; i++)
it++;
teams[i].insert(*it);
number_of_members_in_team++;
new_member = it->length();
it = lista.erase(it);
n--;
}
}
}
return teams;
}
int main() {
for (auto i :
Distribution({"Damir", "Ana", "Muhamed", "Marko", "Ivan", "Mirsad",
"Nikolina", "Alen", "Jasmina", "Merima"},
3)) {
for (auto j : i)
std::cout << j << " ";
std::cout << std::endl;
}
return 0;
}
Correct output would be:
Ana Damir Mirsad Muhammed
Ivan Merima Nikolina
Alen Jasmina Marko
I get nothing in the output!
I just need simple fix in algorithm. Reason why I have nothing in output is this:
it = lista.erase(it);
n--;
If I don't use those lines of code I would get 3 teams with 4, 3, and 3 names respectively, just with wrong names. So now I get the right number of teams and the right number of team members. The only problem here remains to add correct names to teams...
Could you give me better approach?