Skip to content
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

[Algorithm] [1차] 뉴스 클러스터링 #114

Closed
hwangJi-dev opened this issue Feb 22, 2023 · 0 comments
Closed

[Algorithm] [1차] 뉴스 클러스터링 #114

hwangJi-dev opened this issue Feb 22, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Feb 22, 2023

💬 문제

[1차] 뉴스 클러스터링


💬 Idea

  1. 2개를 기준으로 문자열을 슬라이싱하며 정규표현식과 일치하지 않는 문자열은 건너띄고, 일치하는 문자열을 저장하여 반환한다.
  2. set으로 key값을 생성하여 key값을 돌면서 교집합과 합집합의 개수를 구한다.

💬 풀이

func solution(str1:String, str2:String) -> Int {
    let str1MultipleArr: [String] = sliceArr(Array(str1).map{ String($0) }, "^[a-z]+$")
    let str2MultipleArr: [String] = sliceArr(Array(str2).map{ String($0) }, "^[a-z]+$")
    let key = Set(str1MultipleArr + str2MultipleArr)
    
    var intersection: [String] = []
    var union: [String] = []
    
    for k in key {
        let s1 = str1MultipleArr.filter{ $0 == k }.count
        let s2 = str2MultipleArr.filter{ $0 == k }.count
        
        // 교
        if s1 != 0 && s2 != 0 {
            for _ in 0..<min(s1, s2) {
                intersection.append(k)
            }
        }
        
        // 합
        for _ in 0..<max(s1, s2) {
            union.append(k)
        }
    }
    
    if intersection.count == 0 && union.count == 0 {
        return 65536
    } else {
        return Int((Double(intersection.count) / Double(union.count) * 65536))
    }
}

func sliceArr(_ str: [String], _ regex: String) -> [String] {
    var arr: [String] = []
    for i in 0..<str.count - 1 {
        let s = str[i...i + 1].map{ String($0) }.joined().lowercased()
        
        if !s.allSatisfy({ $0.isLetter }) {
            continue
        }
        if s.range(of: regex, options: .regularExpression) == nil {
            continue
        }

        arr.append(s)
    }
    
    return arr
}

💬 더 나은 방법?

s.allSatisfy({ $0.isLetter }) 
  • 정규표현식을 사용하지 않고도 풀 수 있는 방법!
    • 분명 아는 문법이었는데 왜 풀때는 생각나지 않았는지.. ㅠ 반성해야겠다 !

💬 알게된 문법

func getArrayAfterRegex(regex: String) -> [String] {
        
        do {
            let regex = try NSRegularExpression(pattern: regex)
            let results = regex.matches(in: self,
                                        range: NSRange(self.startIndex..., in: self))
            return results.map {
                String(self[Range($0.range, in: self)!])
            }
        } catch let error {
            print("invalid regex: \(error.localizedDescription)")
            return []
        }
    }

[[Swift] Swift에서 정규표현식(Regular Expression)을 이용하기](https://eunjin3786.tistory.com/12)

  • 소수점 이하 나누는 방법 잊지 말 것…
    • Double(num1) / Double(num2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant