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
firstare at indexes 0 and 1, so the slice isfirst[0:2]. - The last 3 letters of
laststart at indexlen(last)-3and run to the end, so the slice islast[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
- Take the first two letters of
firstwith the slice from 0 up to 2. - Find where the last three letters begin: the length of
lastminus 3. - Slice from that index to the end by leaving out the end index.
- 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. Writingfirst[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 writtenfirst[:2], andlast[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)-3is 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, andlast[len(last)-4:]gives 4. - Changing the case. The ID keeps each letter as written, so no call to
strings.ToUpperorstrings.ToLoweris needed. - Swapping the arguments. The first name comes first in the call and in the ID; the 2 letters come from
firstand the 3 letters fromlast.
Testing
"John","Smith"gives"Joith"."Jo","Doe"gives"JoDoe"."ada","Lovelace"gives"adace".
