-2

I'm having a difficult time learning python syntax. I've been doing some algorithms for a merge sort and I've run into a bit of an issue.

def arrMerge(a):
    for i in range(1,len(a), *2):
        for j in range(0,len(a)-1,2*i):
            end2 = (2*i < len(a) -j) ? 2*i : len(a) -j

this block in python any ideas on how I should go about executing it?

Siggiofur
  • 3
  • 2
  • 1
    Sorry, what is your question? What are you trying to achieve? Your code is certainly not valid Python, but it is unclear to me what you are trying to achieve. – Martijn Pieters Oct 10 '14 at 12:12
  • You need to be clearer in terms of what you want to achieve. What language is this code supposed to be in, are you trying to port it to or from c++? – Yann Oct 10 '14 at 12:14

1 Answers1

1

I assume that you are asking what is the Python equivalent syntax to C++ ternary operator. In Python you would use a conditional expression that has the syntax value if condition else other_value.

So your assignment would become:

end2 = 2 * i if 2 * i < len(a) - j else len(a) - j

It is usually better to use a plain if though:

if 2 * i < len(a) - j:
  end2 = 2 * i
else:
  end2 = len(a) - j
Sylvain Defresne
  • 42,429
  • 12
  • 75
  • 85