Start learning free

LESSON 71 OF 285 · STRINGS - CHALLENGES

Creating IDs in Go: Slicing the Start and End of Strings

Practice Read the full explanation

A website creates a user ID from each person's name: the first 2 letters of the first name, followed by the last 3 letters of the last name.

Given the first name first and the last name last, return the ID.

 

Example 1:

Input: first = "John", last = "Smith"
Output: "Joith"
Explanation: "Jo" are the first 2 letters of "John" and "ith" are the last 3 letters of "Smith".

Example 2:

Input: first = "Jo", last = "Doe"
Output: "JoDoe"
Explanation: "Jo" has exactly 2 letters and "Doe" exactly 3, so both names are used whole.

Example 3:

Input: first = "ada", last = "Lovelace"
Output: "adace"
Explanation: The letters keep their case: the ID doesn't change any letter to upper or lower case.

 

Constraints:

  • first has at least 2 letters and last has at least 3 letters.
  • Both names are made of English letters only, lowercase or uppercase.
STEP 1 OF 1
    Ctrl + Enter to check

    GO · STRINGS - CHALLENGES

    Creating Ids in Go, explained

    Problem overview

    The function creatingIds receives a first name first and a last name last. It returns a user ID made of the first 2 letters of the first name followed by the last 3 letters of the last name.

    For "John" and "Smith" the ID is "Joith": Jo from John, ith from Smith. When the names are exactly as long as the parts you take, they are used whole: "Jo" and "Doe" give "JoDoe". The letters keep their case, so "ada" and "Lovelace" give "adace".

    Approach

    Each half of the ID is a piece of a string, and Go's slice expression s[low:high] cuts such a piece. The piece starts at index low and stops just before index high.

    • The first 2 letters of first are at indexes 0 and 1, so the slice is first[0:2].
    • The last 3 letters of last start at index len(last)-3 and run to the end, so the slice is last[len(last)-3:]. Go has no negative indexes, so the start has to be computed from the length.

    Joining the two slices with + gives the ID. Because both names are guaranteed to be long enough, neither slice can fall outside its string.

    Step by step

    1. Take the first two letters of first with the slice from 0 up to 2.
    2. Find where the last three letters begin: the length of last minus 3.
    3. Slice from that index to the end by leaving out the end index.
    4. Join the two pieces with + and return them.

    The solution in Go

    func creatingIds(first string, last string) string {
        return first[0:2] + last[len(last)-3:]
    }
    

    Running it on the three examples gives these results:

    first = "John", last = "Smith"    -> "Joith"
    first = "Jo",   last = "Doe"      -> "JoDoe"
    first = "ada",  last = "Lovelace" -> "adace"
    

    Some details of Go's slices worth keeping in mind:

    • The end index is excluded. first[0:2] ends before index 2, so it holds exactly the characters at 0 and 1. Writing first[0:3] would give three letters.
    • Omitted bounds have defaults. A missing low bound means 0 and a missing high bound means the length. So first[0:2] can also be written first[:2], and last[len(last)-3:] goes to the end.
    • Bounds are checked at run time. A bound beyond the length of the string stops the program with a panic. For example, slicing "a" up to index 2 fails with "slice bounds out of range [:2] with length 1". The constraints rule that out here.
    • Slicing counts bytes. That matches letters for English names, which is what the constraints promise. A letter such as é occupies two bytes, and a slice could cut it in the middle.

    Edge cases and common mistakes

    • Names of exactly the minimum length. With "Jo" and "Doe", len(last)-3 is 0, so the second slice is the whole name. Nothing special is needed.
    • Taking the last letters from the wrong end. last[:3] is the first three letters, so "Smith" would give "Smi" instead of "ith".
    • Off by one on the start. last[len(last)-2:] gives only 2 letters, and last[len(last)-4:] gives 4.
    • Changing the case. The ID keeps each letter as written, so no call to strings.ToUpper or strings.ToLower is needed.
    • Swapping the arguments. The first name comes first in the call and in the ID; the 2 letters come from first and the 3 letters from last.

    Testing

    • "John", "Smith" gives "Joith".
    • "Jo", "Doe" gives "JoDoe".
    • "ada", "Lovelace" gives "adace".
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free