Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'vito-Z80'
- def sum(totals,*args):
- """
- totals = list or tuple
- *args:
- [N,N], (N,N) = the beginning and the end of the added values (end= -1)
- [N],(N) = sequence of the added values from the beginning to N (-1)
- 'all' = amount of all string and numerical values
- 'str' = amount of all string values
- totals=1,3,5,'spam','eggs',7,10 = list or tuple
- (sum(totals) = only numerical
- (sum(totals,'all') = numerical + strings
- (sum(totals,'str') = only string
- (sum(totals,[2,6]) = only numerical from 2 to 6-1
- (sum(totals,[4],'str') = only string from 0 to 4-1
- (sum(totals,'all',[3,999]) = numerical + strings from 3 to end
- """
- result,start,end,full_sum=0,0,len(totals),False
- if type(totals)==str:return len(totals)
- for i in args:
- if type(i)==str:full_sum=i
- elif type(i)==list or tuple:
- if len(i)==2:start,end=i[0],i[1]
- else: end=i[0]
- for field in totals:
- if start == 0 and end:
- if full_sum=='str':
- if type(field)==str:result+=len(field)
- else:
- if type(field)==int:result+=field
- else:
- if full_sum=='all':result+=len(field)
- start+=1
- end-=1
- start-=1
- return result
- if __name__ == '__main__':
- totals=1,3,5,'spam','eggs',7,10
- print(sum(totals))
- print(sum(totals,'all'))
- print(sum(totals,'str'))
- print(sum(totals,[2,6]))
- print(sum(totals,[4],'str'))
- print(sum(totals,'all',[3,999]))
Advertisement
Add Comment
Please, Sign In to add comment