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

L

lokeshkoppaka

i need to write a code which can sort the list in order of 'n' without use builtin functions
can anyone help me how to do?
 
D

Dave Angel

i need to write a code which can sort the list in order of 'n' without use builtin functions
can anyone help me how to do?

You could sort, but you couldn't print out the results, so what's the
point? In Python 3.3 at least, print() is a built-in function.

Is the homework assignment more clearly worded than your summary? And
if so, how far have you gotten on it?
 
C

Carlos Nepomuceno

lol wtf?

If 'n' is the quantity of elements to be sorted there's
no way you can write an algorithm with complexity O(n) for the worst
case not knowing something special about the data.

For example, Quicksort will give you O(n*(log(n)) on average case (O(n^2)in the worst case).

You gotta be much more specific than that if you really need an O(n) code.

There's probably assumptions you didn't mention about the data if O(n) it'sreally what you're looking for.

PS: Looks like a joke as stated..

----------------------------------------
 
L

lokeshkoppaka

i need to write a code which can sort the list in order of 'n' without use builtin functions

can anyone help me how to do?

Note:
the list only contains 0's,1's,2's
need to sort them in order of 'n'
 
C

Chris Angelico

Note:
the list only contains 0's,1's,2's
need to sort them in order of 'n'

In that case, you're not really ordering them, you're counting them.
Look at the collections module; you can very easily figure out how
many of each there are, and then reconstruct the list afterward.

ChrisA
 
L

lokeshkoppaka

In that case, you're not really ordering them, you're counting them.

Look at the collections module; you can very easily figure out how

many of each there are, and then reconstruct the list afterward.



ChrisA

but i need to do it with out using builtin functions
 
C

Chris Angelico

but i need to do it with out using builtin functions

Depending on your definitions, that's either trivially easy (the
'collections' module is not builtin functions) or completely
impossible. Have fun.

ChrisA
 
S

Steven D'Aprano

but i need to do it with out using builtin functions


How would you, an intelligent human being count them?

Describe how you would count them in English, or whatever your native
language is.

"First I would start a tally for the number of zeroes, tally = 0. Then I
look at each item in turn, and if it is 0, I add one to the tally. When I
get to the end, I the number of zeroes is equal to the tally.

Then I do the same for the number of ones, and the number of twos."

Now turn that into Python code.

tally = 0
for item in list_of_items:
if item == 0:
tally = tally + 1

print "The number of zeroes equals", tally
 
L

lokeshkoppaka

How would you, an intelligent human being count them?



Describe how you would count them in English, or whatever your native

language is.



"First I would start a tally for the number of zeroes, tally = 0. Then I

look at each item in turn, and if it is 0, I add one to the tally. When I

get to the end, I the number of zeroes is equal to the tally.



Then I do the same for the number of ones, and the number of twos."



Now turn that into Python code.



tally = 0

for item in list_of_items:

if item == 0:

tally = tally + 1



print "The number of zeroes equals", tally

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
 
C

Chris Angelico

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

And now you finally admit that it's homework.

Solve it yourself, or go to your prof and say that you need help.

ChrisA
 
C

Carlos Nepomuceno

----------------------------------------
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: (e-mail address removed)
To: (e-mail address removed) [...]
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>0)-1)
        r.insert(count[l], l)
    return r

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

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

Good luck!
 
C

Carlos Nepomuceno

----------------------------------------
Date: Sat, 25 May 2013 18:28:32 +1000
Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
From: (e-mail address removed)
To: (e-mail address removed)

----------------------------------------
Date: Fri, 24 May 2013 23:05:17 -0700
1. No in-built functions should be used
count[2] = len(l)

Fail! :)

ChrisA

lol I forgot to include this monkey patch! ;)

def length(l):
    x=0
    y=l[:]
    while y:
        x+=1
        y.pop()
    return x
 
C

Chris Angelico

----------------------------------------
lol I forgot to include this monkey patch! ;)

def length(l):
x=0
y=l[:]
while y:
x+=1
y.pop()
return x

Nice. Now eliminate abs (easy) and range. :)

ChrisA
 
C

Carlos Nepomuceno

lol

def absolute(x):
    return x if x>0 else -x

def reach(x):
    y=[]
    z=0
    while z<x:
        y.append(z)
        z+=1
    return y

----------------------------------------
Date: Sat, 25 May 2013 18:47:24 +1000
Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
From: (e-mail address removed)
To: (e-mail address removed)

----------------------------------------
lol I forgot to include this monkey patch! ;)

def length(l):
x=0
y=l[:]
while y:
x+=1
y.pop()
return x

Nice. Now eliminate abs (easy) and range. :)

ChrisA
 
C

Chris Angelico

lol

def absolute(x):
return x if x>0 else -x

def reach(x):
y=[]
z=0
while z<x:
y.append(z)
z+=1
return y

Very good. You are now in a position to get past the limitations of a
restricted-environment eval/exec. Avoiding builtins is actually a fun
skill to hone.

ChrisA
 
C

Carlos Nepomuceno

----------------------------------------
Date: Sat, 25 May 2013 19:01:09 +1000
Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
From: (e-mail address removed)
To: (e-mail address removed) [...]
Very good. You are now in a position to get past the limitations of a
restricted-environment eval/exec. Avoiding builtins is actually a fun
skill to hone.

ChrisA


I'm glad he didn't ask for a Pseudo-RNG without built-ins! ;)
 
C

Chris Angelico

----------------------------------------
Date: Sat, 25 May 2013 19:01:09 +1000
Subject: Re: help how to sort a list in order of 'n' in python without using inbuilt functions??
From: (e-mail address removed)
To: (e-mail address removed) [...]
Very good. You are now in a position to get past the limitations of a
restricted-environment eval/exec. Avoiding builtins is actually a fun
skill to hone.

ChrisA


I'm glad he didn't ask for a Pseudo-RNG without built-ins! ;)

def random_number():
return 7

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top