-2

We're sorry if it sounds too noob. But this is our life's first encounter with Python.

We have got a python function like this:

def m(n,k,c=0):x,y=n;return c if n==k else 7*(c>6)or min(m((x+a,y+b),k,c+1)for a,b in[(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)])

which is being called like this:

print m((1,2), (5,6))

In order to understand it we were trying to add line breaks like this:

def m(n,k,c=0):x,y=n;return c
 if n==k else 7*(c>6) or min(m((x+a,y+b),k,c+1)for a,b in[(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)])

We're not understanding why is just a simple new line breaking the code

Once we understand this, probably we shall be able to convert this program to javascript:

function m(n, k, c = 0) {
  x,
  y = n;
  return c
  if (n == k) {

  } else {
    7 * (c > 6) or Math.min(m((x + a, y + b), k, c + 1) for a, b in [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)])
  }
}
Mina
  • 42
  • 5
  • 4
    Line breaks are statement separators in Python. – user2357112 supports Monica May 22 '17 at 18:33
  • @ user2357112 But there is no line break in this entire code. I don't think this is one single statement. There is statement separation for sure, I'm not understanding how to break this down into pieces and target each chunk separately – Mina May 22 '17 at 18:34
  • 1
    You *just put* a line break in the code. – user2357112 supports Monica May 22 '17 at 18:35
  • The whole `return c if n==k else 7*(c>6)or min(m((x+a,y+b),k,c+1)for a,b in[(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)])` part is supposed to be one statement. – user2357112 supports Monica May 22 '17 at 18:36
  • [Conditional expressions](https://docs.python.org/3/reference/expressions.html#conditional-expressions) and [if statements](https://docs.python.org/3/reference/compound_stmts.html#the-if-statement) are distinct syntactical elements. The former does not like to be interrupted with newlines. – Kevin May 22 '17 at 18:37
  • 2
    try to add backslash at the end of the first line (line continuation) – Jean-François Fabre May 22 '17 at 18:43
  • @Mina, it's a function definition consisting of 2 statements, which are separated by a `;` – Brian Minton May 22 '17 at 18:55
  • @BrianMinton So, it looks like we've array (for tuples) and curly braces (for separate statements). I'll updated the JS code – Mina May 22 '17 at 18:58
  • @Mina, you'd also have to make that return statement using the Javascript `? :` operator to match the Python behavior. – Brian Minton May 22 '17 at 19:00

2 Answers2

3

When you define a function, either the function definition must be all on the same line (as in your original example), or the header def m(n,k,c=0): must be on the line of its own, and the remaining statements must be on the next line(s). You cannot mix-and-match.

def m(n, k, c=0):
  x, y = n
  return c if n==k else \
         7 * (c > 6) or min(m((x + a, y + b), k, c + 1)
                            for a,b in [(1,2), (1,-2), (-1,2), (-1,-2),
                                        (2,1), (2,-1), (-2,1), (-2,-1)])

m((1,2), 3)
#7
DYZ
  • 45,526
  • 9
  • 47
  • 76
  • As per your suggestion This results in `def m(n,k,c=0): x,y=n;return c if n==k else 7*(c>6)or min(m((x+a,y+b),k,c+1)for a,b in[(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)])` which is again breaking – Mina May 22 '17 at 18:38
  • [This](https://jsfiddle.net/r79tfjqw/) is what I have came with. I still don't understand what the heck `x, y = n;` is doing there – Mina May 22 '17 at 18:45
  • 2
    I have no idea what you are trying to accomplish, but anything after `return c` will _not_ be executed. – DYZ May 22 '17 at 18:46
  • I simply wanted to understand ` x, y = n;` and other lines. Do you get some sense out of this code? – Mina May 22 '17 at 18:51
  • 2
    @DYZ, it is in fact a list comprehension. It's assigning the tuple `a,b` sequentially to the tuples in the list `[(1,2),(1,-2) ...` – Brian Minton May 22 '17 at 18:51
  • 2
    @Mina `x,y=n` is tuple splitting. See for instance http://www.pythonlearn.com/html-007/cfbook011.html . So, for `x,y=n` to work, n must be a tuple. Then, the first and second item of the tuple are assigned to x and y respectively. – Brian Minton May 22 '17 at 18:53
  • @Mina Actually, the one-liner in your first comment _does_ work: `def m(n,k,c=0): x,y=n;return c if n==k else 7*(c>6)or min(m((x+a,y+b),k,c+1)for a,b in[(1,2),(1,-2),(-1,2),(-1,-2),(2,1),(2,-1),(-2,1),(-2,-1)])`. – DYZ May 22 '17 at 18:54
  • @BrianMinton So, it seems like it is array in javascript. Let me update my code then – Mina May 22 '17 at 18:55
  • @DYZ I know that one liner is not the problem, the whole question is about adding line breaks and understanding the concept / logic behind this code – Mina May 22 '17 at 18:59
  • Why don't you just write it in a nice and clean way? (See updated answer). – DYZ May 22 '17 at 19:02
2

The part that goes return c if n==k else ... is a ternary conditional operator in Python (similar to the ? : operator in JS). See Does Python have a ternary conditional operator? for instance. You can't put a line break there, because in Python that's a statement separator.

Brian Minton
  • 2,562
  • 3
  • 33
  • 38
  • I have added some js code into my question. Does that make things a little more clear? – Mina May 22 '17 at 18:42
  • 2
    @Mina: The function you're looking at seems to have been made cryptic deliberately. Python doesn't usually look like this. You could easily write Javascript that cryptic or more, but (hopefully) you don't. – user2357112 supports Monica May 22 '17 at 18:46
  • @user2357112 But to write cryptic js, I need to understand this first. Are you getting the sense out of this code? – Mina May 22 '17 at 18:48
  • @Mina, JS also has list (array) comprehensions, but I'm not familiar enough with the syntax to understand if your JS code is correct. The docs say, "don't use", but https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions seems to be helpful. It seems to use the keyword `of` instead of the Python `in` keyword. – Brian Minton May 22 '17 at 19:05
  • @BrianMinton: The docs say "don't use" because array comprehensions didn't make it into the standard and only Firefox has them. – user2357112 supports Monica May 22 '17 at 19:35