-2

The explanation for this problem can be found @ http://www.cemc.uwaterloo.ca/contests/computing/2017/stage%201/juniorEF.pdf
(it's the third problem titled "exactly electrical").

def electrical(a,b,c,d,e) :
  charge = e
  x_dif = abs(c - a)
  y_dif = abs(d - b)
  total_dif = (x_dif + y_dif)

  if (((total_dif) == charge) or ((total_dif % 2 == 0) and (charge %2 == 0)) or ((total_dif % 2 != 0) and (charge % 2 !=0))) :
    return "Y"
  else :
    return "N"

print(electrical(10,2,10,4,5))

This code can also be found at https://repl.it/@erichasegawa/2017-CCC-Junior-S3.

I'm studying to write the Canadian Computing Competition this week, and I have a question about one of their algorithms; why will my function return "Y" when both the charge and distance are even or uneven, but if one is even and the other isn't (or vice versa) it returns false. I understand that this works, but I don't know why or how it works. If someone could explain this that would be great.

DYZ
  • 55,249
  • 10
  • 64
  • 93
Eric Hasegawa
  • 169
  • 1
  • 14
  • 2
    Please read How to Ask a Question (https://stackoverflow.com/help/how-to-ask) – DYZ Feb 12 '18 at 00:21

2 Answers2

1

Breakdown the condition:

if (((total_dif) == charge) or ((total_dif % 2 == 0) and (charge %2 == 0)) or ((total_dif % 2 != 0) and (charge % 2 !=0)))

We have...

(total dif == charge) # they are equal, so either both odd or even

or ((total_dif % 2 == 0) and (charge % 2 == 0)) # if their remainder after division by 2 is 0, then they're both even 

or ((total_dif % 2 != 0) and (charge % 2 != 0)) # if their remainder after division by 2 is NOT 0, then they're both odd

Note that the first condition is unnecessary; we already check if both are even or if both are odd later on. Having or removing should not change the behaviour of the program.

Also note that the set of brackets around "total_dif" are unnecessary and make an already huge condition more difficult to read. In fact, you should split up the expression in different parts, perhaps as variables both_even and both_odd, and then check

if (both_even or both_odd)

which is much more readable

Umer Amjad
  • 68
  • 6
0

Your code states

if (((total_dif) == charge) or ((total_dif % 2 == 0) and (charge %2 == 0)) or ((total_dif % 2 != 0) and (charge % 2 !=0))) :

The first condition

(total_dif == charge)

is determining whether the charge is equal to the distance needed, so both are even or odd.

The second condition

((total_dif % 2 == 0) and (charge %2 == 0))

checks if the remainder of both arguments (charge and distance) when divided by two are even (the % operator); it is checking if they are both even numbers.

Your third condition does just the opposite, it checks if they are both odd numbers.

In essence, your conditions are checking if the difference between the charge and the distance is divisible by two, since you can make two U-turns to negate this even surplus.

Therefore, your conditions could be simplified to

if ((charge-total_dif) % 2 == 0):

Your question is technically irrelevant to the problem, since if one argument is even and the other is odd, you will either have a surplus of a deficit. Always.

The only other problem with your code is that it never checks if the charge is greater or equal to the distance!

This means you can have an even distance n (like 28321728932) with 0 charge and still return 'Y', or have an odd distance x (like 3121) with 3 charge and still return 'Y'.

Therefore, you should include the condition

charge >= total_dif

in your code.

In summary, your condition should be

if ((charge >= total_dif) and ((charge-total_dif) % 2 == 0):

Final note: please use shorter variable names. In a short program such as yours, you won't have trouble distinguishing between shorter names.