This code has many flaws. The first one is, it is difficult to read: classes seem not to be known, functions neither.
Don't fix broken code, rewrite it.
So, how about invent yourself the implementation of the task at hand. Did you understand the task at all? That's where it starts. Try to rewrite it in you own words and ask question on the spot. E.g.
1) find three kind of people and group them in teams of 5-8 people
2) one kind of people are friends, the other follower, the third outsiders
3) friends are mutually referenced people
4) followers are unilateral connections
5) outsiders have no contacts
What if the sets of kind of people have less persons than makes a group?
Do the groups have to be evenly sized?
...
From that try to think of data and instructions and write them down in pseudo code. E.g.
class Person
{
variable relations as list of Persons
function isFriend of a Person P { myself isFollower of P and P isFollower of myself }
function isFollower of a Person P { P is one of my relations }
function add Person P { make P being one of my relations unless P is myself }
}
variable comunity is a set of Persons that have a certain realtion
variable friends is a set of Persons given by Friends from the comunity
variable followers is a set of Persons given by Followers from the comunity
variable outsiders is a set of Persons given by Outsiders from the comunity
function Friends of the community
{ member of the community where at least one Person of the comunity is a friend of that member }
function Followers of the community
{ member of the community where at least one Person of the comunity is a follower of that member }
function Outsiders of the community
{ members of the community where no Person of the community is neither friend nor follower of that member }
Write Groups of friends
Write Groups of followers
Write Groups of outsiders
function Write ...
fucntion Groups of a set of Persons
{
if set is less that 5 person, take these persons,
otherwise split set into groups of 8, 7, 6, 5 until the rest if the splitting is 0 or larger than 4
}
Transform this in classes and methods of the target language.
Now you see that the code of your question does not show anything of the decomposition shown here.
No structure in data nor code.
No meaningful naming of the variables (give logic names not type-derived names, e.g. not
dict
, but rather
friends
, etc.)
Cheers
Andi