•
•
•
•
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
![]() |
•
•
Join Date: Apr 2008
Posts: 30
Reputation:
Rep Power: 1
Solved Threads: 0
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??
can anyone help me?
Thanks a lot for all help and advice.
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 yThanks a lot for all help and advice.
•
•
Join Date: Apr 2008
Posts: 30
Reputation:
Rep Power: 1
Solved Threads: 0
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 ??
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 ??
•
•
Join Date: Dec 2006
Posts: 378
Reputation:
Rep Power: 2
Solved Threads: 52
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.
•
•
Join Date: Apr 2008
Posts: 30
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
Similar Threads
- explorer.exe uses 99% CPU (Viruses, Spyware and other Nasties)
- adding & subtracting objects from a list (C++)
- Open In New Window Php (PHP)
- cam ActiveX control? (C++)
- Quicksorting linked list - simple algorithm (C)
- Please someone give me some good advice asap please (Advertising Sales Strategies)
- please help me with the last bit?? (C)
- Heeeeeeeeeeeeeeeeelp pleeeaaazzzz :cry: :cry: :cry: (C)
Other Threads in the Python Forum
- Previous Thread: Trouble with a certain Exception
- Next Thread: Python interaction with firefox?


Linear Mode