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


Groups > comp.lang.python > #50885

Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition

From Dave Angel <davea@davea.name>
Subject Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition
Date 2013-07-18 22:43 -0400
References (6 earlier) <8c5f6217-0029-481f-9bb0-dab1b34180df@googlegroups.com> <mailman.4852.1374191380.3114.python-list@python.org> <2c9344f3-ec42-477f-b9a7-6cf444906298@googlegroups.com> <mailman.4853.1374195892.3114.python-list@python.org> <97eeec63-7a06-47ad-ad8c-863c58369d01@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.4854.1374201809.3114.python-list@python.org> (permalink)

Show all headers | View raw


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 "<pyshell#6>", line 1, in <module>
>      phi_m(x, 2)
>    File "<pyshell#2>", 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

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


Thread

Creating a Program to Decompose a Number and Run a Function on that Decomposition CTSB01 <scott.moore270@gmail.com> - 2013-07-17 16:58 -0700
  Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition Joshua Landau <joshua@landau.ws> - 2013-07-18 10:12 +0100
    Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition CTSB01 <scott.moore270@gmail.com> - 2013-07-18 14:57 -0700
      Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition Gary Herron <gherron@digipen.edu> - 2013-07-18 15:12 -0700
        Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition CTSB01 <scott.moore270@gmail.com> - 2013-07-18 15:18 -0700
          Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-18 16:49 -0600
            Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition CTSB01 <scott.moore270@gmail.com> - 2013-07-18 16:04 -0700
              Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-18 17:42 -0600
              Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-18 17:45 -0600
                Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition CTSB01 <scott.moore270@gmail.com> - 2013-07-18 17:25 -0700
              Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition "Rhodri James" <rhodri@wildebst.demon.co.uk> - 2013-07-19 00:48 +0100
              Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition Dave Angel <davea@davea.name> - 2013-07-18 19:49 -0400
                Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition CTSB01 <scott.moore270@gmail.com> - 2013-07-18 17:35 -0700
                Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition Dave Angel <davea@davea.name> - 2013-07-18 21:04 -0400
                Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition CTSB01 <scott.moore270@gmail.com> - 2013-07-18 19:16 -0700
                Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition Dave Angel <davea@davea.name> - 2013-07-18 22:43 -0400
                Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition CTSB01 <scott.moore270@gmail.com> - 2013-07-18 19:56 -0700
                Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition Fábio Santos <fabiosantosart@gmail.com> - 2013-07-19 03:48 +0100
                Re: Creating a Program to Decompose a Number and Run a Function on that Decomposition CTSB01 <scott.moore270@gmail.com> - 2013-07-18 19:54 -0700

csiph-web