473,241 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

help with object method calls

50
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:

Expand|Select|Wrap|Line Numbers
  1. def factorial(n):
  2.     '''
  3.     n is a positive integer;
  4.     RETURNS: integer factorial of n from a recursive function.
  5.     '''
  6.     f = 1
  7.     while (n > 0):
  8.         f = f * n
  9.         n = n - 1
  10.     return f
  11.  
  12. def binomial(n, p, x):
  13.     '''
  14.     n is a positive integer number of independent Bernoulli Trials;
  15.         p is the probability of success of the binomial event;
  16.         x is the positive integer number of Bernoulli Trials of n.
  17.     RETURNS: The probability of success from a series of Bernoulli Trials
  18.     '''
  19.     fn = factorial(n)
  20.     return (fn/(factorial(x)*(factorial(n-x))))*(p**x)*((1-p)**(n-x))
  21.  
  22.  
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Numbers():
  3.     def __init__(self, numbers):
  4.         self.numbers = numbers
  5.  
  6.     def factorial(self):
  7.         f=1
  8.         while (self.numbers>0):
  9.             f*=self.numbers
  10.             self.numbers-=1
  11.         return f
  12.  
  13.     def binomial(self, x, p):
  14.         fn=self.factorial()
  15.         y=Numbers(x)
  16.         return (fn/(y.factorial()* (y-1).factorial))# stuck here
  17.  
  18.  
Sep 21 '07 #1
2 1173
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:

Expand|Select|Wrap|Line Numbers
  1. def factorial(n):
  2.     '''
  3.     n is a positive integer;
  4.     RETURNS: integer factorial of n from a recursive function.
  5.     '''
  6.     f = 1
  7.     while (n > 0):
  8.         f = f * n
  9.         n = n - 1
  10.     return f
  11.  
  12. def binomial(n, p, x):
  13.     '''
  14.     n is a positive integer number of independent Bernoulli Trials;
  15.         p is the probability of success of the binomial event;
  16.         x is the positive integer number of Bernoulli Trials of n.
  17.     RETURNS: The probability of success from a series of Bernoulli Trials
  18.     '''
  19.     fn = factorial(n)
  20.     return (fn/(factorial(x)*(factorial(n-x))))*(p**x)*((1-p)**(n-x))
  21.  
  22.  
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. class Numbers():
  3.     def __init__(self, numbers):
  4.         self.numbers = numbers
  5.  
  6.     def factorial(self):
  7.         f=1
  8.         while (self.numbers>0):
  9.             f*=self.numbers
  10.             self.numbers-=1
  11.         return f
  12.  
  13.     def binomial(self, x, p):
  14.         fn=self.factorial()
  15.         y=Numbers(x)
  16.         return (fn/(y.factorial()* (y-1).factorial))# stuck here
  17.  
  18.  
This seems to work. You may need to do some error trapping.
Expand|Select|Wrap|Line Numbers
  1. class Numbers(object):
  2.     def __init__(self, numbers):
  3.         self.numbers = numbers
  4.  
  5.     def factorial(self, x):
  6.         f=1
  7.         while (x>0):
  8.             f*=x
  9.             x-=1
  10.         return f
  11.  
  12.     def binomial(self, x, p):
  13.         fn=self.factorial(self.numbers)
  14.         return (float(fn)/(self.factorial(x)*\
  15.                     (self.factorial(self.numbers-x)-1))
  16.                 )*(p**x)*((1-p)**(self.numbers-x)
Sep 21 '07 #2
kdt
50
This seems to work. You may need to do some error trapping.
Expand|Select|Wrap|Line Numbers
  1. class Numbers(object):
  2.     def __init__(self, numbers):
  3.         self.numbers = numbers
  4.  
  5.     def factorial(self, x):
  6.         f=1
  7.         while (x>0):
  8.             f*=x
  9.             x-=1
  10.         return f
  11.  
  12.     def binomial(self, x, p):
  13.         fn=self.factorial(self.numbers)
  14.         return (float(fn)/(self.factorial(x)*\
  15.                     (self.factorial(self.numbers-x)-1))
  16.                 )*(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

Expand|Select|Wrap|Line Numbers
  1. class Number(object):
  2.     def __init__(self, number):
  3.         self.number = number
  4.  
  5.     def factorial(self, x):
  6.         f=1
  7.         while (x>0):
  8.             f*=x
  9.             x-=1
  10.         return f
  11.  
  12.     def binomial(self, x, p):
  13.         fn=float(self.factorial(self.number))
  14.         return (fn/(self.factorial(x)*\
  15.                            (self.factorial(self.number-x))))*\
  16.                            (p**x)*((1-p)**(self.number-x))
  17.     def __str__(self):
  18.         return str(self.number)
  19.  
  20.  
  21. n = Number(10)
  22. print n.factorial(6)
  23. print n.binomial(3, 0.8)
  24.  
  25. >>> 
  26. 720
  27. 0.000786432
  28.  
Sep 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
12
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...
0
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...
3
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...
22
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;
19
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...
24
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...
3
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...
0
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...
0
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...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.