Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: UNSURE 0.366 X-Spam-Level: *** X-Spam-Evidence: '*H*': 0.46; '*S*': 0.19; 'output': 0.05; 'def': 0.12; '1):': 0.16; 'int):': 0.16; 'pythonic': 0.16; 'soap,': 0.16; 'valueerror': 0.16; ':-)': 0.16; 'saying': 0.22; 'cheers,': 0.24; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; '120': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'received:66': 0.35; 'but': 0.35; 'skip:f 40': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'space': 0.40; 'dangerous': 0.60; 'received:unknown': 0.61; 'header :Message-Id:1': 0.63; 'skip:n 10': 0.64; 'charset:windows-1256': 0.64; 'more': 0.64; 'saturday': 0.68; 'million': 0.74; 'received:204': 0.75; 'hand': 0.80; 'citizens.': 0.84; 'nice,': 0.84; 'number):': 0.84; 'boxes': 0.91; 'hands': 0.96; '2013': 0.98 X-Spam-Status: No, score=1.4 required=5.0 X-Spam-Level: + From: Gene Heskett To: python-list@python.org Subject: Re: Obfuscated factorial Date: Sat, 26 Oct 2013 10:13:33 -0400 References: <526badb4$0$29972$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <526badb4$0$29972$c3e8da3$5496439d@news.astraweb.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="windows-1256" Content-Transfer-Encoding: 7bit X-Mailman-Approved-At: Sat, 26 Oct 2013 21:16:48 +0200 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 77 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1382815010 news.xs4all.nl 15867 [2001:888:2000:d::a6]:55853 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:57650 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.