User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 374,198 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,557 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser:
Views: 268 | Replies: 4
Reply
Join Date: Apr 2008
Posts: 30
Reputation: Mackjan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Mackjan Mackjan is offline Offline
Light Poster

Special sort a list

  #1  
May 8th, 2008
How can i create a function which sort a list in this way for example if list 4,4,2
4,4,2
3,3,1,3
2,2,0,2,4
1,1,0,1,3,4
0,0,0,0,2,3,4
0,0,0,0,0,1,2,3,4
If the list is impossible to sort then the program have to try it 15 times not more.
The function which I wrote does it only one time not more??
x=[4,4,2]
def kopi(x):
    b=0
    y = []
    for j in x:
        b+=1
        y.append(j-1)
    y.append (b)
    print y,b 
    return y
can anyone help me?
Thanks a lot for all help and advice.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 71
jrcagle jrcagle is offline Offline
Posting Pro

Re: Special sort a list

  #2  
May 8th, 2008
I'm not following your example.
Reply With Quote  
Join Date: Apr 2008
Posts: 30
Reputation: Mackjan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Mackjan Mackjan is offline Offline
Light Poster

Re: Special sort a list

  #3  
May 9th, 2008
Thanks alot
I try to explain.
I have a list x [4,4,2]
The next step program dose [4-1,4-1,2-1] and we put these value in b now b = 3,we create a new list mylist = [ 3,3,1] + b  mylist = [3,3,1,3].
Now mylist = [3-1,3-1,1-1] + b  mylist [2,2,0,3]
Next step……….
Next step…………….

I have an idea but don’t know how implement it. I must use the same list all the time but print only the new elements which appends to mylist in every step.
For ex:
First print 4,4,2 --> 4 4 2
Second print 3,3,1,3 --> 4 4 2 3 3 1 3
Thread print 2,2,0,2,4 --> 4 4 2 3 3 1 3 2 2 0 2 4
New elements content digits with red color and we print only them.
I am not sure if it is a good idea ??
Reply With Quote  
Join Date: Dec 2006
Posts: 378
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 52
woooee woooee is offline Offline
Posting Whiz

Re: Special sort a list

  #4  
May 9th, 2008
Some modifications to your code. First you have to test for zero, and second, the variable "b" is not necessary as you can just use len(x). Hopefully this is what you want to do.
def kopi(x):
    y = []
    for num in x:
        new_num = num - 1
        if new_num < 0:
           new_num=0
        print new_num,
        y.append(new_num)
    y.append (len(x))
    print len(x)
    print "new list inside function", y
    return y

##   15 iterations
if __name__ == "__main__":
   x=[4,4,2]
   print "the beginning list is", x
   for k in range(0, 15):
      x=kopi(x)
      print "the list is now", x, "\n"    
Last edited by woooee : May 9th, 2008 at 10:52 am.
Reply With Quote  
Join Date: Apr 2008
Posts: 30
Reputation: Mackjan is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Mackjan Mackjan is offline Offline
Light Poster

Re: Special sort a list

  #5  
May 10th, 2008
Hi and thank you very much.
What is this if __name__ == "__main__": in your code?
It was some thing new for me and what dose it?
I try to make a card game, the user put in a list for example [3,2,6,,9] or [4,4,2] or ……
The program must arrange it in this way:
We take list [4,4,2]
You wrote about len(x) instead of b it is a good idea but dosen´t work here.
The programme subtract one from every element and put it in a new element like that:
• List [4-1,4-1,2-1] --> newlist= [3,3,1,3]
• 3,3,1,3
• 2,2,0,2,4 --> (len(x)=5, but we can subtract 1 from only 4 elements which have a value >0 we can not subtract 1 from an element which value is <= 0) and it is important to delete the element which content zero like this:
• 2, 2, ___ ,2,4 --> 2,2,2,4
• 1,1,1,3,4
• 0,0,0,2,3,4
• 0,0,0,0,1,2,3,4
• 0,0,0,0,0,1,2,3,4 ( I delete all zero and reverse sort my list then I have 4,3,2,1 and game is over)
In this way we work only with digits from 1 to 4 not more, len(x) count all elements which content zero …etc. that’s why we go over 4 and programme can´t sort cards as we want.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Python Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 5:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC