473,395 Members | 1,706 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,395 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 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:

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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...

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.