-
Notifications
You must be signed in to change notification settings - Fork 0
방금그곡 #32
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?
Conversation
Summary of ChangesHello @sangYuLv, 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
전반적으로 문제의 요구사항을 잘 이해하고 구현하셨습니다. 다만, 몇 가지 개선할 점이 보입니다.
첫째, toNumber 함수에서 시간 형식 "HH:MM"을 "분:초"로 잘못 해석하고 있어 수정이 필요합니다. 이는 치명적인 버그로, 올바른 결과 도출을 위해 반드시 수정되어야 합니다.
둘째, convertSheet 함수와 solution 함수 내에서 전체 연주된 멜로디를 생성하는 부분의 성능을 개선할 수 있는 여지가 있습니다. 문자열을 반복적으로 생성하고 자르는 대신 더 효율적인 방법을 사용하면 성능을 향상시킬 수 있습니다.
자세한 내용은 각 파일의 코드 리뷰 댓글을 참고해주세요.
| func toNumber(_ str: String) -> Int { | ||
| let sections = str.split(separator: ":").map { String($0) } | ||
| guard sections.count == 2, | ||
| let min = Int(sections[0]), | ||
| let sec = Int(sections[1]) | ||
| else { return 0 } | ||
| return min * 60 + sec | ||
| } |
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.
toNumber 함수가 "HH:MM" 형식의 시간을 "분:초"로 잘못 파싱하고 있습니다. 문제의 요구사항에 따르면 시간은 "시:분" 형식이므로, Int(sections[0])은 시(hour), Int(sections[1])은 분(minute)이 되어야 합니다. 현재 구현은 min * 60 + sec으로 총 초를 계산하는 것처럼 보이지만, 실제로는 시 * 60 + 분으로 총 분을 계산해야 합니다. 또한, 변수명 min, sec이 혼동을 줄 수 있으니 hour, minute 등으로 변경하는 것이 좋겠습니다. 이 버그는 음악의 재생 시간을 잘못 계산하게 만들어 오답을 유발할 수 있습니다.
| func toNumber(_ str: String) -> Int { | |
| let sections = str.split(separator: ":").map { String($0) } | |
| guard sections.count == 2, | |
| let min = Int(sections[0]), | |
| let sec = Int(sections[1]) | |
| else { return 0 } | |
| return min * 60 + sec | |
| } | |
| func toNumber(_ str: String) -> Int { | |
| let sections = str.split(separator: ":").map { String($0) } | |
| guard sections.count == 2, | |
| let hour = Int(sections[0]), | |
| let minute = Int(sections[1]) | |
| else { return 0 } | |
| return hour * 60 + minute | |
| } |
| let repeatCount = duration / sheet.count + 1 | ||
| let played = String((String(repeating: sheet, count: repeatCount)).prefix(duration)) |
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.
재생 시간(duration) 만큼의 전체 멜로디를 생성하는 현재 방식은 매우 비효율적일 수 있습니다. String(repeating:count:)으로 악보를 duration / sheet.count + 1 만큼 반복하여 매우 긴 중간 문자열을 생성한 후, prefix(duration)으로 잘라내고 있습니다. 예를 들어, 재생 시간이 1400분이고 악보 길이가 2라면, 약 1400 * 701 = 981400 길이의 문자열을 생성하게 됩니다. 이는 불필요한 메모리 할당과 연산을 유발합니다. 필요한 길이만큼만 멜로디를 생성하는 것이 훨씬 효율적입니다.
| let repeatCount = duration / sheet.count + 1 | |
| let played = String((String(repeating: sheet, count: repeatCount)).prefix(duration)) | |
| let sheetChars = Array(sheet) | |
| let played = String((0..<duration).map { sheetChars[$0 % sheetChars.count] }) |
| func convertSheet(_ str: String) -> String { | ||
| var sheet: String = str | ||
| while let point = sheet.firstIndex(of: "#") { | ||
| let target = sheet.index(before: point) | ||
| let note = sheet[target].lowercased() | ||
| sheet.removeSubrange(target...point) | ||
| sheet.insert(contentsOf: note, at: target) | ||
| } | ||
| return sheet | ||
| } |
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.
convertSheet 함수에서 while 루프를 사용하여 '#'이 포함된 음을 변환하는 방식은 문자열이 길고 '#'이 많을 경우 비효율적일 수 있습니다. 루프마다 firstIndex(of:)로 문자열을 탐색하고 removeSubrange 및 insert로 문자열을 수정하는 과정은 많은 비용을 발생시킬 수 있습니다. 문제에서 변환해야 할 음표('#'이 붙는 음표)는 C#, D#, F#, G#, A#로 정해져 있으므로, replacingOccurrences(of:with:) 메소드를 연달아 사용하면 코드가 더 간결해지고 성능도 향상될 수 있습니다.
func convertSheet(_ str: String) -> String {
return str
.replacingOccurrences(of: "C#", with: "c")
.replacingOccurrences(of: "D#", with: "d")
.replacingOccurrences(of: "F#", with: "f")
.replacingOccurrences(of: "G#", with: "g")
.replacingOccurrences(of: "A#", with: "a")
}
🔗 문제 링크
✔️ 소요된 시간
40분 + 20분