Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #57650

Re: Obfuscated factorial

From Gene Heskett <gheskett@wdtv.com>
Subject Re: Obfuscated factorial
Date 2013-10-26 10:13 -0400
References <526badb4$0$29972$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.1597.1382815010.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Saturday 26 October 2013 10:12:33 Steven D'Aprano did opine:

> Just for fun:
> 
> 
> class Numberator:
>     def __init__(self, number):
>         self.__number = number
>     def evaluate(self):
>         return self.__number
> 
> class Multiplier:
>     def __init__(self, multiplier, multiplicand):
>         self.multiplier = multiplier
>         self.multiplicand = multiplicand
>     def evaluate(self):
>         return self.multiplier * self.multiplicand.evaluate()
> 
> class FactorialBuilder:
>     def __init__(self, number):
>         if not isinstance(number, int):
>             raise TypeError
>         if number < 0:
>             raise ValueError
>         self.__number = number
>     def build(self):
>         multiplicand = Numberator(1)
>         for n in range(2, self.__number + 1):
>             multiplicand = Multiplier(n, multiplicand)
>         return Factorialiser(multiplicand)
> 
> class Factorialiser:
>     def __init__(self, evaluatorix):
>         self.__evaluatorix = evaluatorix
>     def calculate(self):
>         return self.__evaluatorix.evaluate()
> 
> for i in range(16):
>     print(i, FactorialBuilder(i).build().calculate())
> 
> 
> 
> And the output is:
> 
> 0 1
> 1 1
> 2 2
> 3 6
> 4 24
> 5 120
> 6 720
> 7 5040
> 8 40320
> 9 362880
> 10 3628800
> 11 39916800
> 12 479001600
> 13 6227020800
> 14 87178291200
> 15 1307674368000
> 
Nice, but will it go to 70? :) 
> 
> It goes without saying that this isn't Pythonic :-)


Cheers, Gene
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)

"Out of register space (ugh)"
		-- vi
A pen in the hand of this president is far more
dangerous than 200 million guns in the hands of
         law-abiding citizens.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Obfuscated factorial Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-10-26 11:55 +0000
  Re: Obfuscated factorial Vito De Tullio <vito.detullio@gmail.com> - 2013-10-26 14:50 +0200
  Re: Obfuscated factorial Chris Angelico <rosuav@gmail.com> - 2013-10-27 00:26 +1100
  Re: Obfuscated factorial Joshua Landau <joshua@landau.ws> - 2013-10-26 16:32 +0100
  Re: Obfuscated factorial Tim Chase <python.list@tim.thechases.com> - 2013-10-26 10:35 -0500
  Re: Obfuscated factorial Gene Heskett <gheskett@wdtv.com> - 2013-10-26 10:13 -0400
  Re: Obfuscated factorial Vito De Tullio <vito.detullio@gmail.com> - 2013-10-27 08:28 +0100

csiph-web