python - Two Step Algorithm: Mapping an Input String to an Output String -
i've been puzzling on how function working in python. function, takes positive integers other positive integers follows:
phi_m(n2) = phi_m(m*n + r) = m*x[n] + r*(x[n + 1] - x[n])
the above terms integer valued , defined follows:
n2 = (n2)th slot of output string m = fixed positive integer n = multiple of m such n*m less or equal n2 r = remainder term fill in amount missing n*m in decomposing n2 x[n] = element in [n1]th slot of input string x[n + 1] = element in [n1 + 1]th slot of input string
in general start string of numbers, 0, 1, 1, 2, 3, 3 , end string of (k+1)m-1 terms, k number of terms started with, excluding 0. use function first fix m, m = 2. decompose n2 in terms of m, n2 representing 'slot' of our output sequence. n2=5. asking 'what in fifth 'slot' of our output string'. in case our total output string of length (5+1)2+1. notice not count 0 - present , our purposes 0th term, hence have 5 initial terms. answer our question of goes in slot take 5=2*2+1 our decomposition. have decomposition can apply our function:
f(x(5)) = f(x(2*2+1)) 2x[2] + 1(x[3] - x[2]).
the thing is, python has know how decompose each number. knows 2 fixed, , knows 2*3 , chooses 2*2. has know little , add remainder 1. once it's done can grab n = 5. is, can run function. seems clear once knows how can run through every n in our range, i'm not sure how program meat of function.
now answer questions: x function? list? number? x[n] list.
what mean when "values of input string"? what's signature of phi_m?
the function acting on list takes in single element of list, gives decomposition of number somehow, , applies 'formula' see above. in sense more of 2 step algorithm.
please let me know if unclear. continue revising until makes sense reading.
maybe bit of code close answer:
>>> def phi_m(x, m): ... rtn = [] ... 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 ... rtn ... >>> x = [0, 1, 1, 2, 3, 3] >>> phi_m(x, 2) n2 = 0 : n = 0 r = 0 rtn = [0] n2 = 1 : n = 0 r = 1 rtn = [0, 1] n2 = 2 : n = 1 r = 0 rtn = [0, 1, 2] n2 = 3 : n = 1 r = 1 rtn = [0, 1, 2, 2] n2 = 4 : n = 2 r = 0 rtn = [0, 1, 2, 2, 2] n2 = 5 : n = 2 r = 1 rtn = [0, 1, 2, 2, 2, 3] n2 = 6 : n = 3 r = 0 rtn = [0, 1, 2, 2, 2, 3, 4] n2 = 7 : n = 3 r = 1 rtn = [0, 1, 2, 2, 2, 3, 4, 5] n2 = 8 : n = 4 r = 0 rtn = [0, 1, 2, 2, 2, 3, 4, 5, 6] n2 = 9 : n = 4 r = 1 rtn = [0, 1, 2, 2, 2, 3, 4, 5, 6, 6] >>>
your original formula length produced out-of-bounds error did 1 in comment. m * len(x) - 2
apparently meant.
Comments
Post a Comment