Join us at IRC!
Society leans ever heavily on computers, if you have the power to take out computers you can take out society. - cubeman372
Friday, May 25, 2012
Navigation
Members Online
Total Online: 37
Web Spiders: 14
Guests Online: 37
Members Online: 0

Registered Members: 70209
Newest Member: KalareShou
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python 2.6.4 - Integer list operations

FallFromINFINITY
Member

Posts: 9
Location:
Joined: 23.03.09
Rank:
Apprentice
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.

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:
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.


--------------------------------------------------------------------
Who is "General Error" and why is he reading my harddisk?
FallFromINFINITY@hotmail.com FallFromINFINITY
Author

RE: Python 2.6.4 - Integer list operations

only_samurai
[IRC Rockstar]

Posts: 984
Location: idling in some random irc channel
Joined: 18.08.06
Rank:
.|unranked|.
Posted on 06-06-10 16:58
Hmm.... well I see a few things that could be revised:


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


probably should be


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:


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


The problem with a fool-proof system, is eliminating the fool.

"His name is Cereal Killer...Like Fruitloops."
If you cut me, I bleed binary.

http://blog.psych0tik.net/
http://blog.psych0tik.net
Author

RE: Python 2.6.4 - Integer list operations

FallFromINFINITY
Member

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


--------------------------------------------------------------------
Who is "General Error" and why is he reading my harddisk?
FallFromINFINITY@hotmail.com FallFromINFINITY
Guest
Username

Password

Remember Me


Bookmark This Page
Affiliates
Adverts

 

 

Links
By using, viewing or obtaining any information contained on this site, you agree to the disclaimer.

© HellBound Hackers 2008- 2009. Since 3rd December 2004.