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.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Warmup
  4. Solve Me First
  5. Discussions

Solve Me First

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 752 Discussions, By:

votes

Please Login in order to post a comment

  • maiya_o
    5 years ago+ 25 comments

    Is there anyone who is able to give a tutorial on what this problem is asking for. I find the explanation to be completely confusing and nonsensical. Maybe if you already understand the concepts, it makes sense; but to a new person, it's not explained well.

    94|
    Permalink
    View more Comments..
  • purplepowder
    1 year ago+ 1 comment

    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 :

    def solveMeFirst(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 :

    def solveMeFirst(a,b):
        c = a+b
        return c
    

    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.

    25|
    Permalink
  • brentonstrine
    7 years ago+ 5 comments

    Is the Javascript version using some sort of framework? I've never seen process.stdin before. Is that Node.js? I'm super confused about what this is doing.

    18|
    Permalink
    View more Comments..
  • yash_900209
    5 years ago+ 3 comments

    Solution in C# :)

    using System;
    using System.Collections.Generic;
    using System.IO;
    class Solution {
        static int solveMeFirst(int a, int b) { 
          int sum=0;
            sum=a+b;
          return sum;
          
        }
        static void Main(String[] args) {
            int val1 = Convert.ToInt32(Console.ReadLine());
            int val2 = Convert.ToInt32(Console.ReadLine());
            int sum = solveMeFirst(val1,val2);
            Console.WriteLine(sum);
        }
    }
    
    5|
    Permalink
  • melihmucuk
    6 years ago+ 2 comments

    line 15 should be for Go:

    fmt.Scanf("%v\n %v\n", &a,&b)
    
    2|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature