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);
}