Created at : 2025-01-14 21:06
Auther: Soo.Y


๐Ÿ“๋ฉ”๋ชจ

๋‚ด๊ฐ€ ์ œ์ถœํ•œ ์ฝ”๋“œ

typedef MyDictionary = Map<String, String>;
 
class myClass {
  MyDictionary myMap = {};
 
  void add(String term, String definition) => myMap[term] = definition;
 
  String? get(String term) {
    return myMap[term];
  }
 
  void delete(String term) => myMap.remove(term);
 
  void showAll() => myMap.forEach((term, definition) => print('${term}: ${definition}'));
 
  void count() => print('์‚ฌ์ „ ๋‹จ์–ด ์ด ์ˆ˜ : ${myMap.length} ์ž…๋‹ˆ๋‹ค.');
 
  void upsert(String term, String definition) => myMap[term] = definition;
 
  bool exists(String term) {
    return myMap.containsKey(term);
  }
 
  void bulkAdd(List<MyDictionary> words) {
    words.forEach((for_word) => myMap['${for_word['term']}'] = '${for_word['definition']}');
  }
 
  void bulkDelete(List<String> words) {
    words.forEach((for_word) => myMap.remove(for_word));
  }
 
}
 
void main() {
 
  myClass my_dict1 = myClass();
  print('=========๋‹จ์–ด ๊ฐœ๋ณ„ ์ž…๋ ฅ==========');
  my_dict1.add('๊น€์น˜','๋ง›์žˆ๋‹ค.');
  my_dict1.add('์•„ํŒŒํŠธ','๋น„์‹ธ๋‹ค.');
  my_dict1.add('Dart','์–ด๋ ต๋‹ค.');
 
  my_dict1.showAll();
  my_dict1.count();
  print('=========๊น€์น˜ ๊ฒ€์ƒ‰===========');
  print(my_dict1.get('๊น€์น˜'));
  print('=========๊น€์น˜ ๋‹จ์–ด ์—…๋ฐ์ดํŠธ===========');
  my_dict1.upsert('๊น€์น˜', '์‚ฌ๋ž‘ํ•ด');
  my_dict1.showAll();
  print('=========๋‹จ์–ด ์กด์žฌ ์—ฌ๋ถ€ exists===========');
  print(my_dict1.exists('๊น€์น˜'));
  print('=========ํŠน์ • ๋‹จ์–ด ์‚ญ์ œ===========');
  my_dict1.delete('๊น€์น˜');
  my_dict1.showAll();
  print('=========์—ฌ๋Ÿฌ ๋‹จ์–ด ์ž…๋ ฅ===========');
  my_dict1.bulkAdd([{"term":"๊น€์น˜", "definition":"๋Œ€๋ฐ•์ด๋„ค~"}, {"term":"์•„ํŒŒํŠธ", "definition":"๋น„์‹ธ๋„ค~"}]);
  my_dict1.showAll();
  print('=========์—ฌ๋Ÿฌ ๋‹จ์–ด ์‚ญ์ œ===========');
  my_dict1.bulkDelete(["๊น€์น˜", "์•„ํŒŒํŠธ"]);
  my_dict1.showAll();
 
}
 

๊ณต์‹ ์ •๋‹ต

class Word {
  final String term;
  final String definition;
  Word({
    required this.term,
    required this.definition,
  });
}
 
typedef WordsInput = List<Map<String, String>>;
 
class Dictionary {
  Map<String, Word> words = {};
 
  bool exists(String term) {
    return words.containsKey(term);
  }
 
  Word? get(String term) {
    return words[term];
  }
 
  void add(String term, String definition) {
    if (!exists(term)) {
      words[term] = Word(
        term: term,
        definition: definition,
      );
    }
  }
 
  void showAll() {
    print("----");
    words.forEach((key, value) {
      print("${value.term}: ${value.definition}\n");
    });
    print("----");
  }
 
  int count() {
    return words.length;
  }
 
  void update(String term, String definition) {
    if (exists(term)) {
      words[term] = Word(
        term: term,
        definition: definition,
      );
    }
  }
 
  void delete(String term) {
    if (exists(term)) {
      words.remove(term);
    }
  }
 
