Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Symmetrical Sum

### START FUNCTION
def symmetrical_sum(a):
   
    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
### END FUNCTION
Comment

PREVIOUS NEXT
Code Example
Python :: pygame pin to top 
Python :: fast way to load mongodb data into python list 
Python :: list all placeholders python pptx 
Python :: python requests with authorisation token 
Python :: how to create one list from 2d list python 
Python :: change gles3 to gles2 
Python :: Changing the data type to category 
Python :: find an element using id in requests-html library in python 
Python :: how to append dict to dict in python 
Python :: Add New Column to Pandas from Dictionary 
Python :: MNIST model 
Python :: Flatten List in Python Using NumPy concatenate 
Python :: set index values pandas 
Python :: python email subject decode 
Python :: Python - Sort Lists 
Python :: unpacking in python 
Python :: img not responding jupyter notebook imshow 
Python :: python get timestamp 2020-04-23T12:00:00Z 
Python :: how to remove axis in matplotlib 
Python :: how to make bak files with python 
Python :: how to make a bot send whatever you dm it into a server discord.py 
Python :: cv2 remove black borders on images 
Python :: how to get all distinct substrings in a string python 
Python :: python second interval 
Python :: how to redirect where requests library downloads file python 
Python :: msg91 python 
Python :: # add keys to existing dictionary 
Python :: how to install dependencies python 
Python :: what is fn.call 
Python :: how to check if a key is present in python dictionary 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =