Common Python Coding Interview Questions: Part 7
A breakdown of a common Python problem given in interviews
Write a function that calculates the sum of two integers without using the + or – operators…
def get_sum(a, b):
nums = []
nums.append(a)
nums.append(b)
return sum(nums)
Step 1: Declare the function
Since you are asked to write a function, it’s important to do so! Right away you should type ‘def’ and come up with an appropriate name for your function. Since the prompt tells you that the function will be taking in two numbers, be sure to include two arguments in your function declaration that will represent those numbers.
Step 2: Create an empty list
After reading the prompt, you should be thinking to yourself: “How can I calculate a sum in Python without using + or –?” My first thought is to use the sum function, which finds the sum of all numbers in a list. Therefore, the first thing we have to do is create an empty list that is going to store our input numbers.