- # // A music player allows users to choose songs to play, but only in pairs and only songs
- # // with duration that adds up to a multiple of 60 seconds (e.g. 60,120,180 etc.). Given a
- # // list of song durations, calculate the total number of song pairs that can be chosen.
- # input [20,30, ...]
- def find_song_pairs(list_songs):
- count = 0
- i = 0
- if type(list_songs) == list:
- if len(list_songs) >= 2:
- while i < len(list_songs):
- j = i+1
- while j < len(list_songs):
- if ((list_songs[i] + list_songs[j] ) % 60) == 0:
- count += 1
- j += 1
- i += 1
- result = count
- else:
- result = f"Length Error: Song duration list contains {len(list_songs)} song(s) which is less than 2 items"
- else:
- result = f"Type Error: Function expecting an input of type 'list' but received type {type(list_songs)}"
- return result
- if __name__ == '__main__':
- '''
- Test Cases Results:
- # Finds correct pair count: [60, 20, 10, 180, 50, 240]
- Result: 4 pairs
- # Finds no pair count: [50, 60, 15, 100]
- Result: 0 pairs
- # Empty list []
- Result: Length error
- # List with 1 element
- Result: Length error
- # Input of type int
- Result: Type error
- '''
- test_cases = {
- 'correct_pair': [60, 20, 10, 180, 50, 240],
- 'no_pairs': [50, 60, 15, 100],
- 'empty_list': [],
- 'one_song_duration': [180],
- 'type_error': 3
- }
- for test in test_cases.values():
- res = find_song_pairs(test)
- print(res)
Raw Paste