Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #57598 > unrolled thread
| Started by | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| First post | 2013-10-26 11:55 +0000 |
| Last post | 2013-10-27 08:28 +0100 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.python
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
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-10-26 11:55 +0000 |
| Subject | Obfuscated factorial |
| Message-ID | <526badb4$0$29972$c3e8da3$5496439d@news.astraweb.com> |
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
It goes without saying that this isn't Pythonic :-)
--
Steven
[toc] | [next] | [standalone]
| From | Vito De Tullio <vito.detullio@gmail.com> |
|---|---|
| Date | 2013-10-26 14:50 +0200 |
| Message-ID | <mailman.1572.1382791829.18130.python-list@python.org> |
| In reply to | #57598 |
Steven D'Aprano wrote: > Just for fun: [...] you miss a FactoryFactory and a couple of *Manager. Oh, and it should be xml-config-driven. -- By ZeD
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-10-27 00:26 +1100 |
| Message-ID | <mailman.1574.1382794028.18130.python-list@python.org> |
| In reply to | #57598 |
On Sat, Oct 26, 2013 at 11:50 PM, Vito De Tullio <vito.detullio@gmail.com> wrote: > you miss a FactoryFactory and a couple of *Manager. > > Oh, and it should be xml-config-driven. Or a metaclass and a few decorators. They're inherently Pythonic, right? ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Joshua Landau <joshua@landau.ws> |
|---|---|
| Date | 2013-10-26 16:32 +0100 |
| Message-ID | <mailman.1581.1382801572.18130.python-list@python.org> |
| In reply to | #57598 |
On 26 October 2013 12:55, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Just for fun:
...
> for i in range(16):
> print(i, FactorialBuilder(i).build().calculate())
Python already supports the factorial operator, -ⵘ. You just have to import it.
# Import statement
ⵘ = type("",(),{"__rsub__":lambda s,n:(lambda f,n:f(f,n))(lambda
f,z:round((2.5066282746310002*(z+0j+7.5)**(z+0.5)*2.718281828459045**(-z-7.5)*(0.99999999999980993+676.5203681218851/(z+1)-1259.1392167224028/(z+2)+771.32342877765313/(z+3)-176.61502916214059/(z+4)+12.507343278686905/(z+5)-0.13857109526572012/(z+6)+9.9843695780195716e-6/(z+7)+1.5056327351493116e-7/(z+8))).real)if
z>=0.5 else 3.141592653589793/(__import__("math").sin(3.141592653589793*z)*f(f,1-z)),n)})()
assert 2-ⵘ == 2
assert 3-ⵘ == 6
assert 4-ⵘ == 24
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-10-26 10:35 -0500 |
| Message-ID | <mailman.1582.1382801642.18130.python-list@python.org> |
| In reply to | #57598 |
On 2013-10-27 00:26, Chris Angelico wrote: > On Sat, Oct 26, 2013 at 11:50 PM, Vito De Tullio > <vito.detullio@gmail.com> wrote: > > you miss a FactoryFactory and a couple of *Manager. > > > > Oh, and it should be xml-config-driven. > > Or a metaclass and a few decorators. They're inherently Pythonic, > right? You are deploying it to The Cloud™ right? And where is the horizontal scalability? Why doesn't it use NoSQL? Bah, just goes to show that Python is no good for Enterprise™ applications ;-) -tkc
[toc] | [prev] | [next] | [standalone]
| From | Gene Heskett <gheskett@wdtv.com> |
|---|---|
| Date | 2013-10-26 10:13 -0400 |
| Message-ID | <mailman.1597.1382815010.18130.python-list@python.org> |
| In reply to | #57598 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Vito De Tullio <vito.detullio@gmail.com> |
|---|---|
| Date | 2013-10-27 08:28 +0100 |
| Message-ID | <mailman.1641.1382858920.18130.python-list@python.org> |
| In reply to | #57598 |
Joshua Landau wrote:
> Python already supports the factorial operator, -ⵘ.
why use ⵘ (TIFINAGH LETTER AYER YAGH) when you can have the BANG? 방 (HANGUL
SYLLABLE BANG)
> You just have to
> import it.
>
> # Import statement
> ⵘ = type("",(),{"__rsub__":lambda s,n:(lambda f,n:f(f,n))(lambda
> f,z:round((2.5066282746310002*(z+0j+7.5)**(z+0.5)*2.718281828459045**(-
z-7.5)*(0.99999999999980993+676.5203681218851/(z+1)-1259.1392167224028/(z+2)+771.32342877765313/(z+3)-176.61502916214059/(z+4)+12.507343278686905/(z+5)-0.13857109526572012/(z+6)+9.9843695780195716e-6/(z+7)+1.5056327351493116e-7/(z+8))).real)if
> z>=0.5 else
> 3.141592653589793/(__import__("math").sin(3.141592653589793*z)*f(f,1-
z)),n)})()
I'm really, really impressed!
--
By ZeD
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web