-1

What the code does: Takes a Python list of integers as input and searches for a 'symmetrical' inner-portion of the list.

Examples of what i want:

symmetrical_sum([10,11,12,11,12]) == ([11, 12, 11], 34)
symmetrical_sum([9,99,88,8,77,7,77,8,88,10,100]) == ([88, 8, 77, 7, 77, 8, 88], 353)
symmetrical_sum([10,8,7,5,9,8,15]) == ([8, 7, 5, 9, 8], 37) 

Symmetry occurs if the value of the ith element from the start of the list is equal to the value of the ith element from the end of the list.

My Code:

def symmetrical_sum(a):

#extract duplicate value
    dupe = [x for n, x in enumerate(a) if x in a[:n]] 

#if no duplicate values found, do the following:
    if dupe == []:
        middle = float(len(a))/2
    if middle % 2 != 0:
        sym = a[int(middle - .5):int(middle + .5)]
        ans = a[int(middle - .5)]
        tuple1 = (sym,ans)
    elif middle % 2 == 0:
        sym = a[int(middle - 1):int(middle + 1)]
        ans = sum(a[int(middle - 1):int(middle + 1)])//2
        tuple1 = (sym,ans)

    return tuple1

else:
    d_to_i = int("".join(map(str, dupe))) #convert duplicate value to integer
    p1 = a.index(d_to_i) #get index of first duplicate
    p2 = a.index(d_to_i, p1+1) #get index of second duplicate
    sym = a[p1:p2+1] #[symmetrical-portion]
    ans = sum(sym) #sum-of-symmetrical-portion
    tuple2 = (sym, ans)

return tuple2

My code works but it would be great if someone can post a way shorter solution for efficiency purposes, please.

  • Is the list symmetrical only because one value is mirrored? what do you want to happen if more "symmetrical" sublists exist? – joostblack Mar 18 '21 at 07:00
  • Did you want to get the largest possible chain of sums or the max value or get all symmetrical sums? Because theoretically you can get many symmetrical sums in some list of integers. I think list comprehension or a recursive algorithm would be helpful. – Jason Chia Mar 18 '21 at 07:06
  • @JasonChia I just want to have one list of integers and then get the symmetrical list of that list and the sum of that symmetrical list. And it would be great if it can be one line of code since i am not an expert so my code is very long –  Mar 18 '21 at 07:16
  • @joostblack look at examples i have added then you will know what i am looking for :) –  Mar 18 '21 at 07:35

1 Answers1

1
x = [10,11,12,11,12]
output = [(x[n:-n],sum(x[n:-n])) for n in range(len(x))  if x[n] == x[-n-1]]
#this will output 3 cases: Symmetry regardless of even/odd elements. OR no symmetry for odd. (middle index)

if output == []:#even number of elements with no symmetry at all
     pass

if len(output[0][0]) == 1: #odd number of elements with no symmetry at all
    pass
print(output[0])

I hope this helps. I don't really understand what you do when no symmetry is detected. output will return all lists of symmetry and their sums including if there is no symmetry but odd number of elements. Not particularly sure if this is the optimal way to do what you want.

Jason Chia
  • 1,144
  • 1
  • 5
  • 18