中级农民
- 积分
- 135
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2020-6-15
- 最后登录
- 1970-1-1
|
本帖最后由 李浩泉 于 2020-8-5 06:53 编辑
做几道DE的真题,检验一下。SDE们也可以过来看看,试试。
# What Is Wrong With This Family?
Michael always knew that there was something wrong with his family. Many strangers were introduced to him as part of it.
Michael should figure this out. He's spent almost a month parsing the family archives. He has all father-son connections of his entire family collected in one place.
With all that data Michael can easily understand who the strangers are. Or maybe the only stranger in this family is Michael? Let's see.
You have a list of family ties between father and son. Each element on this list has two elements. The first is the father's name, the second is the son's name. All names in the family are unique. Check if the family tree is correct. There are no strangers in the family tree. All connections in the family are natural.
Input: list of lists. Each element has two strings. The list has at least one element
Output: bool. Is the family tree correct.
- def is_family(tree):
-
- # 先大概清理一下数据,简单条件排除明显错误:排除多个祖先
- father = []
- son = []
- for c in tree:
- father.append(c[0])
- son.append(c[1])
- if len(set(father).difference(set(son))) != 1:
- return False
-
- # 排除互为父子现象
- for j in range(len(tree)):
- for k in range(1,len(tree)):
- if tree[j] == tree[k][::-1]:
- return False
-
- # 为了构造有序的新树,先做一个小函数,返回当前的Father
- def top_helper(tree):
- father = []
- son = []
- for c in tree:
- father.append(c[0])
- son.append(c[1])
- if len(set(father).difference(set(son))) == 1:
- return ''.join(set(father).difference(set(son)))
-
- # 构建一个有序的新树,保证先祖永远在前,子孙永远在后
- dummy_tree = tree
- list1 = []
- list2 = []
- while len(dummy_tree) > 1:
- top = top_helper(dummy_tree)
- list2 = []
- for i in range(len(dummy_tree)):
- if top in dummy_tree[i]:
- list1.append(dummy_tree[i])
- else:
- list2.append(dummy_tree[i])
- dummy_tree = list2
- new_tree = list1 + list2
-
- # 字典法,祖先为0,后世子孙从1开始递加,如果最后字典成立,则返回True
- if len(new_tree) < 2: return True
- dic = {}
- dic[new_tree[0][0]] = 0
- dic[new_tree[0][1]] = 1
- num = 1
- for i in range(1,len(new_tree)):
- if new_tree[i][0] not in dic or new_tree[i][1] in dic:
- return False
- elif new_tree[i][1] not in dic:
- dic[new_tree[i][1]] = num + 1
- num += 1
- return True
- if __name__ == "__main__":
- #These "asserts" using only for self-checking and not necessary for auto-testing
- assert is_family([
- ['Logan', 'Mike']
- ]) == True, 'One father, one son'
- assert is_family([
- ['Logan', 'Mike'],
- ['Logan', 'Jack']
- ]) == True, 'Two sons'
- assert is_family([
- ['Logan', 'Mike'],
- ['Logan', 'Jack'],
- ['Mike', 'Alexander']
- ]) == True, 'Grandfather'
- assert is_family([
- ['Logan', 'Mike'],
- ['Logan', 'Jack'],
- ['Mike', 'Logan']
- ]) == False, 'Can you be a father to your father?'
- assert is_family([
- ['Logan', 'Mike'],
- ['Logan', 'Jack'],
- ['Mike', 'Jack']
- ]) == False, 'Can you be a father to your brother?'
- assert is_family([
- ["Logan","Mike"],
- ["Alexander","Jack"],
- ["Jack","Alexander"]
- ]) == False
- assert is_family([
- ["Logan","Mike"],
- ["Alexander","Jack"],
- ["Jack","Logan"]
- ]) == True,'Family connections can be listed in any directions'
- assert is_family([
- ['Logan', 'Mike'],
- ['Logan', 'Jack'],
- ['Mike', 'Alexander']
- ]) == True
- assert is_family([
- ['Logan', 'William'],
- ['Logan', 'Jack'],
- ['Mike', 'Alexander']
- ]) == False, 'Looks like Mike is stranger in Logan\'s family'
- print("Looks like you know everything. It is time for 'Check'!")
复制代码
[/i][/i][/i][/i][/i][/i][/i]
|
|