Hone logo
Hone
Problems

Python List Sorting: The Ordered Sequence Challenge

Your task is to implement a function that sorts a given list of numbers in ascending order. This is a fundamental operation in computer science, essential for organizing data, improving search efficiency, and forming the basis of many more complex algorithms.

Problem Description

You need to create a Python function called sort_list that accepts a single argument: a list of integers. The function should return a new list containing the same integers as the input list, but sorted in ascending order. The original input list should not be modified.

Key Requirements:

  • The function must be named sort_list.
  • It must accept one argument: a list of integers.
  • It must return a new list.
  • The returned list must be sorted in ascending order.
  • The original input list must remain unchanged.

Expected Behavior:

If the input list contains numbers, they should be arranged from smallest to largest in the output list.

Edge Cases to Consider:

  • An empty input list.
  • A list containing only one element.
  • A list with duplicate elements.
  • A list that is already sorted.
  • A list sorted in descending order.

Examples

Example 1:

Input: [5, 2, 8, 1, 9]
Output: [1, 2, 5, 8, 9]
Explanation: The input list is sorted from the smallest element (1) to the largest element (9). The original list is not modified.

Example 2:

Input: [10, -3, 0, 7, -5, 2]
Output: [-5, -3, 0, 2, 7, 10]
Explanation: The function handles negative numbers correctly and sorts them in ascending order.

Example 3:

Input: []
Output: []
Explanation: An empty input list should result in an empty output list.

Example 4:

Input: [3, 3, 1, 2, 1, 2]
Output: [1, 1, 2, 2, 3, 3]
Explanation: Duplicate elements are preserved and sorted correctly.

Constraints

  • The input list will contain only integers.
  • The input list can have between 0 and 1000 elements, inclusive.
  • The values of the integers will be between -1000 and 1000, inclusive.
  • Your solution should aim for a reasonable time complexity. While Python's built-in sorted() function is highly optimized, try to understand the underlying logic of sorting.

Notes

This challenge is designed to test your understanding of fundamental list manipulation and the concept of sorting. You can leverage Python's built-in sorting capabilities or implement a sorting algorithm yourself. However, for this challenge, using sorted() is perfectly acceptable and encouraged to focus on the core requirement of returning a new, sorted list. Remember to ensure that the original list is not altered.

Loading editor...
python