Sort ice-creams based on ratings

Problem Statement

The company ABC has online ice-cream store. The company wants to sort the ice-creams, based on the average rating given by the customers for the respective ice-creams in the online store. This can help the company to display the most rated ice-creams in the top, and help the customers choose.

Input

The first line of input is icecream_names, space separated values, where each value represents the name of an ice-cream.

The second line of input is ratings, space separated values, where each value is a float number representing the average rating given to the respective ice-cream (from the first input). An average rating is in the range [1.0, 5.0] in steps of 0.1.

The above two inputs are converted to lists. And these lists are of same length.

Output

Print the ice-cream names in the descending order of the average rating.

As is

The boilerplate code is already there to read the inputs from user, in the main() function, and to print the value returned by the sort_icecreams_by_rating() function. Do not edit this code.

To do ✍

sort_icecreams_by_rating() has two parameters: icecream_names and ratings, that are lists. Write code inside the function body to sort the ice-cream names in descending order, based on the average ratings of the respective ice-creams, and return the sorted ice-cream names as a list.

Example 1

Test input

choco butter vanilla cone pista
4.1 3.8 4.0 4.3 3.6

Test output

cone choco vanilla butter pista

Explanation

Zip the two lists
('choco', 4.1), ('butter', 3.8), ('vanilla', 4.0), ('cone', 4.3), ('pista', 3.6)

Sort these tuples based on the second element
('cone', 4.3), ('choco', 4.1), ('vanilla', 4.0), ('butter', 3.8), ('pista', 3.6)

Get the first elements from these tuples, and form a list
[cone, choco, vanilla, butter, pista]

Useful References

Python Program

# Complete the following function
# Return the sorted ice-creams
def sort_icecreams_by_rating(icecream_names, ratings):









# Boilerplate code - Do not edit the following
def main():
    icecream_names = [x for x in input().split()]
    ratings = [float(x) for x in input().split()]
    print(" ".join(sort_icecreams_by_rating(icecream_names, ratings)))

if __name__ == "__main__":
    main()

Testcase 1

choco butter vanilla cone pista
4.1 3.8 4.0 4.3 3.6
cone choco vanilla butter pista

Testcase 2

choco butter cone pista
4.1 4 3.9 4.2
pista choco butter cone

Testcase 3

choco
4.1
choco

Testcase 4

choco apple
4.1 5.0
apple choco
# Complete the following function
# Return the sorted ice-creams
def sort_icecreams_by_rating(icecream_names, ratings):
    # Zip names and ratings into a list of tuples
    name_ratings = zip(icecream_names, ratings)
    # Sort the list based on second element of tuple
    name_ratings = sorted(name_ratings, key=lambda x: x[1], reverse=True)
    # Get names from the sorted list
    sorted_names = [x[0] for x in name_ratings]
    return sorted_names

# Boilerplate code - Do not edit the following
def main():
    icecream_names = [x for x in input().split()]
    ratings = [float(x) for x in input().split()]
    print(" ".join(sort_icecreams_by_rating(icecream_names, ratings)))

if __name__ == "__main__":
    main()
show answer
main.py
⏵︎ Run Tests
Reset
Download
×
Code copied to clipboard successfully 👍