用于查找喜欢和不喜欢的数量的Python程序
Alice 和 Bob 列出了一个主题列表,他们都投票决定喜欢或不喜欢这个主题。他们写 0 表示不喜欢,写 1 表示喜欢。任务是计算他们都喜欢或不喜欢的主题数量。
例子:
Input: alice = “010101” bob = “101101”
Output: 3
alice and bob both like 4th and 6th topics and dislike the 5th topic. Hence, the output is 3.
Input: alice = “111111” bob = “000000”
Output: 0
There are no common likes or dislikes between alice and bob. Hence, the output is 0.
Input: alice = “110000” bob = “110011”
Output: 4
alice and bob both like 1st and 2nd topics and dislike 3rd and 4th topics. Hence, the result is 4.
方法:
- 首先,我们必须遍历由 alice 的好恶组成的字符串的每个字符。
- 然后,我们必须将它与由 bob 的好恶组成的字符串的相应条目进行比较。
- 最后,我们将统计相似条目的数量并打印。
下面是实现。
# function to obtain no
# of topics both alice and
# bob like
def commontopics(alice, bob):
# initiate count with 0
count = 0
# iterating through alice
for i in range(0,len(alice)):
# comparing with corresponding
# bob entry
if alice[i] == bob[i]:
# counting similar entries
count += 1
# printing the count
print(count)
#main function
def main():
commontopics("010101", "101101")
commontopics("111111", "000000")
commontopics("110000", "110011")
# driver code
if __name__ == "__main__":
main()
# This code is contributed by SrujayReddy
输出:
3
0
4