You are given a play list, where each song is represented by an integer. Your task is to count how many sublists contain no song twice.
For example, the sublists of the play list are , , , , , , , , and . For instance, the sublist is valid as is contains each of the songs and once only. The sublist is not valid as it contains the song twice.
The desired answer for the play list is , because the valid sublists are , , , , , , and . Notice that the sublist is counted twice since it occurs twice in the play list.
In a file playlist.py
, implement the function count_parts
that takes a play list as a parameter and returns the number of valid sublists.
The function should be efficient even for long lists. The last two test cases in the code template are long lists and the function should finish quickly in these cases too.
def count_parts(songs): # TODO if __name__ == "__main__": print(count_parts([1, 1, 1, 1])) # 4 print(count_parts([1, 2, 3, 4])) # 10 print(count_parts([1, 2, 1, 2])) # 7 print(count_parts([1, 2, 1, 3])) # 8 print(count_parts([1, 1, 2, 1])) # 6 songs = [1, 2] * 10**5 print(count_parts(songs)) # 399999 songs = list(range(1, 10**5 + 1)) * 2 print(count_parts(songs)) # 15000050000