Все задачи
Легко Решено: 0 Процент решений: 0%

One Shared Configuration

Simulate a singleton settings object: every part of the program reads and writes the same instance.

Чему вы научитесь

  • Share one object across the whole program
  • Create an instance lazily on first use
  • Read and update settings through one place

Условие

Показать перевод

The singleton pattern makes sure a class has exactly one instance that everyone shares, a common choice for application settings.

Simulate one shared configuration that starts empty. Process the operations in order:

  • set <key> <value>: store the value for the key (replacing any old value).
  • get <key>: read the key's current value, or missing if it was never set.

Because every caller uses the same instance, a value set by one operation is visible to every later get.

Implement shared_configuration(operations).

Input

operations: Strings in the two forms above. Keys and values contain no spaces.

Output

Return the results of the get operations, in order.

Ограничения

1 ≤ number of operations ≤ 10,000

Keys and values have 1 to 20 characters

Примеры

Пример 1
Ввод:
operations = ["set theme dark", "get theme", "get language"]

Ожидаемый вывод:
expected = ["dark", "missing"]
Ввод:
std::vector<std::string> operations = {"set theme dark", "get theme", "get language"};

Ожидаемый вывод:
std::vector<std::string> expected = {"dark", "missing"};
Ввод:
string[] operations = new string[] {"set theme dark", "get theme", "get language"};

Ожидаемый вывод:
string[] expected = new string[] {"dark", "missing"};

Пояснение

theme was set to dark; language was never set, so it is missing: ["dark", "missing"].

Пример 2
Ввод:
operations = ["set lang en", "set lang vi", "get lang"]

Ожидаемый вывод:
expected = ["vi"]
Ввод:
std::vector<std::string> operations = {"set lang en", "set lang vi", "get lang"};

Ожидаемый вывод:
std::vector<std::string> expected = {"vi"};
Ввод:
string[] operations = new string[] {"set lang en", "set lang vi", "get lang"};

Ожидаемый вывод:
string[] expected = new string[] {"vi"};

Пояснение

The second set replaces the first value in the same shared instance: ["vi"].

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

All operations read and write the same single object.

Подсказка 2

A dictionary from key to value is enough to hold the settings.

Подсказка 3

A key that was never set must return the text missing.

Ожидаемая сложность

O(number of operations) time, O(number of keys) extra space

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.