1

Funnction Name: lone_sum

Parameters a : an integer value b : an integer value c : an integer value

Given 3 integer values, a, b, c, return their sum. However, if one of the values is the same as another of the values, it does not count towards the sum.

Return Value: The sum of a, b, and c, leaving out like values.

I have this and I'm completely lost with it! What am I missing?

def lone_sum(a,b,c):
    t=0
    if a!=b and b!=c:
        t=a+b+c
    elif a==b and a!=c:
        t= a+c
    elif a==c and a!=b:
        t=a+b
    elif b==c and b!=a:
        t=b+a
    elif a==b and b==c:
        t=0
    return t
1
  • 1
    Your last elif is not consistent with the others: if all 3 are equals according to the other "elifs" you are considering only one of the "dups" but in the last elif you don't consider any of them. Commented Apr 7, 2017 at 22:05

2 Answers 2

3

Problems :

1)

a!=b and b!=c

This test isn't enough : a could be equal to c. (e.g. 1,0,1)

2)

If a==b and b==c, total should be a, not 0, right?

Solutions

Modified code

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

Alternative

You could simply pack the values into a set to remove the 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 that this behaviour corresponds to your description, not to your code (last example would be 0).

As a bonus, it's trivial to adapt the function to n values :

def lone_sum(*values):
    return sum(set(values))
Sign up to request clarification or add additional context in comments.

Comments

0

You could use Set to work with unique values, and reduce to sum them:

from sets import Set

def lone_sum(a,b,c):
   nums = Set([a,b,c])
   return reduce(lambda x,y: x+y, nums)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.