Write a function sorting(L).

Tim Chase python.list at tim.thechases.com
Fri Apr 21 17:21:34 EDT 2017


On 2017-04-21 12:58, Mohammed Ahmed wrote:
> Write a function sorting(L) that takes a list of numbers and
> returns the list with all elements sorted in ascending order.
> Note: do not use the sort built in function
> 
> it is a python question

No "sort" functions here...

  >>> lst=[3,1,4,1,5,9,2,6,5]
  >>> getattr(__builtins__, dir(__builtins__)[-9])(lst)
  [1, 1, 2, 3, 4, 5, 5, 6, 9]

Might have to tweak it based on your version of Python.

For a more robust version:

  >>> import heapq
  >>> heapq.heapify(lst)
  >>> heapq.nsmallest(len(lst), lst)
  [1, 1, 2, 3, 4, 5, 5, 6, 9]

No built-in sort there either. ;-)

Or, you could show your code instead of trying to get the mailing
list to do your homework for you.

-tkc






More information about the Python-list mailing list