Created
August 22, 2009 20:45
-
-
Save sirupsen/172979 to your computer and use it in GitHub Desktop.
Which language is best at taking all the integer paraments and get the average? Look for yourself!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def average(*numbers) | |
((numbers.inject {|sum, element| sum + element}) / numbers.length).to_f | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdarg.h> | |
double average(int count, ...) | |
{ | |
va_list ap; | |
int j; | |
double tot = 0; | |
va_start(ap, count); | |
for(j=0; j<count; j++) | |
tot+=va_arg(ap, double); | |
va_end(ap); | |
return tot/count; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double Avg(params double[] a) | |
{ | |
return a.Sum() / a.Length; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function average(){ | |
numbers=average.arguments; | |
sum=0; | |
for(var i=0; i<numbers.length; i++){ | |
sum+=numbers[i] | |
}; | |
return sum/numbers.length; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static double average(int... numbers) { | |
// Needs Java 1.5 | |
double count = 0; | |
double sum = 0; | |
for(int num: numbers) { | |
count++; | |
sum += num; | |
} | |
return sum / count; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun average (&rest numbers) | |
(/ (apply #'+ numbers)(length numbers))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function average (...) | |
sum = 0 | |
for _,n in ipairs({...}) do sum = sum + n end | |
return sum / #{...} | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Perl | |
sub Average | |
{ | |
return sum(@_)/scalar(@_); | |
} | |
# ASM x86 | |
average: | |
pop cl | |
mov ax, 0 | |
mov ch, cl | |
.addnext: | |
cmp cl, 0 | |
je .div | |
pop bx | |
add ax, bx | |
dec cl | |
jmp .addnext | |
.div: | |
div ax, ch | |
push ax | |
ret |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function average() { | |
return floatval(array_sum(func_get_args())/func_num_args()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def average(*numbers): | |
return float(sum(numbers))/len(numbers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment