Problem overview
This lesson explains how challenges work, using a tiny one: given two numbers a and b, return their sum. For a = 4 and b = 9 the answer is 13.
Every challenge has the same shape: some input goes in, and your code must produce the matching output. The statement describes both and gives examples to confirm you understood. Here the input is two numbers and the output is one number.
How a challenge is laid out
The class
The editor starts with a class:
class Solution {
}
A class groups code together. You don't need to know more about classes yet; leave this wrapper as it is and write inside it.
The method
Inside the class there is a method, and that is where your solution goes. A method is a function that lives inside a class. In this challenge it is:
def sum(a: Int, b: Int): Int = {
// Your solution goes here!
}
Read the header from left to right:
defstarts a function or method definition.sumis its name. Keep the names you are given, because the lesson's tests call the method by that name.(a: Int, b: Int)lists the parameters. Each one is written as a name, a colon and a type.Intis a 32-bit whole number.- The
: Intafter the parentheses is the return type: the method must produce anInt. - The
=and the braces introduce the body, where your code goes.
Parameters and return
The parameters always match the input in the statement. Inside the method you use a and b as ordinary values. The method must then return a value that matches the output in the statement, here the sum.
Step by step
- Find the input and the output. The input is
aandb. The output isa + b. - Add the two parameters with the
+operator. ForIntvalues it adds numerically. - Return the result so the caller receives it. Returning hands the value back to whoever called
sum.
The solution in Scala
class Solution {
def sum(a: Int, b: Int): Int = {
// Write the first step below:
return a + b
}
}
A few details about this code:
returnis optional in Scala. The value of the last expression in a block is the block's value, so writing justa + bas the last line does exactly the same thing. This lesson usesreturnto make the idea explicit. Many Scala programmers leave it out.- Return types are checked. The method says it returns an
Int, anda + bof twoIntvalues is anInt, so the types fit. - Comments. Text after
//to the end of the line is ignored. The comment in the starter code only marks where to type. - Parameters can't be reassigned. Method parameters in Scala are immutable, so
aandbkeep the values they were given.
Edge cases and common mistakes
- Negative numbers and zero. They need no special handling.
sum(-4, 9)is5, andsum(0, 0)is0. - Overflow. An
Intholds values up to 2,147,483,647 and wraps silently past it, with no error.sum(2147483647, 1)gives-2147483648. The challenge usesIntinputs of moderate size, but remember this limit when you move to bigger numbers:Longis the 64-bit type. - Forgetting to return. A body with no final expression doesn't give the method a value, and the compiler rejects it because the method promises an
Int. - Printing instead of returning. A
printlnshows a value on the console, but the tests read what the method returns. - Editing the class or method header. Changing a name or a type means the tests can no longer call your code.
Testing
a = 4,b = 9gives13.a = -4,b = 9gives5.a = 0,b = 0gives0.a = 2147483647,b = 1gives-2147483648, because of the wraparound.
