One-hot encoding
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.
Implement
one_hot_encode(labels: list[str]) → list[list[int]]Examples
in
[["cat","dog","cat"]]out[[1,0],[0,1],[1,0]]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 18 min
solution.py
InputExpectedGot
[["cat","dog","cat"]][[1,0],[0,1],[1,0]]not run yetsample