Your First Coding Challenge in Python: Sum of Two Numbers


In this lesson we will introduce you to how Coding Challenges / Coding Questions work and how to approach solving them. Watch this video first:



Now let's check your first coding challenge:

Sum of Two Numbers

You are given two numbers a and b and you should return their sum.

Examples:

Input: a = 4, b = 9

Output: 13

Explanation:
The sum of a and b is 4 + 9, which is 13.

This is the challenge statement. It explains what the challenge asks us to do and provides some examples to make sure we understand the statement.

Each challenge gives us some input and asks us to write some code that successfully takes that input and uses it to produce some desired output.

In this case, the input is two numbers a and b and the output is their sum.


The class:

If you look at the code editor, you will see that everything is written inside a class:

class Solution:

You don't need to know anything about classes at this point, we will study them in future lessons. Just leave that code untouched.


The method:

Inside this class, we will always be provided with a method. Inside this method is where we should write our solution.

In our case the method is:

def sum(self, a: int, b: int) -> int:
    # Your solution goes here

A method is just a function inside a class. When defining methods, we always have self as the first parameter. Don't dwell on this, we will study methods in future lessons.


Parameters and return:

This method's parameters will always match the input described in the challenge's statement. In our case, the parameters are a and b, the two numbers we are given.

The method should return a value which represents the desired output described in the challenge's statement. In our case, it should return a + b.


Understanding the Problem

The core challenge of this problem is very straightforward: you need to add two numbers and return the result. This is a fundamental operation in programming and is often used as a warm-up exercise to get familiar with the syntax of a new language or environment.

Common applications of this problem include basic arithmetic operations in calculators, financial calculations, and any scenario where two quantities need to be combined.

Potential pitfalls include misunderstanding the input types or overcomplicating the solution. Since the problem is simple, it's crucial to focus on writing clean and correct code.

Approach

To solve this problem, you need to think about the following steps:

  1. Read the input values.
  2. Perform the addition operation.
  3. Return the result.

Let's start with a naive solution:

def sum(a, b):
    return a + b

This solution is already optimal for this problem. The addition operation is a constant time operation, O(1), and there are no further optimizations needed.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Define a method sum that takes two parameters, a and b.
  2. Inside the method, return the result of a + b.

Code Implementation

Let's implement the solution inside the provided class:

class Solution:
    def sum(self, a: int, b: int) -> int:
        # Return the sum of a and b
        return a + b

Explanation of the code:

Complexity Analysis

The time complexity of this solution is O(1) because the addition operation takes constant time.

The space complexity is also O(1) because no additional space is required beyond the input parameters.

Edge Cases

Potential edge cases include:

These edge cases are handled correctly by the addition operation.

Testing

To test the solution comprehensively, you can use the following test cases:

def test_sum():
    solution = Solution()
    assert solution.sum(4, 9) == 13
    assert solution.sum(0, 0) == 0
    assert solution.sum(-5, 10) == 5
    assert solution.sum(-3, -7) == -10
    print("All test cases pass")

test_sum()

This test function covers a variety of cases, from simple to complex.

Thinking and Problem-Solving Tips

When approaching such problems, keep the following tips in mind:

To develop problem-solving skills, practice regularly on coding challenge platforms and study different algorithms.

Conclusion

In this blog post, we covered the problem of summing two numbers. We discussed the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong programming skills. Keep practicing and exploring further!

Additional Resources

For further reading and practice problems, check out the following resources: