help how to sort a list in order of 'n' in python without using inbuilt functions??

Carlos Nepomuceno carlosnepomuceno at outlook.com
Sat May 25 03:53:19 EDT 2013


----------------------------------------
> Date: Fri, 24 May 2013 23:05:17 -0700
> Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
> From: lokeshkoppaka at gmail.com
> To: python-list at python.org
[...]
> ya steven i had done the similar logic but thats not satisfying my professor
> he had given the following constrains
> 1. No in-built functions should be used
> 2. we are expecting a O(n) solution
> 3. Don't use count method

That's trivial!

# l is a list of numbers: 0,1,2
def order_n(l):
    r = []
    count = [0]*3
    count[2] = len(l)
    for i in range(len(l)):
        count[1] += abs((l[i]>0)-1)
        r.insert(count[l[i]], l[i])
    return r

'count' is a list I've defined to count the quantity of the elements in the argument, not the method.

I don't think it needs any explanations, but if you have any doubts just ask. ;)

Good luck! 		 	   		  


More information about the Python-list mailing list