因此,对于家庭作业,我们被要求编写一个函数,该函数将 2 个列表作为输入,并使用顺序/线性搜索来遍历它们,如果两个列表中都出现了任何名称,则将该名称附加到新列表中。对于实际分配,两个类被指定为 VoterList 和 VoterName,因此不允许我们使用“in”,并且只有 VoterNames 可以附加到 VoterList。(这项任务将发展为寻找在两个不同的投票亭中为一次选举投票两次的人)。
因此,我编写了一个函数,当我传入 3-4 人的长列表时,它似乎可以工作,但我不确定它实际上是一个顺序搜索,它应该是如何工作的。一些建议会很棒。干杯
def fraud_detect_seq(first_booth_voters, second_booth_voters):
fraud = []
length_first = len(first_booth_voters)
length_second = len(second_booth_voters)
first_booth_position = 0
second_booth_position = 0
while first_booth_position < length_first:
name_comparison = first_booth_voters[first_booth_position]
if second_booth_position == length_second:
first_booth_position += 1
second_booth_position = 0
elif second_booth_voters[second_booth_position] == name_comparison:
fraud.append(second_booth_voters[second_booth_position])
first_booth_position += 1
second_booth_position += 1
elif second_booth_voters[second_booth_position] != name_comparison:
second_booth_position += 1
print(fraud)
fraud_detect_seq(['Jackson', 'Dylan', 'Alice'],['Jackson', 'ylan', 'Alice'])
获取输出:
['Jackson', 'Alice']
哪个是对的。但我觉得我做得不对。