  void upsert(String term, String definition) {
    if (exists(term)) {
      update(term, definition);
    } else {
      add(term, definition);
    }
  }
 
  void bulkAdd(WordsInput words) {
    for (var word in words) {
      if (word.containsKey('term') && word.containsKey('definition')) {
        add(word["term"] ?? "", word["definition"] ?? "");
      }
    }
  }
 
  void bulkDelete(List<String> keys) {
    for (var key in keys) {
      delete(key);
    }
  }
}
 
void main() {
  var dictionary = Dictionary();
 
  dictionary.add("๊น€์น˜", "ํ•œ๊ตญ ์Œ์‹");
  dictionary.showAll();
 
  // Count
  print(dictionary.count());
 
  // Update
  dictionary.update("๊น€์น˜", "๋ฐ‹์žˆ๋Š” ํ•œ๊ตญ ์Œ์‹!!!");
  print(dictionary.get("๊น€์น˜"));
 
  // Delete
  dictionary.delete("๊น€์น˜");
  print(dictionary.count());
 
  // Upsert
  dictionary.upsert("๊น€์น˜", "๋ฐ‹์žˆ๋Š” ํ•œ๊ตญ ์Œ์‹!!!");
  print(dictionary.get("๊น€์น˜"));
  dictionary.upsert("๊น€์น˜", "์ง„์งœ ๋ฐ‹์žˆ๋Š” ํ•œ๊ตญ ์Œ์‹!!!");
  print(dictionary.get("๊น€์น˜"));
 
  // Exists
  print(dictionary.exists("๊น€์น˜"));
 
  // Bulk Add
  dictionary.bulkAdd([
    {"term": "A", "definition": "B"},
    {"term": "X", "definition": "Y"}
  ]);
  dictionary.showAll();
 
  // Bulk Delete
  dictionary.bulkDelete(["A", "X"]);
  dictionary.showAll();
}
 

TA๊ฐ€ ์ œ๊ณตํ•ด์ค€ ์ •๋‹ต

TA ๊ฐ€ ์ œ์‹œํ•˜๋Š” ์†”๋ฃจ์…˜
class ๋Š” ์†์„ฑ๊ณผ ๋™์ž‘์„ ํ•˜๋‚˜์˜ ํ…œํ”Œ๋ฆฟ์œผ๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑŒ๋ฆฐ์ง€์˜ ๊ฒฝ์šฐ Dictionary ๋Š” words ๋ฅผ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๊ณ  add, get.. ๋“ฑ๊ณผ ๊ฐ™์€ ๋™์ž‘์„ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. typedef ๋Š” Dart์˜ ๊ธฐ๋ณธ type ์„ ์‚ฌ์šฉ์ž ์ •์˜์˜ ์ด๋ฆ„์œผ๋กœ aliasing ํ•ด์ค๋‹ˆ๋‹ค. List ๋Š” ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ํ•ญ๋ชฉ๋“ค์˜ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. List๋Š” ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ์˜ ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•˜๋ฉฐ, ๊ฐ ํ•ญ๋ชฉ์€ ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธ๋ฑ์Šค๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. Map ์€ ํ‚ค-๊ฐ’ ์Œ์„ ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๊ฐ ํ‚ค๋Š” ๊ณ ์œ ํ•ด์•ผ ํ•˜๋ฉฐ, ๊ฐ ํ‚ค๋Š” ํ•˜๋‚˜์˜ ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํŠน์„ฑ ๋•Œ๋ฌธ์— Map์€ ๋น ๋ฅด๊ฒŒ ํŠน์ • ๊ฐ’์„ ์ฐพ๋Š” ๋ฐ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์†”๋ฃจ์…˜์—์„œ๋Š” Dictionary ๋‚ด word ์˜ ์œ ๋‹ˆํฌํ•จ์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด List ๋ณด๋‹ค๋Š” Map์„ ์‚ฌ์šฉ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. Map์€ == ์—ฐ์‚ฐ์ž๋กœ ๋น„๊ตํ•  ๊ฒฝ์šฐ ์ฃผ์†Œ๊ฐ’์„ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“  key์™€ value ๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด equals ๋ฉ”์„œ๋“œ๋ฅผ Extension ์œผ๋กœ ๊ตฌํ˜„ํ•˜์—ฌ ์ฒด์ด๋‹ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์„ ํ†ตํ•ด ์‚ฌ์šฉ์„ฑ์„ ๋†’์˜€์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์†”๋ฃจ์…˜์€ replit ์— ํŽธ๋ฆฌํ•˜๊ฒŒ ์ œ์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•˜๋‚˜์˜ ํŒŒ์ผ์—์„œ assert๋ฅผ ํ†ตํ•ด ํ…Œ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์‹คํ–‰์„ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ ์˜ต์…˜์„ ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. dart โ€”enable-asserts main.dart ์‹ค์ œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ๋Š” test ํŒจํ‚ค์ง€ ๋ฅผ ํ†ตํ•ด ์œ ๋‹› ๋ฐ ํ†ตํ•ฉ ํ…Œ์ŠคํŒ…์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํ‹ฑํ†ก ํด๋ก  30๊ฐ• ์—์„œ๋„ ํ…Œ์ŠคํŒ…์— ๋Œ€ํ•ด ์ž์„ธํžˆ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

