USING PYTHON 3.7:
Exercise 1 – Comparing song lengths
Complete the function design for the is_longer function, whichtakes two songs as parameters, and returns the longer of the twosongs. In the case of a tie, the song that is the first parameteris returned.
Exercise 2 – Counting the number of songs with a givenartist
Complete the function design for the count_with_artist function,which takes a list of songs and a string (representing the name ofan artist) as parameters, and returns the number of songs in thelist with the given artist.
PYTHON CODES:
tests = 0
passed = 0
import Song as s
def main():
### PART 1: Song
test_is_longer()
test_count_with_artist()
print(“TEST RESULTS:”, passed, “/”,tests)
########################
### PART 1 FUNCTIONS ###
def test_is_longer():
print(“testing is_longer”)
s1 = s.Song(“Despacito”, “Justin Bieber”, 225)
s2 = s.Song(“Crazy”, “Britney Spears”, 245)
s3 = s.Song(“Champions”, “Queen”, 220)
s4 = s.Song(“The Motto”, “Drake”, 225)
result = is_longer(s1, s2)
print_test(“testing with s1 and s2”, result==s2)
result = is_longer(s1, s3)
print_test(“testing with s1 and s3”, result==s1)
result = is_longer(s1, s4)
print_test(“testing with s1 and s4”, result==s1)
print()
# (Song, Song -> Song)
# return the song with the longest duration
def is_longer(s1, s2):
print(“Fix me”)
def test_count_with_artist():
print(“testing count_with_artist”)
s1 = s.Song(“Despacito”, “Justin Bieber”, 225)
s2 = s.Song(“Crazy”, “Britney Spears”, 245)
s3 = s.Song(“Champions”, “Queen”, 220)
s4 = s.Song(“The Motto”, “Drake”, 225)
s5 = s.Song(“Baby”, “Justin Bieber”, 216)
list0 = []
result = count_with_artist(list0, “JustinBieber”)
print_test(“testing with empty and Bieber”,result==0)
list1 = [s1, s2, s3, s4, s5]
result = count_with_artist(list1, “Elton John”)
print_test(“testing with list1 and Elton John”,result==0)
result = count_with_artist(list1, “Drake”)
print_test(“testing with list1 and Drake”,result==1)
result = count_with_artist(list1, “JustinBieber”)
print_test(“testing with list1 and Bieber”,result==2)
print()
# ((list of Song), str -> int)
# return a count of the number of songs
# in the list with the given artist
def count_with_artist(los, a):
print(“Fix me”)
# (str, bool -> None)
# takes the name or description of a test and whether the
# test produced the expected output (True) or not (False)
# and prints out whether that test passed or failed
# NOTE: You should not have to modify this in any way.
def print_test(test_name, result_correct):
global tests
global passed
tests += 1
if(result_correct):
print(test_name + “: passed”)
passed += 1
else:
print(test_name + “: failed”)
# The following code will call your main function
if __name__ == ‘__main__’:
main()
——————————————————
Song.py:
class Song:
def __init__(self, title, artist, duration):
self.title = title
self.artist = artist
self.duration = duration
def __str__(self):
result = ‘”‘ + self.title + ‘” – ‘+ self.artist
return result
def __repr__(self):
return self.title
def __eq__(self, other):
if (type(other) != Song):
returnFalse
if self.title == other.get_title()and self.artist == other.get_artist():
returnTrue
else:
return False
def get_title(self):
return self.title
def set_title(self, title):
self.title = title
def get_artist(self):
return self.artist
def set_artist(self, artist):
self.artist = artist
def get_duration(self):
return self.duration
def set_duration(self, duration):
self.duration = duration
Expert Answer
Answer to USING PYTHON 3.7: Exercise 1 – Comparing song lengths Complete the function design for the is_longer function, which t…