parameters - Simple adding in Python -


funnction name: lone_sum

parameters : integer value b : integer value c : integer value

given 3 integer values, a, b, c, return sum. however, if 1 of values same of values, not count towards sum.

return value: sum of a, b, , c, leaving out values.

i have , i'm lost it! missing?

def lone_sum(a,b,c):     t=0     if a!=b , b!=c:         t=a+b+c     elif a==b , a!=c:         t= a+c     elif a==c , a!=b:         t=a+b     elif b==c , b!=a:         t=b+a     elif a==b , b==c:         t=0     return t 

problems :

1)

a!=b , b!=c 

this test isn't enough : a equal c. (e.g. 1,0,1)

2)

if a==b , b==c, total should a, not 0, right?

solutions

modified code

def lone_sum(a,b,c):     t=0     if a!=b , b!=c , a!=c:         t = + b + c     elif a==b , a!=c:         t = + c     elif a==c , a!=b:         t = + b     elif b==c , b!=a:         t = b +     elif a==b , b==c:         t =     return t 

alternative

you pack values set remove duplicates :

def lone_sum(a,b,c):     return sum(set([a,b,c]))  print(lone_sum(1,2,3)) # 6 print(lone_sum(1,2,2)) # 3 print(lone_sum(3,3,3)) # 3 

note behaviour corresponds description, not code (last example 0).

as bonus, it's trivial adapt function n values :

def lone_sum(*values):     return sum(set(values)) 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -