Creating a Program to Decompose a Number and Run a Function on that Decomposition
CTSB01
scott.moore270 at gmail.com
Thu Jul 18 22:56:05 EDT 2013
On Thursday, July 18, 2013 10:43:11 PM UTC-4, Dave Angel wrote:
> 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
Thanks Dave, I'll take a look at that.
More information about the Python-list
mailing list