From 7a472b0ca0261de300b81ce714658c45d2d665dc Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Mon, 6 May 2024 08:07:19 +0100 Subject: [PATCH 1/3] Swift Implementation for LCCI 16.02 --- lcci/16.02.Words Frequency/README.md | 22 ++++++++++++++++++++++ lcci/16.02.Words Frequency/README_EN.md | 22 ++++++++++++++++++++++ lcci/16.02.Words Frequency/Solution.swift | 13 +++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 lcci/16.02.Words Frequency/Solution.swift diff --git a/lcci/16.02.Words Frequency/README.md b/lcci/16.02.Words Frequency/README.md index b27886435d3b9..69daeb39039bf 100644 --- a/lcci/16.02.Words Frequency/README.md +++ b/lcci/16.02.Words Frequency/README.md @@ -203,6 +203,28 @@ WordsFrequency.prototype.get = function (word) { */ ``` +```swift +class WordsFrequency { + private var cnt: [String: Int] = [:] + + init(_ book: [String]) { + for word in book { + cnt[word, default: 0] += 1 + } + } + + func get(_ word: String) -> Int { + return cnt[word, default: 0] + } +} + +/** + * Your WordsFrequency object will be instantiated and called as such: + * let obj = WordsFrequency(book) + * let param_1 = obj.get(word) + */ +``` + diff --git a/lcci/16.02.Words Frequency/README_EN.md b/lcci/16.02.Words Frequency/README_EN.md index efae82c6c72ef..a20e35e540cee 100644 --- a/lcci/16.02.Words Frequency/README_EN.md +++ b/lcci/16.02.Words Frequency/README_EN.md @@ -215,6 +215,28 @@ WordsFrequency.prototype.get = function (word) { */ ``` +```swift +class WordsFrequency { + private var cnt: [String: Int] = [:] + + init(_ book: [String]) { + for word in book { + cnt[word, default: 0] += 1 + } + } + + func get(_ word: String) -> Int { + return cnt[word, default: 0] + } +} + +/** + * Your WordsFrequency object will be instantiated and called as such: + * let obj = WordsFrequency(book) + * let param_1 = obj.get(word) + */ +``` + diff --git a/lcci/16.02.Words Frequency/Solution.swift b/lcci/16.02.Words Frequency/Solution.swift new file mode 100644 index 0000000000000..786c533f40002 --- /dev/null +++ b/lcci/16.02.Words Frequency/Solution.swift @@ -0,0 +1,13 @@ +class WordsFrequency { + private var cnt: [String: Int] = [:] + + init(_ book: [String]) { + for word in book { + cnt[word, default: 0] += 1 + } + } + + func get(_ word: String) -> Int { + return cnt[word, default: 0] + } +} From 14c94e6724fd1f52e253cf9bcb16f8b3997881dc Mon Sep 17 00:00:00 2001 From: thinkasany <480968828@qq.com> Date: Mon, 6 May 2024 16:06:22 +0800 Subject: [PATCH 2/3] Update lcci/16.02.Words Frequency/README_EN.md --- lcci/16.02.Words Frequency/README_EN.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lcci/16.02.Words Frequency/README_EN.md b/lcci/16.02.Words Frequency/README_EN.md index a20e35e540cee..13b14374647af 100644 --- a/lcci/16.02.Words Frequency/README_EN.md +++ b/lcci/16.02.Words Frequency/README_EN.md @@ -229,12 +229,6 @@ class WordsFrequency { return cnt[word, default: 0] } } - -/** - * Your WordsFrequency object will be instantiated and called as such: - * let obj = WordsFrequency(book) - * let param_1 = obj.get(word) - */ ``` From 0eaaf1abb416b8658769b876051b19332660f646 Mon Sep 17 00:00:00 2001 From: thinkasany <480968828@qq.com> Date: Mon, 6 May 2024 16:06:27 +0800 Subject: [PATCH 3/3] Update lcci/16.02.Words Frequency/README.md --- lcci/16.02.Words Frequency/README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lcci/16.02.Words Frequency/README.md b/lcci/16.02.Words Frequency/README.md index 69daeb39039bf..9c65dc344a14c 100644 --- a/lcci/16.02.Words Frequency/README.md +++ b/lcci/16.02.Words Frequency/README.md @@ -217,12 +217,6 @@ class WordsFrequency { return cnt[word, default: 0] } } - -/** - * Your WordsFrequency object will be instantiated and called as such: - * let obj = WordsFrequency(book) - * let param_1 = obj.get(word) - */ ```