Kotlinlearncs.online LogoJava
Return to List

Homework: Section Lists to Map

Created By: Geoffrey Challen
/ Version: 2022.2.0

Write a method called sectionListsToMap that, given a List of Strings, parses it into a Map<String, MutableSet<String>> as follows. Each String in the passed list contains a comma-separated list of names of people in a discussion section. The first name is the section leader, and the rest are students. Your map should map each section leader to the set of students in their section. No section leader or student will appear twice in the data set.

For example, given the Strings "challen,student1", "ruisong4,student2, student3" and "friendly,student4, student5", your map would have keys "challen", "ruisong4", and "friendly". "challen" would map to a set containing "student1", "ruisong4" would map to a set containing "student2" and "student3", and so on.

A few hints for approaching this problem. First, consider how to use .split and .trim appropriately to parse the input String. You should get this part to work before proceeding. Then consider when you need to create the map and each set, and how to populate them.

You should not need import statements to solve this problem. Rather, create your maps and sets using mutableMapOf() and mutableSetOf() where appropriate.

Related Lessons

Stuck? You may find these lessons helpful: