Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
func solution(_ str1:String, _ str2:String) -> Int {

var str1 = makeWordList(str1)
var str2 = makeWordList(str2)
var intersection: Int = 0
for key in str1.0.keys {
let num = min(str1.0[key] ?? 0, str2.0[key] ?? 0)
intersection += num
}
let union = str1.1 + str2.1 - intersection

if str1.1 == 0 && str2.1 == 0 {
return 65536
Comment on lines +3 to +13

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

makeWordList 함수에서 반환하는 튜플의 요소에 이름을 부여하면 코드의 가독성을 크게 향상시킬 수 있습니다. 또한, str1str2 매개변수를 섀도잉하는 대신, 튜플을 직접 구조 분해하여 명확한 변수 이름을 사용하는 것이 좋습니다.

Suggested change
var str1 = makeWordList(str1)
var str2 = makeWordList(str2)
var intersection: Int = 0
for key in str1.0.keys {
let num = min(str1.0[key] ?? 0, str2.0[key] ?? 0)
intersection += num
}
let union = str1.1 + str2.1 - intersection
if str1.1 == 0 && str2.1 == 0 {
return 65536
let (str1Map, str1Count) = makeWordList(str1)
let (str2Map, str2Count) = makeWordList(str2)
var intersection: Int = 0
for key in str1Map.keys {
let num = min(str1Map[key] ?? 0, str2Map[key] ?? 0)
intersection += num
}
let union = str1Count + str2Count - intersection
if str1Count == 0 && str2Count == 0 {
return 65536

}
return 65536 * intersection / union
Comment on lines +13 to +15

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

65536이라는 상수가 두 번 사용되고 있습니다. 이러한 '매직 넘버'는 코드의 가독성을 저해하고 향후 변경 시 오류를 유발할 수 있으므로, 상수로 정의하여 사용하는 것이 좋습니다. 예를 들어, let JACCARD_MULTIPLIER = 65536과 같이 정의한 후 사용하는 것을 고려해 보세요.

}

func makeWordList(_ str: String) -> ([String: Int], Int) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

makeWordList 함수가 반환하는 튜플에 이름을 지정하여, 호출하는 쪽에서 .0, .1과 같은 인덱스 대신 의미 있는 이름으로 접근할 수 있도록 개선할 수 있습니다. 이는 코드의 가독성과 유지보수성을 높이는 좋은 방법입니다.

Suggested change
func makeWordList(_ str: String) -> ([String: Int], Int) {
func makeWordList(_ str: String) -> (wordMap: [String: Int], totalCount: Int) {

var word: String = ""
var wordMap: [String: Int] = [:]
var count: Int = 0
for s in str.lowercased() {
if !s.isLetter {
word = ""
continue
}
word += String(s)
if word.count == 2 {
wordMap[word, default: 0] += 1
count += 1
word = String(s)
}
}
return (wordMap, count)
}