I made a mean function in Python

I made a mean function in Python, it's all yours friend. Prove you can do it better, without using a library.
def mean(list):
sum = 0
for x in list:
sum = sum + x
return (sum/len(list))

Other urls found in this thread:

en.wikipedia.org/wiki/Generalized_mean
twitter.com/SFWRedditGifs

>two iterations over the input
see me after class

def mean(list):
return sum(list)/len(list)

I never saw how basic that math was until now.

len is O(1) for Python lists.

Thank you, I didn't even know sum is built in.

So it's constant? Plz explain big O, don't be mean

The pic? That's at least engineering level. The mean? Yeah it is

import timeit
import random

somenums = [random.randint(1,999999) for x in xrange(1000)]

def mean():
sum = 0
for x in somenums:
sum = sum + x
return (sum/len(somenums))

def mean2():
return sum(somenums)/len(somenums)

print timeit.timeit(mean,number=50000)
print timeit.timeit(mean2,number=50000)

2.22395437696
0.405432454195

>So it's constant?
yes.
>Plz explain big O
no, go google it.

>That's at least engineering level.
you learn all of that in highschool in north america

I'm better because i'm not a PySoyBoy
mean(){
$(($(echo ${list[@]} `seq -f "+" -s ' ' $((${#list[@]} - 1))` p | dc) / ${#list[@]}))
}

You learned the integral of 1/cos^2x in highschool at normal level math? Lol okay

i mean it's been a while, but yeah, integral and differential calculus are taught in the last year of highschool. i guess it's an optional class, so maybe you don't count that? i will say i can't guarantee you're taught to derive all those integrals, but IIRC you're at least given them as facts.

so list isn't a list?
>dynamic typing

def mean(nums):
return sum(nums)/max(len(nums),1)


i forgot to add this for empty lists

why does maintaining a length attribute make something not a list in your mind?

Idk I didn't get into that stuff until college. In my highschool, anything pic related is AP level.

brainlet detected

def mean(list):
print "You are a pathetic cunt"

Cranky nerd detected

Same for Denmark. Maybe not cos^2. But it should depend on the teacher as well here the teacher has two optional "projects". So we also did Taylor polynomials, brief work with imaginary numbers to understand that famous e^(i Pi) + 1 = 0, simple differential equations and integrals in 3D space (whatever that is called in English). We had a good teacher

/thread
Python is a disgusting language

Do you have unit tests for this?

How did he manage to implement this? I'm very confused

Are you fucking kidding me?

>basic trigonometry and calculating the areal beneath the curve and some integration is hard
Everybody learned this in high school, even in my shitty Yuropoor country with subpar education and math skills. You either didn't pay attention or you're some elaborate troll.

>python2

>Using two (2) '
Barbarian

class MeanTest(unittest.TestCase):
def test(self):
self.assertFeelingsHurt(mean(None))

Yes obviously. 8 hours math here though. We also learned about the Riemann Hypothesis, fractals, the central limit theorem and solving Google Pagerank matrix with eigenvectors. To be fair my highschool math teacher was a PhD in Algebraic Structures. Also I'm from belgium where we have actually have highschools that are non-shitty.

Italy here
Touched almost differential equation on last year of calcus.
US cucks can't even imagine

Dude that's some basic shit

Here's how you do it in Groovy:

[list].sum()/[list].size()

yes

Nothing special about that. In Eastern Europe in the last year of school we're introduced to integration as well as limits, matrices and imaginary numbers (just so we know what that is).

Next time make a nice function desu.

>hard coding the 1-mean
top pleb

def mean(list, p=1):
if(p==0):
sum=1.0
for x in list:
sum = sum * x;
return sum**(1.0/len(list))
sum = 0.0
for x in list:
print(x, " : ", sum);
sum = sum + x**p
return (sum/len(list))**(1.0/p)


en.wikipedia.org/wiki/Generalized_mean

You managed to overwrite two builtins in 5 lines of code. Next time use `int` as looping variable for maximum pajeet

>tg for tan

what kind of massive faggot does that?

1/cos^2(x) = sec^2(x) which is the derivative of tan(x)

>this kills the Sup Forums

>what kind of massive faggot does that?
Probably the same kind of faggots that uses lg for log

>what kind of massive faggot does that?

Only white people are tan therefore it reenforces white supremacy and body shaming. Of course we would stop using such problematic names in 2018.

>Only white people are tan
mfw

def succ (x):
y = x + 1
return y

def add (x, y):
z = x + y
return z

def mean (xs):
sum = 0
count = 0
for x in xs:
sum = add (sum, x)
count = succ (count)
m = sum / count
return m

American education system is far worse than Europoor countries'

Not my country, we consistently rank lower than the US on the TIMSS. That's what happen when social democrats rule supreme for too long and tell kids to follow their dreams rather than "do good in school and you can do whatever"

Canadian here.

It was taught in my second last year of maths

Asians are white as far as sjw are concerned

I had to read it 5 minutes to understand it.

(grounded in reality) Fear of coding like a math-less pajeet is what stops me from learning programming. I wouldn't be able to write so nicely myself. The best I ever made was a mess of if and else statements and apparently it's a bad way of coding.

>12 year old: the post
wtf my son is ninth grade and he has a test tomorrow about it.

>succ

Yes for the NL

On purpose.

I doubt a ninth grader is learning integrals, but the rest of the stuff checks out

let mean zero add div l =
let rec loop sum count = function
| [] -> sum, count
| x :: xs -> loop (add sum x) (succ count) xs in
let sum, count = loop zero 0 l in
div sum count
;;

let mean_floats =
let zero = 0.0 in
let add sum x = sum +. x in
let div sum count = sum /. float count in
mean zero add div
;;

let round x = int_of_float (x +. if x < 0.0 then -0.5 else 0.5);;

let mean_ints =
let zero = 0.0 in
let add sum x = sum +. float x in
let div sum count = round (sum /. float count) in
mean zero add div
;;

let () =
Printf.fprintf stdout "%f\n" (mean_floats [ 3.0; 4.0; 7.0; ]);
Printf.fprintf stdout "%d\n" (mean_ints [ 3; 4; 7; ]);
flush stdout;
;;

we're better than whites akshully

>def mean(list):
> return sum(list)/len(list)

Is that bad?

from statistics import mode is literally better than anything most people will ever bother writing
what is programmers' obsession with reinventing the wheel

mode isn't the same as mean
You'd know that if you knew how anything works instead of relying of foreign code for everything.

I want to make a script in Python that makes me win in life. How do I begin? Python3 or 2?

of course. Why would you do anything twice if you could just do it once?