We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Method Chaining

Difficulty: Easy


Problem Description

Write a solution to list the names of animals that weigh strictly more than 100 kilograms. Return the animals sorted by weight in descending order.


Key Insights

  • The task requires filtering a DataFrame based on weight.
  • Method chaining allows for compact and readable code to perform multiple operations in one line.
  • The final output needs to be sorted in descending order.

Space and Time Complexity

Time Complexity: O(n log n) - due to the sorting operation. Space Complexity: O(n) - for storing the filtered results.


Solution

The solution involves filtering the DataFrame to include only those animals whose weight is greater than 100 kg. Following the filtering, the results are sorted by weight in descending order. Finally, we select only the name column for the output. The data structure used here is a DataFrame, which allows for easy manipulation of tabular data.


Code Solutions

# Using Pandas to filter and sort the DataFrame
result = animals[animals['weight'] > 100].sort_values(by='weight', ascending=False)['name']
← Back to All Questions