Question

Use sorting techniques in the language Python 3

Devise an algorithm and implement it in a program to solve the following problem, similar to one often faced by an MP3 player

0 0
Add a comment Improve this question Transcribed image text
Answer #1
class Song:
    def __init__(self, name, composer, length):
        self.name = name
        self.composer = composer
        self.length = length

    def __lt__(self, other):
        if self.length==other.length and self.name is other.name:
            return self.composer<other.composer
        elif self.length == other.length:
            return self.name < other.name
        else:
            return self.length > other.length


if __name__ == "__main__":
    n=int(input())

    # n, k = list(map(int, input().split()))
    k=int(input())
    c=input()
    songs = []
    for i in range(n):
        list1 = list(input().split(c))
        if len(list1) is 2:
            songs.append(Song(list1[0], None, list1[1]))
        else:
            songs.append(Song(list1[0], list1[1], list1[2]))
    songs.sort()
    if k > n:
        print("provided k is greater than n")
    else:
        for i in range(k):
            print(songs[i].name, " ", songs[i].length)

Add a comment
Know the answer?
Add Answer to:
Use sorting techniques in the language Python 3 Devise an algorithm and implement it in a program to solve the following problem, similar to one often faced by an MP3 player. For our purposes, a song...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A*...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class. For more information, please read the wiki page of 15-puzzle problem at https://en.wikipedia.org/wiki/15_puzzle, and the wiki page of...

  • Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* s...

    Major Homework #2 Implement a C program major_hw2.c to solve the 15-puzzle problem using the A* search algorithm. Please include pictures that the code runs and shows the different states as it reaches goal state please. 1. Objectives • To gain more experience on using pointers and linked lists in C programs. • To learn how to solve problems using state space search and A* search algorithm. 2. Background A* search and 15-puzzle problem have been introduced in the class....

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT