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.

I'm not following your example.

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 ??

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"

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.