Skip to content

Instantly share code, notes, and snippets.

@stefan2904
Created December 6, 2013 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefan2904/7833872 to your computer and use it in GitHub Desktop.
Save stefan2904/7833872 to your computer and use it in GitHub Desktop.
the worst program I ever wrote
import sys
# 10 x 20 x 30
# a * b * c = 10 * 20 * 30
# a : b : c = 10 : 20 : 30
# a : b = 10 : 20 => b = a * 20
# a : c = 10 : 30 => c = a * 30
# b : c = 20 : 30 => b = c * 20/30
# legal operations:
## scale down: divide by cubic root of factor of 2: x / (y**(1/3.0))
## scale up: multiply by factor of 2: x * (y**(1/3.0))
def f(x):
return str(("%.2f" % x).rstrip('0').rstrip('.'))
standard = [10, 20, 30]
volumen = reduce(lambda x, y: x * y, standard)
for line in sys.stdin:
v = []
factor = 1
v = map(lambda x: float(x), line.strip().split('x'))
v.sort()
#print v
for i in range(0, len(v)):
vi = v[i]
sif = standard[i]*factor
#print "does", vi, "fit in", sif
if vi > sif:
j = 1
while vi > sif*(j**(1/3.0)): j *= 2
#j /= 2
#print " scale package up by factor", j
factor *= (j**(1/3.0))
elif vi < sif and i == 0:
j = 1
while vi < sif/(j**(1/3.0)): j *= 2
j /= 2
#print " scale package down by factor", j
factor = 1/(j**(1/3.0))
#else:
#print " do nothing"
#print "="
erg = map(lambda x: x * factor, standard)
#print reduce(lambda x, y: str(x) + "x" +str(y), erg)
print ("%sx%sx%s") % (f(erg[0]), f(erg[1]), f(erg[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment