Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'float': 0.07; 'indices': 0.07; '[0,': 0.09; 'function,': 0.09; 'len(x)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Function': 0.09; 'subject:Number': 0.09; 'python': 0.11; 'def': 0.12; '2):': 0.16; 'integers,': 0.16; 'integers.': 0.16; 'range(0,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject: \n ': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'pointed': 0.19; 'machine': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'errors.': 0.24; 'integer': 0.24; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; 'fixed': 0.29; "doesn't": 0.30; 'code': 0.31; '"",': 0.31; '>>>>': 0.31; 'subject:that': 0.31; 'file': 0.32; '(most': 0.33; "i'd": 0.34; 'something': 0.35; 'no,': 0.35; 'but': 0.35; 'there': 0.35; 'right?': 0.36; 'list': 0.37; 'implement': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'ian': 0.60; 'results.': 0.60; 'matter': 0.61; "you'll": 0.62; 'more': 0.64; 'receive': 0.70; "it'd": 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition Date: Thu, 18 Jul 2013 22:43:11 -0400 References: <8a23cbf1-3cfd-48e3-a460-64551119fde7@googlegroups.com> <537e9d19-6587-4773-8910-df4005505653@googlegroups.com> <8c5f6217-0029-481f-9bb0-dab1b34180df@googlegroups.com> <2c9344f3-ec42-477f-b9a7-6cf444906298@googlegroups.com> <97eeec63-7a06-47ad-ad8c-863c58369d01@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 174.32.174.33 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7 In-Reply-To: <97eeec63-7a06-47ad-ad8c-863c58369d01@googlegroups.com> 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374201809 news.xs4all.nl 15914 [2001:888:2000:d::a6]:60603 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50885 On 07/18/2013 10:16 PM, CTSB01 wrote: > >> > > Does something like > > def phi_m(x, m): > rtn = [] > for n2 in range(0, len(x) * m - 2): > n = n2 / m > r = n2 - n * m > rtn.append(m * x[n] + r * (x[n + 1] - x[n])) > print ('n2 =', n2, ': n =', n, ' r =' , r, ' rtn =', rtn) > return rtn > > look right? No, as Ian has pointed out, you need to use the // operator in Python 3 if you want to get integer results. So it'd be n = n2 // m However, even better is to use the divmod() function, which is intended for the purpose: n, r = divmod(n2, m) > > It doesn't seem to have any errors. However, I do receive the following error when trying to implement an x after having defined phi: > >>>> x = [0, 1, 1, 2, 3] >>>> phi_m(x, 2) > Traceback (most recent call last): > File "", line 1, in > phi_m(x, 2) > File "", line 6, in phi_m > rtn.append(m * x[n] + r * (x[n + 1] - x[n])) > TypeError: list indices must be integers, not float > That will be fixed if you correct the code as I described, so you'll get integers. There is a Brezenham algorith that might accomplish this whole function more accurately, or more efficiently. But I'd have to re-derive it, as it's been about 30 years since I used it. And chances are that the efficiencies it brought to machine code won't matter much here. -- DaveA