Skip to content

Instantly share code, notes, and snippets.

@haileys
Forked from jacobh/pretty-python.py
Created May 5, 2012 03:30
Show Gist options
  • Save haileys/2599382 to your computer and use it in GitHub Desktop.
Save haileys/2599382 to your computer and use it in GitHub Desktop.
OOPD 110 test 2
monthWeight m = do
putStr $ "input weight for month #" ++ show m ++ ": "
readLn
main = do
weights <- mapM monthWeight [1..12]
putStrLn $ "average weight is: " ++ (show $ sum weights `div` length weights)
months = []
for month in range(12):
weight = -1
while weight < 0:
weight = float(raw_input('input weight for month #%d: ' % (month+1)))
months.append(weight)
average = reduce(lambda x,y:x+y, months) / 12
print 'average weight is %f' % average
import io.*
class YearWeight {
public static void main (String[] args) {
monthOne = input("weight for month #1");
monthTwo = input("weight for month #2");
monthThree = input("weight for month #3");
monthFour = input("weight for month #4");
monthFive = input("weight for month #5");
monthSix = input("weight for month #6");
monthSeven = input("weight for month #7");
monthEight = input("weight for month #8");
monthNine = input("weight for month #9");
monthTen = input("weight for month #10");
monthEleven = input("weight for month #11");
monthTwelve = input("weight for month #12");
total = totalWeight(monthOne, monthTwo, monthThree, monthFour,
monthFive, monthSix, monthSeven, monthEight, monthNine, monthTen,
monthEleven, monthTwelve
);
average = averageWeight(total, 12);
System.out.println("average weight is " + average)
}
private static double input (String message) {
double weight;
do {
weight = ConsoleInput.readDouble(message);
} while (weight < 0);
return weight;
}
private static double totalWeight(double monthOne, double monthTwo,
double monthThree, double monthFour, double monthFive, double monthSix,
double monthSeven, double monthEight, double monthNine, double monthTen,
double monthEleven, double monthTwelve) {
return monthOne + monthTwo + monthThree + monthFour + monthFive +
monthSix + monthSeven + monthEight + monthNine + monthTen +
monthEleven + monthTwelve;
}
private static double averageWeight(double total, int length) {
return total / length;
}
}
def input(message, weight=-1):
while weight < 0:
print message
IMPORT weight
return weight
def totalWeight(monthOne...monthTwelve):
return monthOne + monthTwo + monthTwelve + monthFour + monthFive + monthSix
+ monthSeven + monthEight + monthNine + monthTen + monthEleven + monthTwelve
def average(total, length):
return total / length
def main:
monthOne = input('weight for #1')
monthTwo = input('weight for #2')
monthThree = input('weight for #3')
monthFour = input('weight for #4')
monthFive = input('weight for #5')
monthSix = input('weight for #6')
monthSeven = input('weight for #7')
monthEight = input('weight for #8')
monthNine = input('weight for #9')
monthTen = input('weight for #10')
monthEleven = input('weight for #11')
monthTwelve = input('weight for #12')
total = totalWeight(monthOne...monthTwelve)
avg = average(total, 12)
print "average weight is" + avg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment