One-hot encoding

Write codeEasy18 min

Your task

One-hot encode a list of categorical string labels. First build the sorted list of unique categories. Then return a list of one-hot row vectors (lists of 0/1 ints), one per input label, where each row has a 1 in the column corresponding to that label's position in the sorted unique-category list and 0 elsewhere. The input list is non-empty.

Examples

one_hot_encode(labels: list[str]) -> list[list[int]]
Example 1
Inputone_hot_encode(["cat", "dog", "cat"])
Output[[1, 0], [0, 1], [1, 0]]