li = list(map(int, input().split()))
n = li.pop(0)
k = li.pop(0)
a = li[:n] #first shelf => stores the lower numbers
b = li[n:] #second shelf => stores the larger numbers
for _ in range(k):
a.sort()
b.sort()
a = a[::-1]
b = b[::-1]
if max(a) > max(b):
a, b = b, a
if (a[0] > b[-1]): #if swapping will help
a[0], b[-1] = b[-1], a[0] #swap largest number from a with smallest number in b
print(max(a) + max(b)) #print skew
Could someone explain the issue with my code. And suggest how I am supposed to be able to understand what input my code is giving the wrong input for?
Since the two original shelves are \{10,1,2,3,4\} and \{5,6,7,8,9\}, the correct value can be obtained by swapping 10 and 5 to give a result of {\red{15}}
Could you explain how you see/understand which input would fail a code?
The glib answer is “experience”, but a good place to start getting some of those skills is to understand why you wrongly thought 17 was the right answer.
You have great information in this case – other people got a different answer which was accepted as correct – which would be a rarity in most programming projects. You need to be able to separate what is the outcome of the code you wrote from what is a correct outcome from reading the specification. Sometimes you can use a slow algorithm on simple data to spot the difference with your outcomes. Sometimes you just need to work through enough cases to get a feel for possible paths your algorithm needs to account for.