All problems
Character Vocabulary
Problem
Build a character-level vocabulary from a text string.
Return a dict mapping each unique character to its rank in sorted order, so the alphabetically first character gets index 0. An empty string yields an empty dict.
Hint
Sorting the unique characters makes the vocabulary deterministic.
Test cases
case 1
in: ["cab"]
expected: {"a":0,"b":1,"c":2}
case 2
in: ["hello"]
expected: {"e":0,"h":1,"l":2,"o":3}
case 3
in: [""]
expected: {}
case 4
in: ["a"]
expected: {"a":0}
Starter codePython
def char_vocab(text):
# Your code here
pass4 test cases · run your code against them in the editor