-
Notifications
You must be signed in to change notification settings - Fork 0
뉴스 클러스터링 #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
뉴스 클러스터링 #36
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| func solution(_ str1:String, _ str2:String) -> Int { | ||
| var result = 65536 | ||
| let (lhs, rhs) = (parse(str1), parse(str2)) | ||
|
|
||
| let intersection = getIntersection(lhs, rhs) | ||
| let union = lhs.values.reduce(0, +) + rhs.values.reduce(0, +) - intersection | ||
|
|
||
| guard union != 0 else { return 65536 } | ||
|
|
||
| result = Int(Double(intersection) / Double(union) * 65536) | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| func getIntersection(_ lhs: [String: Int], _ rhs: [String: Int]) -> Int { | ||
| var result = 0 | ||
|
|
||
| for word in lhs.keys { | ||
| result += min(lhs[word, default: 0], rhs[word, default: 0]) | ||
| } | ||
|
|
||
| return result | ||
|
Comment on lines
+16
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| func parse(_ str: String) -> [String: Int] { | ||
| var result = [String: Int]() | ||
| let arr = Array(str) | ||
|
|
||
| for i in 0..<str.count - 1 { | ||
| guard arr[i].isLetter && arr[i + 1].isLetter else { continue } | ||
|
|
||
| let word = String(arr[i]) + String(arr[i + 1]) | ||
| result[word.lowercased(), default: 0] += 1 | ||
| } | ||
|
|
||
| return result | ||
|
Comment on lines
+26
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
var result = [String: Int]()
let arr = Array(str.lowercased())
for i in 0..<(arr.count - 1) {
guard arr[i].isLetter && arr[i + 1].isLetter else { continue }
let word = String(arr[i...i+1])
result[word, default: 0] += 1
}
return result |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
result변수에 값을 할당한 후 바로 반환하는 대신, 계산된 값을 직접 반환하여 코드를 간결하게 만들 수 있습니다. 이 변경으로 인해 2행의var result = 65536선언이 불필요해지므로 함께 제거하는 것이 좋습니다. Swift에서는 불필요한 가변 상태를 줄이는 것이 권장됩니다.