Hi,
Been reading about objects today. I have a program, that I am trying to rewrite as much as possible as objects. I want to write the following two functions as methods in the Number class:
[CODE=python]
def factorial(n):
'''
n is a positive integer;
RETURNS: integer factorial of n from a recursive function.
'''
f = 1
while (n > 0):
f = f * n
n = n - 1
return f
def binomial(n, p, x):
'''
n is a positive integer number of independent Bernoulli Trials;
p is the probability of success of the binomial event;
x is the positive integer number of Bernoulli Trials of n.
RETURNS: The probability of success from a series of Bernoulli Trials
'''
fn = factorial(n)
return (fn/(factorial(x)*( factorial(n-x))))*(p**x)*(( 1-p)**(n-x))
[/CODE]
I am stuck on the last line of the binomial, as I seem to have to instantiate x and n-x as Numbers. Is this right? Or is it better to not make objects out of them? Thanks
[CODE=python]
class Numbers():
def __init__(self, numbers):
self.numbers = numbers
def factorial(self) :
f=1
while (self.numbers>0 ):
f*=self.numbers
self.numbers-=1
return f
def binomial(self, x, p):
fn=self.factori al()
y=Numbers(x)
return (fn/(y.factorial()* (y-1).factorial))# stuck here
[/CODE]
Been reading about objects today. I have a program, that I am trying to rewrite as much as possible as objects. I want to write the following two functions as methods in the Number class:
[CODE=python]
def factorial(n):
'''
n is a positive integer;
RETURNS: integer factorial of n from a recursive function.
'''
f = 1
while (n > 0):
f = f * n
n = n - 1
return f
def binomial(n, p, x):
'''
n is a positive integer number of independent Bernoulli Trials;
p is the probability of success of the binomial event;
x is the positive integer number of Bernoulli Trials of n.
RETURNS: The probability of success from a series of Bernoulli Trials
'''
fn = factorial(n)
return (fn/(factorial(x)*( factorial(n-x))))*(p**x)*(( 1-p)**(n-x))
[/CODE]
I am stuck on the last line of the binomial, as I seem to have to instantiate x and n-x as Numbers. Is this right? Or is it better to not make objects out of them? Thanks
[CODE=python]
class Numbers():
def __init__(self, numbers):
self.numbers = numbers
def factorial(self) :
f=1
while (self.numbers>0 ):
f*=self.numbers
self.numbers-=1
return f
def binomial(self, x, p):
fn=self.factori al()
y=Numbers(x)
return (fn/(y.factorial()* (y-1).factorial))# stuck here
[/CODE]
Comment