-2

I have to find the sum of a range between the values a and b, although either can be a negative number. If they are the same number I should just return that number. A complete beginner here. Stuck on a Code-Wars kata.

Apparently, my code returns None. I don't necessarily want the solution to the problem. I more want to know why my code is wrong. (The first line of the code is given)

def get_sum(a,b):
    if a == b:
        return a
    
    num = 0
    if a > b:
        for i in range(a, b):
            num += i
            return num
    elif a < b:
        for i in range(b, a):
            num += i
            return num
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
VibesT93
  • 5
  • 1
  • _"I more want to know why my code is wrong"_ no, _you_ need to tell us what's wrong with it. The first step to being able to fix your code is identifying what's wrong with it. Please see [ask]. Welcome to Stack Overflow! – Pranav Hosangadi Jan 25 '23 at 15:56
  • 1
    What's the one function in your code that you didn't write? Try looking up the documentation for that. – picobit Jan 25 '23 at 15:57
  • 1
    Your idea for the solution is not wrong. You have two problems: as @imM4TT mentions, the indentation for the `return` statement is wrong. Your other problem is the order of parameters in the `range` expression. You need the smaller one first. – Jeanot Zubler Jan 25 '23 at 15:59
  • @JeanotZubler yes, I didn't notice that. After OP realizes that typo, their next question will be "why does it always return the first number", so I preemptively suggested a duplicate :) – Pranav Hosangadi Jan 25 '23 at 16:02

4 Answers4

3

I think there is an indentation issue located on the return instruction and also a problem on the sign greater than.

def get_sum(a,b):
    if a == b:
        return a
    
    num = 0
    if a < b:
        for i in range(a, b):
            num += i
        return num
    elif a > b:
        for i in range(b, a):
            num += i
        return num

You could also use built-in functions to make it faster and more concise

sum(range(a, b))
imM4TT
  • 292
  • 1
  • 8
3

Thinking about the problem itself, rather than your particular function, you could use Gauss's method: reverse the sequence, add it to itself, and the total will be twice the sum sought. However, each term is now equal, so you have reduced the question to a multiplication.

1 + 2 + 3 + 4
4 + 3 + 2 + 1
-------------
5 + 5 + 5 + 5 = 20

20/2 = 10

In Python this would be:

def get_sum(small, large):
    return int((large - small + 1) * (small + large) / 2)
nimasmi
  • 3,978
  • 1
  • 25
  • 41
  • 3
    and if you take the asolute value of the difference, you can even ignore which number is large and which is small: `return (abs(a - b) + 1) * (a + b) / 2` – Jeanot Zubler Jan 25 '23 at 16:18
0

You can use the gauss formula: n(n+1)/2

https://letstalkscience.ca/educational-resources/backgrounders/gauss-summation

def gauss(n):
   return (abs(n)*(abs(n)+1)/2)* (-1 if n < 0 else 1)

def sum_between(a, b):
   a,b = min(a, b), max(a, b)
   return gauss(b) - gauss(abs(a))

You can actually transform sum_between in a single formula with a bit of algebra.

For the same number then just add an if

Axeltherabbit
  • 680
  • 3
  • 20
0

There's always

def get_sum(a, b):
    return sum(range(min(a, b), max(a, b)))

add 1 to the lower-bound if you want numbers strictly between, or add 1 to the upper bound if you want it included in the sum.

It might not be as pedagogical as writing it out yourself and it doesn't rely on math(s).

thebjorn
  • 26,297
  • 11
  • 96
  • 138