1. 풀어야하는 문제

 

 

 - Challenge goals:
Using everything we learned, make a Dictionary class with the following methods:

 

 - class 내에 구현해야하는 method 10가지
1. add: 단어를 추가함.
2. get: 단어의 정의를 리턴함.
3. delete: 단어를 삭제함.
4. update: 단어를 업데이트 함.
5. showAll: 사전 단어를 모두 보여줌.
6. count: 사전 단어들의 총 갯수를 리턴함.
7. upsert 단어를 업데이트 함. 존재하지 않을시. 이를 추가함. (update + insert = upsert)
8. exists: 해당 단어가 사전에 존재하는지 여부를 알려줌.
9. bulkAdd: 다음과 같은 방식으로. 여러개의 단어를 한번에 추가할 수 있게 해줌. [{"term":"김치", "definition":"대박이네~"}, {"term":"아파트", "definition":"비싸네~"}]
10. bulkDelete: 다음과 같은 방식으로. 여러개의 단어를 한번에 삭제할 수 있게 해줌. ["김치", "아파트"]


 - Requirements:
Use class
Use typedefs
Use List
Use Map

 

 

 

 

 

2. 해답 코드 예시

 

typedef Term = String;
typedef Definition = String;
typedef DictionaryMap = Map<Term, Definition>;

class Dictionary {
  DictionaryMap words = {};
  
  void add(Term term, Definition definition) {
    words[term] = definition;
  }
  
  // return값인 words[term]의 결과가 null일 수도 있기 때문에 Definition 뒤에 ?를 붙입니다.
  Definition? get(Term term) {
    return words[term];
  }
  
  void delete(Term term) {
    words.remove(term);
  }
  
  void update(Term term, Definition definition) {
    words[term] = definition;
  }
  
  List<Definition> showAll() {
    return words.keys.toList();
  }
  
  int count() {
    return words.length;
  }
  
  void upsert(Term term, Definition definition) {
    words[term] = definition;
  }
  
  bool exists(Term term) {
    return words.containsKey(term);
  }
  
  void bulkAdd(List<Map<String, String>> tryAddList) {
    for (Map<String, String> eachTryAdd in tryAddList) {
      words[eachTryAdd["term"]!] = eachTryAdd["definition"]!;
    }
  }
  
  void bulkDelete(List<Map<String, String>> tryRemoveList) {
    tryRemoveList.forEach((Map<String, String> eachTryRemove)
      => words.remove(eachTryRemove["term"]));
  }
  
}

void main() {
  
  var dataForTest
    = [
    {"term":"김치", "definition":"대박이네~"}, 
    {"term":"아파트", "definition":"비싸네~"}
  ];
  
  var test = Dictionary();
  
  test.bulkAdd(dataForTest);
  print(test.showAll());
  
  test.bulkDelete(dataForTest);
  print(test.showAll());
}

 

 

 

+ Recent posts