Word-Level Translator
Given a word-level translation dictionary and a source sentence, translate each word. If a word is not in the dictionary, keep it unchanged.
Input format:
- Line 1: Number of dictionary entries
- Next n lines: "source_word target_word"
- Last line: Source sentence to translate
All matching is case-insensitive, but output the translation in its original form from the dictionary.
Function: translate(dictionary, sentence) receives the entries already parsed into a dictionary (lowercase source word → target word) and the sentence as a string. Return the translated sentence as a string, with words separated by single spaces.
Example
3 hello hola world mundo good bueno hello world
hola mundo
- Inputs:
dictionaryholds three source → target pairs with lowercase keys: hello → hola, world → mundo, good → bueno.sentenceishello world, two words separated by a space. - Word 1: "hello", compared without regard to case, matches the key hello, so it is replaced by
hola. - Word 2: "world" matches the key world and becomes
mundo. The pair good → bueno is never used: the dictionary may contain far more words than the sentence does. - Output: the translated words in their original order, separated by single spaces:
hola mundo. Every source word maps to exactly one output word, so the output has the same word count (2) as the input. - Notice: case is ignored only when matching. The replacement is copied from the dictionary exactly as stored, and a word with no entry passes through untouched with its original capitalisation, so "Hello there World" would give "hola there mundo".
Constraints
- Case-insensitive lookup
- Unknown words kept as-is
- Output: Translated sentence