Conversation
Summary of ChangesHello @snughnu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 이 PR은 프로그래머스 '뉴스 클러스터링' 문제를 해결하기 위한 Swift 코드를 추가합니다. 두 입력 문자열로부터 영문자로만 구성된 두 글자 쌍의 다중집합을 생성하고, 이들을 기반으로 자카드 유사도를 계산한 후, 문제에서 요구하는 특정 값으로 스케일링된 정수 결과를 반환합니다. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This PR adds Swift code to solve the news clustering problem. A potential Denial of Service (DoS) vulnerability was identified in the makeSet function, where an empty input string leads to a runtime crash due to an invalid range construction. Beyond this, while the Jaccard similarity calculation logic is correctly implemented, there's room for performance improvement in calculating multiset intersection and union. The current approach using array's firstIndex(of:) and remove(at:) can be inefficient for large inputs; using a dictionary to manage bigram counts would offer a more efficient O(N+M) time complexity.
| func jaccard(_ s1: String, _ s2: String) -> Double { | ||
| let set1 = makeSet(s1) | ||
| var set2 = makeSet(s2) | ||
|
|
||
| if set1.isEmpty && set2.isEmpty { return 1.0 } | ||
|
|
||
| var intersection: [String] = [] | ||
| var unionCount: Int = 0 | ||
|
|
||
| for element in set1 { | ||
| if let idx = set2.firstIndex(of: element) { | ||
| intersection.append(element) | ||
| set2.remove(at: idx) | ||
| } | ||
| } | ||
|
|
||
| unionCount = set1.count + set2.count | ||
| return Double(intersection.count) / Double(unionCount) | ||
| } |
There was a problem hiding this comment.
jaccard 함수에서 교집합과 합집합을 계산하는 현재 방식은 배열에 대해 firstIndex(of:)와 remove(at:)를 반복적으로 사용합니다. 이 배열 연산은 최악의 경우 O(N*M)의 시간 복잡도를 가질 수 있어, 입력 크기가 커질수록 성능 저하가 발생할 수 있습니다.
makeSet 함수에서 반환하는 딕셔너리(다중집합)를 활용하면, 교집합과 합집합을 O(N+M) 시간 복잡도로 효율적으로 계산할 수 있습니다. 제안된 코드는 다중집합의 합집합 공식인 |A ∪ B| = |A| + |B| - |A ∩ B|를 사용합니다.
func jaccard(_ s1: String, _ s2: String) -> Double {
let multiset1 = makeSet(s1)
let multiset2 = makeSet(s2)
if multiset1.isEmpty && multiset2.isEmpty { return 1.0 }
var intersectionCount = 0
// 교집합 계산
for (bigram, count1) in multiset1 {
if let count2 = multiset2[bigram] {
intersectionCount += min(count1, count2)
}
}
// 합집합 계산: |A U B| = |A| + |B| - |A intersect B|
let sumOfCounts1 = multiset1.values.reduce(0, +)
let sumOfCounts2 = multiset2.values.reduce(0, +)
let unionCount = sumOfCounts1 + sumOfCounts2 - intersectionCount
// unionCount가 0인 경우 (두 다중집합 모두 비어있거나, 교집합이 전체를 상쇄하는 경우 등)
if unionCount == 0 { return 1.0 }
return Double(intersectionCount) / Double(unionCount)
}| let chars = Array(string.lowercased()) | ||
| var result: [String] = [] | ||
|
|
||
| for i in 0..<chars.count-1 { |
There was a problem hiding this comment.
The makeSet function has a potential Denial of Service (DoS) vulnerability. If an empty string is passed, the range 0..<chars.count-1 becomes 0..<-1, causing a runtime crash in Swift. A robust implementation should handle empty inputs to prevent this. Additionally, for better performance and clearer multiset representation, consider changing makeSet to return a dictionary [String: Int] to store bigram counts, which would also improve the efficiency of jaccard function calculations.
| for i in 0..<chars.count-1 { | |
| for i in 0..<max(0, chars.count - 1) { |
🔗 문제 링크
✔️ 소요된 시간
48m
📚 새롭게 알게된 내용