We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
So here's an explanation for newbies (in Python 3 here).
First, the line :
def solveMeFirst(a,b):
tells you the program wants you to create a function. A function is a list of instructions step by step. We'll see this later, don't worry about it for now.
Then, the program asks the user to type two values : num1 and num2.
num1=int(input())num2=int(input())
It means : the variable num1 will be equal to what Hackerrank will type. Same with num2. Here, num1 and num2 will be 2 and 3.
Then :
res = solveMeFirst(num1,num2)
The program is creating a variable named "res" wich will contain the result of the function declared before ("solveMeFirst") with two parameters : the variables num1 and num2 typed before by the Hackerrank website. It basically means that you are injecting these variables inside the function for them to be exploited. In fact, when you create a function, you need to put some variables in it. It works as an instance. What exists in a function doesn't outside the function.
Finally :
print(res)
means that you are displaying the value of res (thus, the result of solveMeFirst).
Now that you know how to read the code, let's dive in the solution. The goal here is to complete the function solveMeFirst :
def solveMeFirst(a,b):
So here, you see that you're def**ining the function. However, for now, it is empty. See the **a and b as parameters? The variables num1 and num2 that you saw as parameters before :
res = solveMeFirst(num1,num2)
are injected in solveMeFirst and are now existing inside solveMeFirst as a (=num1) and b (=num2).
So what's the sum of num1 and num2 inside solveMeFirst to you?
Answer : no more than a (=num1) + b (=num2).
So under the lign which defines solveMeFirst, create a variable that will contain the sum. Let's call it c. Now, we have :
defsolveMeFirst(a,b):c=a+b
But there's one problem here : c is existing inside solveMeFirst but not outside so you can't print it.
To make it exist outside, you have to return the value of c. The return command will allow you to break the boundaries of the function in some sort.
Here's what it looks like :
defsolveMeFirst(a,b):c=a+breturnc
First, you're defining solveMeFirst with a and b as parameters to make them exist inside it.
Then, you're creating a variable c which will contain the result of the sum.
Finally, you make c exist outside the function.
And you're done !
Now, the value of c will be contained in the variable res as described in the line :
res = solveMeFirst(num1,num2)
Hope that helped.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Solve Me First
You are viewing a single comment's thread. Return to all comments →
So here's an explanation for newbies (in Python 3 here).
First, the line :
def solveMeFirst(a,b):
tells you the program wants you to create a function. A function is a list of instructions step by step. We'll see this later, don't worry about it for now.
Then, the program asks the user to type two values : num1 and num2.
It means : the variable num1 will be equal to what Hackerrank will type. Same with num2. Here, num1 and num2 will be 2 and 3.
Then :
res = solveMeFirst(num1,num2)
The program is creating a variable named "res" wich will contain the result of the function declared before ("solveMeFirst") with two parameters : the variables num1 and num2 typed before by the Hackerrank website. It basically means that you are injecting these variables inside the function for them to be exploited. In fact, when you create a function, you need to put some variables in it. It works as an instance. What exists in a function doesn't outside the function.
Finally :
print(res)
means that you are displaying the value of res (thus, the result of solveMeFirst).
Now that you know how to read the code, let's dive in the solution. The goal here is to complete the function solveMeFirst :
def solveMeFirst(a,b):
So here, you see that you're def**ining the function. However, for now, it is empty. See the **a and b as parameters? The variables num1 and num2 that you saw as parameters before :
res = solveMeFirst(num1,num2)
are injected in solveMeFirst and are now existing inside solveMeFirst as a (=num1) and b (=num2).
So what's the sum of num1 and num2 inside solveMeFirst to you? Answer : no more than a (=num1) + b (=num2). So under the lign which defines solveMeFirst, create a variable that will contain the sum. Let's call it c. Now, we have :
But there's one problem here : c is existing inside solveMeFirst but not outside so you can't print it.
To make it exist outside, you have to return the value of c. The return command will allow you to break the boundaries of the function in some sort.
Here's what it looks like :
First, you're defining solveMeFirst with a and b as parameters to make them exist inside it. Then, you're creating a variable c which will contain the result of the sum. Finally, you make c exist outside the function.
And you're done !
Now, the value of c will be contained in the variable res as described in the line :
res = solveMeFirst(num1,num2)
Hope that helped.