Help with complex calculating within a list, from CodeWars


#1

Thought I’d run this by you guys, if that’s okay?

Job is to take an integer array and find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N. If there is no index that would make this happen, return -1.

def find_even_index(arr):
  #taking index length, checking if even or odd, and then dividing it by 2 to get midpoint, or close to midpoint     of list
   y = len(arr)
  if y % 2 != 0 and y != 1:
    x = (y-1)/2
  else:
    x = y/2    
  print "x is %s" % x 
  #summing up each item of arr list, then dividing by 2 to get a value.
  a= 0
  for z in range(len(arr)):
    a += arr[z]
  b = a/2
  #checking if b is equal to any item of the arr list, by going through its indices
  for N in arr:
    if b == arr[N]:
      print "Iterator N is %d" % N
      print "b is %d" % b
      print "Index value is %d" % arr[N]
      return True
  if N == range(len(arr)) and b != arr[N]:
      print "-1"
      return -1

So why doesn’t it return correctly for me?


#2

this does probably not what you expect. In this case N is not the index number of the array but the value. So arr[N] will give you an “list index out of range” error if N is higher than the length of the arr.

This might give better results:

#checking if b is equal to any item of the arr list, by going through its indices
	for N in arr:
		if b == N:
			print ("value N is %d" % N)
			print ("b is %d" % b)
			# print ("Index value is %d" % arr[N])  N is the value not the index
			return True

#3

Test your approach manually, or make it print out the reasoning being done and compare it with what should happen.
A good thing to print out would be what the sums on both sides are since without that information you can’t come up with a result. That gives you something that is easy to verify at each iteration as well.


#4

Alright, I already had a modified version, but I’ll try yours as well.

Here’s my version for now, you can check out.

def find_even_index(arr):
  #taking index length, checking if even or odd, and then dividing it by 2 to get midpoint, or close to midpoint     of list
   y = len(arr)
  if y % 2 != 0 and y != 1:
    x = (y-1)/2
  else:
    x = y/2    
  print "x is %s" % x 
  #summing up each item of arr list, then dividing by 2 to get a value.
  a= 0
  for z in range(len(arr)):
    a += arr[z]
  b = a/2
  #checking if b is equal to any item of the arr list, by going through its indices
  for N in arr:
    if b == arr[N]:
      print "Iterator N is %d" % N
      print "b is %d" % b
      print "Index value is %d" % arr[N]
      return True
  if N == range(len(arr)) and b != arr[N]:
      print "-1"
      return -1

#6

What makes you say that b would be in arr for a solvable arr? And even if it is, what makes you say it’s in the right position? What if all values in arr are equal, how will you then know which one is the right one?


#7

Yeah the formula doesn’t work, I realize.


#8

The instructions already describe a way to do it. It’s a bit slow, but it may be fast enough for somewhat small input.

Doing it manually may make it fairly easy to find a better solution, humans are decent at finding patterns.

The task is easy to carry out manually for small input, so there is a lot of opportunity to work out an algorithm with pen and paper.

You can also reason it out by considering how the sums of each side relate to each other and how this changes as you iterate.