Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am quite new to programming, and was wondering if someone might be able to help me get the following working in python:

Define $\Phi_mx(mn+r)=mx(n)+\frac{r}{m}(x(n+1)-x(n))$, where we consider x(0)=0, r is always positive and less than m, and all values of n are positive integers. $\Phi_m$ takes in a string of numbers and outputs another string. How might I program this?

share|improve this question

closed as off-topic by Glenn Nelson, MichaelT, GlenH7, BЈовић, Dynamic Jul 5 '13 at 13:44

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Programmers.SE doesn't accept that type of markup. Could you rewrite it... and consider taking a stab at writing some pseudocode for it? –  MichaelT Jul 4 '13 at 23:10
2  
Looks like a homework problem to me. –  vy32 Jul 4 '13 at 23:37
2  
@vy32: homework-related problems are fine as long as the person asking the question is trying to master programming, and not get the homework done for him. –  9000 Jul 4 '13 at 23:39
2  
It actually isn't homework related, I'm a math student trying to automate this to make some other work using it faster. –  user95751 Jul 4 '13 at 23:49
2  
Could you take another crack at trying to explain it and walk us through what it is doing? THere are quite a few values there that appear to be interrelated. Also please note that none of the stack exchanges are a code writing service, if you are looking for the code you might want to try writing some first and then ask about the problems you are having in the code. –  MichaelT Jul 5 '13 at 0:12
show 3 more comments

1 Answer

up vote 2 down vote accepted

OK, some quick intro for you.

To store ready-made finite sequences, use lists:

x = [0, 1, 2, 2, 3, 3, 3, 3]

This lets you easily address each sequence element by index in brackets, 0 being the first index, so x[0] == 0 and x[4] == 3.

Your function Ф could be defined as

def F(n, m, r, x):
  return m * x[n] + (r / m) * (x[n + 1] - x[n])

So F(3, 2.0, 1.5, x) == 4.75.

To see the function applied to the entire list x, you can do something like this:

# we use len() - 1 because len(x) is the number of elements in x,
# and the last valid k for x[k] is len(x) - 1;
# F uses x[n+1], so n must not exceed len(x) - 2.
# But range(n) gives us numbers 0..n-1, so the last n we generate
# is (len(x) - 1) - 1, exactly what we need. 
for n in range(len(x) - 1):
  print n, F(n, 2.0, 1.5, x)

Now go read Python in 5 minutes and then Dive Into Python to understand things better. Though a math student might find Haskell more compelling, if not as easy :)

share|improve this answer
    
I see what you mean. Is there a way though for Python to read the whole list at once and give separate output for each integer? Or even better (though I should read up on it for this one I would guess) a variety of m's? –  user95751 Jul 5 '13 at 0:30
    
I actually have several math friends that like Prolog –  jozefg Jul 5 '13 at 0:48
    
See the updated answer. –  9000 Jul 5 '13 at 0:48
    
@jozefg: Prolog is nice, but usually it's a key from a different door than this. Einstein's houses puzzle is a good fit for Prolog. –  9000 Jul 5 '13 at 0:50
    
@9000 Thanks, that makes good sense. The one last hurdle I'll have to overcome is varying r, since r should in some sense depend on a value a. That is, we seek x(a)=x(mn+r), so for example x(5) = x(2x2+1). This gives us n and r, but we only actually knew a and any previously calculated terms. –  user95751 Jul 5 '13 at 4:21
show 1 more comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.