Question
add a sort by ID to this python code class Song : # constructor def __init__ ( self , song_name , artist ): self .
add a sort by ID to this python code
class Song:
# constructor
def __init__(self, song_name, artist):
self.song_name = song_name
self.artist = artist
# points to the next node
self.next = None
# setter methods
def setSongName(self, song_name):
self.song_name = song_name
def setArtist(self, artist):
self.artist = artist
def setNext(self, next):
self.next = next
# getter methods
def getSongName(self):
return self.song_name
def getArtist(self):
return self.artist
def getNext(self):
return self.next
class linkedlist:
# constructor
def __init__(self):
# points to the first node
self.first = None
# store the size f list
self.size = 0
# add newSong object to the first position
def AddsongToFront(self, newSong):
# make newSong point to first node of list
newSong.setNext(self.first)
# set newSong as the first node of list
self.first = newSong
# add newSong object to the given position
def AddSongAtPosition(self, newSong, x):
if x == 0:
self.AddsongToFront(newSong)
else:
trav = self.first
for i in range(1, x):
# move to next node
trav = trav.getNext()
newSong.setNext(trav.getNext())
trav.setNext(newSong)
def RemoveSongAtPosition(self, x):
# if list is empty
if self.first == None:
print('Error! List is already empty')
return
elif x < 0 and x >= self.size:
print('Error! Enter a valid position')
return
# if the node to be removed is the first node
elif x == 0:
# move to next node
self.first = self.first.getNext()
else:
# point trav to first node of the list
trav = self.first
for i in range(1, x):
# move to next node
trav = trav.getNext()
trav.setNext(trav.getNext().getNext())
def DisplaySong(self):
# point trav to first node of the list
trav = self.first
# traverse the list
while trav != None:
print(trav.getSongName(), ' : ', trav.getArtist())
# move to next node
trav = trav.getNext()
def SortByArtistName(self):
arr = []
# point trav to first node of the list
trav = self.first
# traverse the list
while trav != None:
# append the current node data to arr
arr.append([trav.getSongName(), trav.getArtist()])
# move to next node
trav = trav.getNext()
# sort list
arr.sort(key = lambda x : x[1])
# point trav to first node of the list
trav = self.first
i = 0
# traverse the list
while trav != None:
trav.setSongName(arr[i][0])
trav.setArtist(arr[i][1])
# move to next node
trav = trav.getNext()
i += 1
ob = linkedlist()
ob.AddsongToFront(Song('Perfect', 'Ed Sheeren'))
ob.AddsongToFront(Song('Hello', 'Adele'))
ob.AddsongToFront(Song('Toxic', 'Britney Spears'))
ob.AddSongAtPosition(Song('Pal', 'Arijit Singh'), 0)
ob.AddSongAtPosition(Song('Malibu', 'Miley Cyrus'), 3)
ob.SortByArtistName()
ob.DisplaySong()
print(' After removing element from first place')
ob.RemoveSongAtPosition(0)
ob.DisplaySong()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started