Skip to content

Instantly share code, notes, and snippets.

@grundprinzip
Created February 21, 2010 14:20
Show Gist options
  • Save grundprinzip/310335 to your computer and use it in GitHub Desktop.
Save grundprinzip/310335 to your computer and use it in GitHub Desktop.
class Cost:
def __init__(self, val, men=1, hours=1):
self.men = men
self.hours = hours
self.value = val
def cost_per_hour(self):
return float(self.value) / (self.men * self.hours)
def men_hours(self):
return self.men * self.hours
def better_package(self, other):
return self.men_hours() > other.men_hours() and self.value <= other.value
def __str__(self):
return "<Cost: men=%d hours=%d cost=%d>" %(self.men, self.hours, self.value)
def __cmp__(self, other):
if self.value < other.value:
return -1
elif self.value == other.value:
return 0
else:
return 1
def qtio(self):
return float(self.value) / self.men_hours()
def better_cmp(self, other):
if self.men_hours() < other.men_hours():
return -1
if self.men_hours() > other.men_hours():
return 1
if self.value < other.value:
return -1
elif self.value > other.value:
return 1
return 0
if __name__ == "__main__":
import sys
packages = [ Cost(145,2,3), Cost(185,2,4), Cost(245,2,5), Cost(395,2,8),
Cost(175,3,3), Cost(235,3,4), Cost(315,2,5), Cost(495,2,8),
Cost(225,4,3), Cost(315,4,4), Cost(395,2,5), Cost(595,2,8),
Cost(275,5,3), Cost(355,5,4), Cost(495,2,5), Cost(695,2,8) ]
abc = sorted(packages, lambda a,b: a.better_cmp(b))
for p in abc:
print "%d mh for %d EUR (%s)" % (p.men_hours(), p.value, p)
print "\n\n\nNow detailed package comparison\n"
for p in packages:
res = filter(lambda x: x.better_package(p), packages)
if len(res) > 0:
print "Better packages found, instead of %d for %d m/h" % (p.value, p.men_hours())
for b in sorted(res):
print "\t* %s (%d, %d m/h)" % (b, b.value, b.men_hours())
else:
print "No better package found, use this %s (%d, %d m/h)" % (p, p.value, p.men_hours())
6 mh for 145 EUR (<Cost: men=2 hours=3 cost=145>)
8 mh for 185 EUR (<Cost: men=2 hours=4 cost=185>)
9 mh for 175 EUR (<Cost: men=3 hours=3 cost=175>)
10 mh for 245 EUR (<Cost: men=2 hours=5 cost=245>)
10 mh for 315 EUR (<Cost: men=2 hours=5 cost=315>)
10 mh for 395 EUR (<Cost: men=2 hours=5 cost=395>)
10 mh for 495 EUR (<Cost: men=2 hours=5 cost=495>)
12 mh for 225 EUR (<Cost: men=4 hours=3 cost=225>)
12 mh for 235 EUR (<Cost: men=3 hours=4 cost=235>)
15 mh for 275 EUR (<Cost: men=5 hours=3 cost=275>)
16 mh for 315 EUR (<Cost: men=4 hours=4 cost=315>)
16 mh for 395 EUR (<Cost: men=2 hours=8 cost=395>)
16 mh for 495 EUR (<Cost: men=2 hours=8 cost=495>)
16 mh for 595 EUR (<Cost: men=2 hours=8 cost=595>)
16 mh for 695 EUR (<Cost: men=2 hours=8 cost=695>)
20 mh for 355 EUR (<Cost: men=5 hours=4 cost=355>)
Now detailed package comparison
No better package found, use this <Cost: men=2 hours=3 cost=145> (145, 6 m/h)
Better packages found, instead of 185 for 8 m/h
* <Cost: men=3 hours=3 cost=175> (175, 9 m/h)
Better packages found, instead of 245 for 10 m/h
* <Cost: men=4 hours=3 cost=225> (225, 12 m/h)
* <Cost: men=3 hours=4 cost=235> (235, 12 m/h)
Better packages found, instead of 395 for 16 m/h
* <Cost: men=5 hours=4 cost=355> (355, 20 m/h)
No better package found, use this <Cost: men=3 hours=3 cost=175> (175, 9 m/h)
No better package found, use this <Cost: men=3 hours=4 cost=235> (235, 12 m/h)
Better packages found, instead of 315 for 10 m/h
* <Cost: men=4 hours=3 cost=225> (225, 12 m/h)
* <Cost: men=3 hours=4 cost=235> (235, 12 m/h)
* <Cost: men=5 hours=3 cost=275> (275, 15 m/h)
* <Cost: men=4 hours=4 cost=315> (315, 16 m/h)
Better packages found, instead of 495 for 16 m/h
* <Cost: men=5 hours=4 cost=355> (355, 20 m/h)
No better package found, use this <Cost: men=4 hours=3 cost=225> (225, 12 m/h)
No better package found, use this <Cost: men=4 hours=4 cost=315> (315, 16 m/h)
Better packages found, instead of 395 for 10 m/h
* <Cost: men=4 hours=3 cost=225> (225, 12 m/h)
* <Cost: men=3 hours=4 cost=235> (235, 12 m/h)
* <Cost: men=5 hours=3 cost=275> (275, 15 m/h)
* <Cost: men=4 hours=4 cost=315> (315, 16 m/h)
* <Cost: men=5 hours=4 cost=355> (355, 20 m/h)
* <Cost: men=2 hours=8 cost=395> (395, 16 m/h)
Better packages found, instead of 595 for 16 m/h
* <Cost: men=5 hours=4 cost=355> (355, 20 m/h)
No better package found, use this <Cost: men=5 hours=3 cost=275> (275, 15 m/h)
No better package found, use this <Cost: men=5 hours=4 cost=355> (355, 20 m/h)
Better packages found, instead of 495 for 10 m/h
* <Cost: men=4 hours=3 cost=225> (225, 12 m/h)
* <Cost: men=3 hours=4 cost=235> (235, 12 m/h)
* <Cost: men=5 hours=3 cost=275> (275, 15 m/h)
* <Cost: men=4 hours=4 cost=315> (315, 16 m/h)
* <Cost: men=5 hours=4 cost=355> (355, 20 m/h)
* <Cost: men=2 hours=8 cost=395> (395, 16 m/h)
* <Cost: men=2 hours=8 cost=495> (495, 16 m/h)
Better packages found, instead of 695 for 16 m/h
* <Cost: men=5 hours=4 cost=355> (355, 20 m/h)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment