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: -
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))
-
-
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 -
-
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.factorial()
-
y=Numbers(x)
-
return (fn/(y.factorial()* (y-1).factorial))# stuck here
-
-
2 1176 bvdet 2,851
Expert Mod 2GB
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: -
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))
-
-
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 -
-
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.factorial()
-
y=Numbers(x)
-
return (fn/(y.factorial()* (y-1).factorial))# stuck here
-
-
This seems to work. You may need to do some error trapping. - class Numbers(object):
-
def __init__(self, numbers):
-
self.numbers = numbers
-
-
def factorial(self, x):
-
f=1
-
while (x>0):
-
f*=x
-
x-=1
-
return f
-
-
def binomial(self, x, p):
-
fn=self.factorial(self.numbers)
-
return (float(fn)/(self.factorial(x)*\
-
(self.factorial(self.numbers-x)-1))
-
)*(p**x)*((1-p)**(self.numbers-x)
This seems to work. You may need to do some error trapping. - class Numbers(object):
-
def __init__(self, numbers):
-
self.numbers = numbers
-
-
def factorial(self, x):
-
f=1
-
while (x>0):
-
f*=x
-
x-=1
-
return f
-
-
def binomial(self, x, p):
-
fn=self.factorial(self.numbers)
-
return (float(fn)/(self.factorial(x)*\
-
(self.factorial(self.numbers-x)-1))
-
)*(p**x)*((1-p)**(self.numbers-x)
Thanks bvdet, was struggling with this yesterday. There's an extra '-1' in your code, but apart from that, it works fine. The only obscurity is having to pass two parameters for the factorial method - although I understand that this is required for it to be able to be called from the binomail method. Is it better practice to keep it like this, or should it be left in functions? Just looking for an opinion here, still a newb so want to learn the best practices. Cheers -
class Number(object):
-
def __init__(self, number):
-
self.number = number
-
-
def factorial(self, x):
-
f=1
-
while (x>0):
-
f*=x
-
x-=1
-
return f
-
-
def binomial(self, x, p):
-
fn=float(self.factorial(self.number))
-
return (fn/(self.factorial(x)*\
-
(self.factorial(self.number-x))))*\
-
(p**x)*((1-p)**(self.number-x))
-
def __str__(self):
-
return str(self.number)
-
-
-
n = Number(10)
-
print n.factorial(6)
-
print n.binomial(3, 0.8)
-
-
>>>
-
720
-
0.000786432
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steve Menard |
last post by:
Here is my problem.
I have this library thats hosts another language within python, and
allows that language to call back INTO python.
All is good as long as the other languages calls back on...
|
by: Ricardo Pereira |
last post by:
Hello all,
I have a C# class (in this example, called A) that, in its
constructor, starts a thread with a method of its own. That thread
will be used to continuously check for one of its...
|
by: TT (Tom Tempelaere) |
last post by:
Hi,
In my project I have a seperate logging application. I use remoting to log
to it. The remoted object exposes a certain (log-)interface, and the methods
therein are all OneWay methods...
|
by: Gabe Covert |
last post by:
I'm a new C# developer, and am developing an application which will utilize
a COM library from a third party. I have two following SDK calls from the
3rd-party SDK which I can't get to work under...
|
by: Jeff Louie |
last post by:
Well I wonder if my old brain can handle threading. Dose this code look
reasonable.
Regards,
Jeff
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
|
by: trint |
last post by:
Ok,
I start my thread job:
Thread t = new Thread(new
ThreadStart(invoicePrintingLongRunningCodeThread));
t.IsBackground = true;
t.Start();
There are lots of calls to controls and many...
|
by: arcticool |
last post by:
I had an interview today and I got destroyed :(
The question was why have a stack and a heap?
I could answer all the practical stuff like value types live on the
stack, enums are on the stack, as...
|
by: Grant Schenck |
last post by:
Hello,
I have a Windows Service developed in C# .NET. I'm making it a remote
server and I can, via an IPC Channel, expose methods and call them from a
client. However, I now want my remoted...
|
by: gunimpi |
last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431
********************************************************
VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help
wanted...
|
by: akshaycjoshi |
last post by:
I am reading a book which says
Even though unboxed value types don't have a type object pointer, you can still call virtual
methods (such as Equals, GetHashCode, or ToString) inherited or...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |