vito-Z80

sum of list or tuple with/without str and int

May 25th, 2016
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | None | 0 0
  1. __author__ = 'vito-Z80'
  2. def sum(totals,*args):
  3.     """
  4.    totals                              = list or tuple
  5.    *args:
  6.    [N,N], (N,N)                        = the beginning and the end of the added values (end= -1)
  7.    [N],(N)                             = sequence of the added values from the beginning to N (-1)
  8.    'all'                               = amount of all string and numerical values
  9.    'str'                               = amount of all string values
  10.  
  11.    totals=1,3,5,'spam','eggs',7,10     = list or tuple
  12.    (sum(totals)                        = only numerical
  13.    (sum(totals,'all')                  = numerical + strings
  14.    (sum(totals,'str')                  = only string
  15.    (sum(totals,[2,6])                  = only numerical from 2 to 6-1
  16.    (sum(totals,[4],'str')              = only string from 0 to 4-1
  17.    (sum(totals,'all',[3,999])          = numerical + strings from 3 to end
  18.    """
  19.     result,start,end,full_sum=0,0,len(totals),False
  20.     if type(totals)==str:return len(totals)
  21.     for i in args:
  22.         if type(i)==str:full_sum=i
  23.         elif type(i)==list or tuple:
  24.             if len(i)==2:start,end=i[0],i[1]
  25.             else: end=i[0]
  26.     for field in totals:
  27.         if start == 0 and end:
  28.             if full_sum=='str':
  29.                 if type(field)==str:result+=len(field)
  30.             else:
  31.                 if type(field)==int:result+=field
  32.                 else:
  33.                     if full_sum=='all':result+=len(field)
  34.             start+=1
  35.         end-=1
  36.         start-=1
  37.     return result
  38.  
  39. if __name__ == '__main__':
  40.     totals=1,3,5,'spam','eggs',7,10
  41.     print(sum(totals))
  42.     print(sum(totals,'all'))
  43.     print(sum(totals,'str'))
  44.     print(sum(totals,[2,6]))
  45.     print(sum(totals,[4],'str'))
  46.     print(sum(totals,'all',[3,999]))
Advertisement
Add Comment
Please, Sign In to add comment