# Context

According to Wikipedia : "The seventh son of a seventh son is a concept from folklore regarding special powers given to, or held by, such a son. **The seventh son must come from an unbroken line with no female siblings born between, and be, in turn, born to such a seventh son.**"

# Your task

You will be given a string of JSON, consisting of a family tree containing people's names, genders and children. Your task will be to find the seventh sons of seventh sons in the family tree, and return a __set__ of their names. If there are none, return an __empty set__.


## Tips

* Have a good look at the sample test cases.

* For a seventh son to be a seventh son, there must not be any daughters in the line leading to him. There may be daughters after him, though.

* **You may want to use the json module for this one.**

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Complete search

Starter code: def find_seventh_sons_of_seventh_sons(jstring):
	

Solutions:
import json

def f(data, level):
	if level == 0:
		yield data['name']
		return
	children = data['children']
	if len(children) >= 7 and all((child['gender'] == 'male' for child in children[:7])):
		yield from f(children[6], level - 1)
	for child in children:
		yield from f(child, 2)

def find_seventh_sons_of_seventh_sons(jstring):
	data = json.loads(jstring)
	return set(f(data, 2))


def find_seventh_sons_of_seventh_sons(jstring):
	seventh_of_seventh = set()

	def find_seventh_sons(father, seventh_son=False):
		seven_brother = True
		for (i, son) in enumerate(father['children'], 1):
			if son['gender'] == 'female':
				seventh_son = seven_brother = False
			if i == 7 and seventh_son:
				seventh_of_seventh.add(son['name'])
			find_seventh_sons(son, i == 7 and seven_brother)
	find_seventh_sons(json.loads(jstring))
	return seventh_of_seventh


import json

def find_seventh_sons_of_seventh_sons(jstring):
	return seek4SeventhJSon(json.loads(jstring))

def seek4SeventhJSon(parent, _7up=False):
	(nSons, _7sons7) = (0, set())
	for (n, child) in enumerate(parent['children'], 1):
		if child['gender'] == 'male':
			nSons += 1
			_7sons7 |= seek4SeventhJSon(child, n == nSons == 7)
			if n == nSons == 7 and _7up:
				_7sons7.add(child['name'])
	return _7sons7


import json

def find_seventh_sons_of_seventh_sons(jstring):
	(result, stack) = (set(), [json.loads(jstring)])
	while stack:
		person = stack.pop()
		if len(person['children']) >= 7 and all((child['gender'] == 'male' for child in person['children'][:7])) and (len(person['children'][6]['children']) >= 7) and all((child['gender'] == 'male' for child in person['children'][6]['children'][:7])):
			result.add(person['children'][6]['children'][6]['name'])
		stack.extend(person['children'])
	return result


import json

def validate_sons(data):
	if data['gender'] == 'male' and len(data['children']) >= 7:
		if all([x['gender'] == 'male' for x in data['children'][:7]]):
			if len(data['children'][6]['children']) >= 7:
				if all([x['gender'] == 'male' for x in data['children'][6]['children'][:7]]):
					return True
	return False

def find_seventh_sons_of_seventh_sons(jstring):
	data = json.loads(jstring)
	result = []
	stack = [data]
	while len(stack) != 0:
		a = stack.pop()
		for children in a['children']:
			stack.append(children)
		if validate_sons(a):
			result.append(a['children'][6]['children'][6]['name'])
	return set(result)


def find77(j, son=False, sons=[]):
	sc = 0
	for c in j['children']:
		if c['gender'] == 'male':
			sc += 1
		else:
			sc = 8
		if sc == 7 and 'children' in list(c.keys()):
			sons = find77(c, True, sons)
		elif 'children' in list(c.keys()):
			sons = find77(c, False, sons)
		if sc == 7 and son:
			sons = [c['name']] + sons
	return sons

def find_seventh_sons_of_seventh_sons(jstring):
	jstring = eval(jstring)
	sonlist = find77(jstring, False)
	if len(sonlist) == 0:
		return set()
	else:
		return eval(str(sonlist).replace('[', '{').replace(']', '}'))


def find_seventh_sons_of_seventh_sons(jstring):
	return set(has_7_of_7_son(json.loads(jstring)))

def has_7_of_7_son(person, parent_is_7=False):
	counter = 0
	result = []
	broken = False
	if person['children']:
		for child in person['children']:
			if child['gender'] == 'male':
				counter += 1
			else:
				broken = True
			if counter == 7 and (not broken):
				if parent_is_7:
					result.append(child['name'])
				result.extend(has_7_of_7_son(child, True))
			result.extend(has_7_of_7_son(child, False))
	return result


import json

def find_seventh_sons_of_seventh_sons(jstring, z=[]):
	if type(jstring) == str:
		jstring = json.loads(jstring)
		z = []
	if len(jstring['children']) > 6 and all((children['gender'] == 'male' for children in jstring['children'])) and (len(jstring['children'][6]['children']) > 6) and all((jstring['children'][6]['children'][i]['gender'] == 'male' for i in range(7))):
		z.append(jstring['children'][6]['children'][6]['name'])
	for x in jstring['children']:
		find_seventh_sons_of_seventh_sons(x, z)
	return set(z)


import json

def isItAChainOfSons(listOfCildren):
	chainOfSons = True
	for i in range(0, 7):
		if listOfCildren[i]['gender'] == 'female':
			chainOfSons = False
	return chainOfSons

def rekursiveSonFinder(family, secondGeneration):
	if family['gender'] == 'female' or len(family['children']) < 7:
		return '/'
	elif isItAChainOfSons(family['children']):
		if secondGeneration:
			return '/' + family['children'][6]['name']
		else:
			return rekursiveSonFinder(family['children'][6], True)

def rekursiveIteration(family):
	if len(family['children']) == 0:
		return '/'
	else:
		ownSolution = rekursiveSonFinder(family, False)
		solutionOfChildren = ''
		for child in family['children']:
			if len(child['children']) > 0:
				solutionOfChildren = solutionOfChildren + rekursiveIteration(child)
		if ownSolution == None:
			return solutionOfChildren
		else:
			return ownSolution + solutionOfChildren

def find_seventh_sons_of_seventh_sons(jstring):
	familyTree = json.loads(jstring)
	listOfSons = rekursiveIteration(familyTree)
	listOfSons = listOfSons.split('/')
	solution = set()
	for name in listOfSons:
		if name != '':
			solution.add(name)
	return solution


def find_seventh_sons_of_seventh_sons(family_dict, ss_set=set()):
	if type(family_dict) == str:
		ss_set.clear()
		family_dict = json.loads(family_dict)
	if len(family_dict['children']) > 6 and len(family_dict['children'][6]['children']) > 6:
		children_genders = [x['gender'] for x in family_dict['children'][:7]] + [x['gender'] for x in family_dict['children'][6]['children'][:7]]
		if not 'female' in children_genders:
			ss_set.add(family_dict['children'][6]['children'][6]['name'])
	for child in family_dict['children']:
		find_seventh_sons_of_seventh_sons(child, ss_set)
	return ss_set