List Contact Names
Part of the Logic & Flow section of Coddy's Kotlin journey. Lesson 24 of 63.
Challenge
HardComplete contactBook with parameters commands: List<String>, names: List<String>, phones: List<String>. Build an in-memory contact book that starts empty for each function call. The three lists have equal length, at most 100. At each index, commands selects an operation; names and phones supply its arguments. Names are nonempty lowercase ASCII words; phones are nonempty digit strings and must stay strings to preserve leading zeros. Every command produces one reply in the returned list. Keep everything you wrote in the previous steps, including all earlier commands and replies. Support PUT: save or replace the phone for the supplied name, then reply saved. Add FIND: reply with the stored phone for the supplied name, or missing. Its phone argument is ignored. Add REMOVE: remove the supplied name and reply removed if it existed or missing otherwise. Its phone argument is ignored. Add LIST: reply with the sorted list of saved names in Kotlin's standard bracketed format, for example [ada, bo], or [] when empty. Both argument values are ignored. Use contacts.keys.sorted().toString() to create this one reply string. Inputs contain only commands supported in this step. Ignored argument strings can be any nonempty text.
Unless stricter bounds are stated above, collections contain at most 100 elements at each level, and integer arguments and integer collection values are between -1000 and 1000. Text supplied for parsing can include invalid or out-of-range representations as described.
Return a value of type List<String>. Keep the supplied input/output code. It reads scalar arguments one per line; a list starts with its count followed by its elements, and a map starts with its entry count followed by each key and value. Nested lists repeat the count-and-elements format for each row. The supplied main prints the return value followed by one newline. Lists use Kotlin's standard bracketed format; print no additional labels.
Try it yourself
fun contactBook(commands: List<String>, names: List<String>, phones: List<String>): List<String> {
val contacts = mutableMapOf<String, String>()
val replies = mutableListOf<String>()
for (i in commands.indices) {
val name = names[i]
when (commands[i]) {
"PUT" -> {
contacts[name] = phones[i]
replies.add("saved")
}
"FIND" -> replies.add(contacts[name] ?: "missing")
"REMOVE" -> {
if (contacts.containsKey(name)) {
contacts.remove(name)
replies.add("removed")
} else replies.add("missing")
}
}
}
return replies
}
fun main() {
val commandsCount = readln().toInt()
val commands = mutableListOf<String>()
for (i0 in 0 until commandsCount) {
val commandsItem = readLine().orEmpty()
commands.add(commandsItem)
}
val namesCount = readln().toInt()
val names = mutableListOf<String>()
for (i0 in 0 until namesCount) {
val namesItem = readLine().orEmpty()
names.add(namesItem)
}
val phonesCount = readln().toInt()
val phones = mutableListOf<String>()
for (i0 in 0 until phonesCount) {
val phonesItem = readLine().orEmpty()
phones.add(phonesItem)
}
println(contactBook(commands, names, phones))
}All lessons in Logic & Flow
4Nested Collections
Lists of ListsTraversing RowsRow SummariesFlattening Nested ListsRecap - Seating Rows7Collection Pipelines
Transforming with MapFiltering ValuesTesting Collection RulesAccumulating with FoldRecap - Approved Totals5Project - Contact Book
Save ContactsFind ContactsPractice on your own: Kotlin playground