extension MapExtension<K, V> on Map<K, V> {
  bool equals(Map<K, V> other) {
    if (identical(this, other)) return true;
    if (length != other.length) return false;
    for (final key in keys) {
      if (!other.containsKey(key)) return false;
      if (other[key] != this[key]) return false;
    }
 
    return true;
  }
}
 
typedef Term = String;
typedef Definition = String;
typedef Words = Map<Term, Definition>;
 
class Dictionary {
  Words words;
  Dictionary(this.words);
 
  void add(Term term, Definition definition) {
    words[term] = definition;
  }
 
  Definition? get(Term term) {
    return words[term];
  }
 
  Definition? delete(Term term) {
    return words.remove(term);
  }
 
  Definition? update(Term term, Definition definition) {
    return words.update(term, (value) => definition);
  }
 
  Words showAll() {
    return words;
  }
 
  int count() {
    return words.length;
  }
 
  Definition? upsert(Term term, Definition definition) {
    if (words.containsKey(term)) {
      return this.update(term, definition);
    }
 
    this.add(term, definition);
    return null;
  }
 
  bool exists(Term term) {
    return words.containsKey(term);
  }
 
  void bulkAdd(Words words) {
    this.words.addAll(words);
  }
 
  void bulkDelete(List<Term> terms) {
    terms.forEach((term) => this.words.remove(term));
  }
}
 
main() {
  Dictionary dictionary = Dictionary({
    'Dart': 'A new programming language',
    'Flutter': 'A framework to build cross-platform apps'
  });
 
  dictionary.add('Android', 'A mobile operating system');
  assert(dictionary.count() == 3);
 
  Definition? actual = dictionary.get('Android');
  assert(actual == 'A mobile operating system');
 
  actual = dictionary.delete('Android');
  assert(actual == 'A mobile operating system');
 
  actual = dictionary.update('Dart', '3.0 is awesome');
  assert(actual == '3.0 is awesome');
 
  Words words = dictionary.showAll();
  assert(words.equals({
    'Dart': '3.0 is awesome',
    'Flutter': 'A framework to build cross-platform apps'
  }));
 
  int count = dictionary.count();
  assert(count == 2);
 
  Definition? updated = dictionary.upsert('Dart', 'flirting language');
  assert(updated == 'flirting language');
 
  Definition? inserted = dictionary.upsert('IOS', 'A mobile operating system');
  assert(inserted == null);
  assert(dictionary.count() == 3);
 
  bool exists = dictionary.exists('Dart');
  assert(exists == true);
 
  dictionary.bulkAdd({
    'Rust': 'Super fast programming language',
    'Typescript': 'A typed superset of JavaScript'
  });
  assert(dictionary.count() == 5);
 
  dictionary.bulkDelete(['Rust', 'Typescript']);
  assert(dictionary.count() == 3);
}
 

๐Ÿ“œ์ถœ์ฒ˜(์ฐธ๊ณ  ๋ฌธํ—Œ)


๐Ÿ”—์—ฐ๊ฒฐ ๋ฌธ์„œ

2์ผ์ฐจ 3์ผ์ฐจ 4์ผ์ฐจ