PYTHON   86
By jl3684 on 9th May 2022 07:33:21 PM

1. # // A music player allows users to choose songs to play, but only in pairs and only songs
2. # // with duration that adds up to a multiple of 60 seconds (e.g. 60,120,180 etc.).  Given a
3. # // list of song durations, calculate the total number of song pairs that can be chosen.
4.
5. # input [20,30, ...]
6.
7. def find_song_pairs(list_songs):
8.     count = 0
9.     i = 0
10.
11.     if type(list_songs) == list:
12.         if len(list_songs) >= 2:
13.             while i < len(list_songs):
14.                 j = i+1
15.                 while j < len(list_songs):
16.                     if ((list_songs[i] + list_songs[j] ) % 60) == 0:
17.                         count += 1
18.                     j += 1
19.                 i += 1
20.             result = count
21.         else:
22.             result = f"Length Error: Song duration list contains {len(list_songs)} song(s) which is less than 2 items"
23.     else:
24.         result = f"Type Error: Function expecting an input of type 'list' but received type {type(list_songs)}"
25.
26.     return result
27.
28.
29. if __name__ == '__main__':
30.     '''
31.    Test Cases Results:
32.    # Finds correct pair count: [60, 20, 10, 180, 50, 240]
33.        Result: 4 pairs
34.    # Finds no pair count: [50, 60, 15, 100]
35.        Result: 0 pairs
36.    # Empty list []
37.        Result: Length error
38.    # List with 1 element
39.        Result: Length error
40.    # Input of type int
41.        Result: Type error
42.    '''
43.     test_cases = {
44.         'correct_pair': [60, 20, 10, 180, 50, 240],
45.         'no_pairs': [50, 60, 15, 100],
46.         'empty_list': [],
47.         'one_song_duration': [180],
48.         'type_error': 3
49.     }
50.     for test in test_cases.values():
51.         res = find_song_pairs(test)
52.         print(res)

Raw Paste

or to edit or fork this paste. It's free.