Follow us on Twitter!
You cannot teach a man anything; you can only help him find it within himself. - Galileo
Friday, April 25, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 17
Guests Online: 15
Members Online: 2

Registered Members: 82906
Newest Member: ilija
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python 2.6.4 - Integer list operations


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 06-06-10 05:15
I need to find a way to compare a number of integers that have been assigned to variables, then drop the lowest out of the list. Here's an example of what I've got.

Code
d1 = random.randint(1,10)
d2 = random.randint(1,5)
d3 = 5 + d1 - d2




After, I need a line immediately after that that will combine d1, d2 and d3 (addition) then figure out which is the lowest and drop it, or one of them if there are multiples of the same value. (or the other way around, drop the lowest and combine the rest)

I attempted this:
Code
dr1 = min(list('d1''d2''d3'))
s1 = d1 + d2 + d3 - dr1



But the "min(list())" outputs a string, and we all know that "integer + string = error"

Is there a way to make a array/pool/list of integers and then either compare them all and select the lowest, or just drop the lowest from it then total.

This is just a small example. I had thought using "compare()", but the large pools would have too many compare() functions to code efficiently.


Author

RE: Python 2.6.4 - Integer list operations


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 06-06-10 16:58
Hmm.... well I see a few things that could be revised:

Code

dr1 = min(list('d1''d2''d3'))





probably should be

Code

dr1 = min([d1,d2,d3])





list() takes only one argument, a sequence type such as a tuple, and converts it to a list. Additionally, enclosing the variables in ' ' will probably result in a conversion to strings. (i'm a little surprised that didnt error...)
using [] will create a list and then min, will operate properly (as the object it was handed previously was wrong)

I'm a little bit confused in exactly what you're trying to do here, but here's your code re-written to do what i think you're getting at. With debugging and output:

Code

import random

d1 = random.randint(1,10)
d2 = random.randint(1,5)
d3 = 5 + d1 - d2

print "d1: %s" % d1
print "d2: %s" % d2
print "d3: %s" % d3

dr1 = min([d1,d2,d3])
print "dr1: %s" % dr1
s1 = d1 + d2 + d3 - dr1
print "s1: %s" % s1

--- output ---
d1: 1
d2: 4
d3: 2
dr1: 1
s1: 6





~samurai


Author

RE: Python 2.6.4 - Integer list operations


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 06-06-10 19:59
Thanks. Just what I needed. Works like a charm.