(I am a beginner in python) I am trying to solve a python problem which is the following:
However, it doesn’t pass all tests. Help is appreciated.
This is what I managed to write:
choc_weight = int(input()) div = choc_weight // 2 if choc_weight % 2 == 0 and div % 2 == 0: print("YES") else: print("NO")
EDIT: I can not see failed tests. They are hidden. The only visible test is available in the screenshot
NOTE: according to the exercise, 10 as input does NOT work because half of 10 is 5 and 5 is odd. That’s why I included the div logic, so it makes sure the halving of the chocolate weight is EVEN and not ODD
10 as input does NOT work
But it should… I think you should focus more on this statement
the parts do not need to be equal
Based on that statement, 10 can be made into 4 and 6 or 2 and 8… So, don’t divide by two.
Other example, already given, 6 is split into 4 and 2 (so output is YES since both are even), but it’s 3 by dividing by 2. Using
3 % 2 == 0 is causing you to print NO.
Therefore int-division by 2 isn’t really correct.
You can also consider other cases like 4 is the first YES since nothing below can be split that way, and you can immediately exclude all odds since two even numbers must add to an even.
So, this is probably closer to what you want
if choc_weight < 4: print("NO") elif choc_weight % 2!=0: print("NO") else: print("YES")
You can also make one of the girls be greedy and take the most bar possible by giving the other girl at least
large_piece = choc_weight - 2 if choc_weight % 2 == 0 and (large_piece > 1 and large_piece % 2 == 0): # implicitly ensures choc_weight > 3 print("YES") else: print("NO")