Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
February 7th, 2013, 05:01 PM
 laffytaffykidd
Registered User

Join Date: Feb 2013
Posts: 2
Time spent in forums: 32 m 12 sec
Reputation Power: 0
Outputting Maximum and Minimum Values

The expected results:

2012 vs. 1980s
March coverage fraction 0.969491525424
September coverage fraction 0.476319350474
2012 vs. 1990s
March coverage fraction 0.990990990991
September coverage fraction 0.527736131934
2012 vs. 2000s
March coverage fraction 1.01203113942
September coverage fraction 0.628571428571
Max winter coverage loss 0.969491525424
Max summer coverage loss 0.476319350474

What I get:
2012 vs. 2000s
March coverage fraction 0.969491525424
2012 vs. 1990s
March coverage fraction 0.990990990991
2012 vs. 2000s
March coverage fraction 1.01203113942
Max winter coverage loss 0.969491525424

Question:
Using only one function with four parameters, how can I get the September print statements right after the March statements?
My original idea was to call the function again with the parameters (s1980, s1990, s2000, s2012) but that gave me the wrong results!

My code:

Code:
#!/usr/bin/python3

m1980 = 14.75
m1990 = 14.43
m2000 = 14.13
m2012 = 14.30

s1980 = 7.39
s1990 = 6.67
s2000 = 5.60
s2012 = 3.52

def march_frac_years(w, x, y, z):

print '2012 vs. 2000s'
m2012_1980 = z/w
print '  March coverage fraction ' + str(m2012_1980)

print '2012 vs. 1990s'
m2012_1990 = z/x
print '  March coverage fraction ' + str(m2012_1990)

print '2012 vs. 2000s'
m2012_2000 = z/y
print '  March coverage fraction ' + str(m2012_2000)

max_winter = min(m2012_1980, m2012_1990, m2012_2000)
print 'Max winter coverage loss ' + str(max_winter)

march_frac_years(m1980, m1990, m2000, m2012)

#2
February 8th, 2013, 10:09 AM
 dwblas
Contributing User

Join Date: May 2009
Posts: 294
Time spent in forums: 3 Days 18 h 53 m 55 sec
Reputation Power: 7
One way, and it requires some tweaking yet
Code:
def frac_years(numer, denom, ctr, month):
if ctr < 3:
result = numer/denom
print '  %s coverage fraction %f' % (month, result)
return result

if 3 == ctr:
max_winter = min(numer, denom, month)
print 'Minimum winter coverage loss ' + str(max_winter)

lits_list = ['2012 vs. 1980s', '2012 vs. 1990s', '2012 vs. 2000s']
march = [14.75, 14.43, 14.13, 14.30]
sep = [7.39, 6.67, 5.60, 3.52]
march_results=[]
sep_results=[]
for ctr in range(3):
print lits_list[ctr]
result=frac_years(march[ctr], march[3], ctr, "March")
march_results.append(result)
result=frac_years(sep[ctr], sep[3], ctr, "September")
sep_results.append(result)
print
frac_years(march_results[0], march_results[1], 3, march_results[2])
frac_years(sep_results[0], sep_results[1], 3, sep_results[2])

Last edited by dwblas : February 8th, 2013 at 10:12 AM.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Outputting Maximum and Minimum Values