Skip to content

Conversation

@sangYuLv
Copy link
Collaborator

@sangYuLv sangYuLv commented Feb 3, 2026

🔗 문제 링크

✔️ 소요된 시간

40분 + 20분

@gemini-code-assist
Copy link

Summary of Changes

Hello @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

  • 새로운 Swift 솔루션 추가: 프로그래머스 '방금그곡' 문제에 대한 Swift 언어 솔루션 파일이 새로 추가되었습니다.
  • 음악 정보 처리 로직 구현: 주어진 음악 정보 문자열을 파싱하고, 재생 시간 및 악보를 처리하여 일치하는 멜로디를 찾는 핵심 로직이 구현되었습니다.
  • 멜로디 변환 유틸리티: 샵(#)이 붙은 음표를 소문자 단일 문자로 변환하여 멜로디 매칭을 용이하게 하는 convertSheet 함수가 추가되었습니다.
  • 시간 변환 유틸리티: "HH:MM" 형식의 시간을 초 단위 정수로 변환하는 toNumber 함수가 구현되었습니다.

🧠 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
  • WEEK03/프로그래머스_방금그곡/Liv.swift
    • 프로그래머스 '방금그곡' 문제 해결을 위한 Swift 코드 파일이 새로 추가되었습니다.
    • 주어진 멜로디와 음악 정보 배열을 처리하여 일치하는 음악을 반환하는 solution 함수가 구현되었습니다.
    • 시간 문자열을 초 단위 정수로 변환하는 toNumber 헬퍼 함수가 추가되었습니다.
    • 샵(#) 음표를 소문자 단일 문자로 변환하여 멜로디 매칭을 단순화하는 convertSheet 헬퍼 함수가 추가되었습니다.
Activity
  • sangYuLv 님이 프로그래머스 '방금그곡' 문제에 대한 Swift 솔루션을 추가했습니다.
  • 문제 링크와 난이도, 그리고 문제 해결에 소요된 시간을 PR 본문에 명시했습니다.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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 함수 내에서 전체 연주된 멜로디를 생성하는 부분의 성능을 개선할 수 있는 여지가 있습니다. 문자열을 반복적으로 생성하고 자르는 대신 더 효율적인 방법을 사용하면 성능을 향상시킬 수 있습니다.
자세한 내용은 각 파일의 코드 리뷰 댓글을 참고해주세요.

Comment on lines +26 to +33
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
}

Choose a reason for hiding this comment

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

critical

toNumber 함수가 "HH:MM" 형식의 시간을 "분:초"로 잘못 파싱하고 있습니다. 문제의 요구사항에 따르면 시간은 "시:분" 형식이므로, Int(sections[0])은 시(hour), Int(sections[1])은 분(minute)이 되어야 합니다. 현재 구현은 min * 60 + sec으로 총 초를 계산하는 것처럼 보이지만, 실제로는 시 * 60 + 분으로 총 분을 계산해야 합니다. 또한, 변수명 min, sec이 혼동을 줄 수 있으니 hour, minute 등으로 변경하는 것이 좋겠습니다. 이 버그는 음악의 재생 시간을 잘못 계산하게 만들어 오답을 유발할 수 있습니다.

Suggested change
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
}

Comment on lines +15 to +16
let repeatCount = duration / sheet.count + 1
let played = String((String(repeating: sheet, count: repeatCount)).prefix(duration))

Choose a reason for hiding this comment

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

high

재생 시간(duration) 만큼의 전체 멜로디를 생성하는 현재 방식은 매우 비효율적일 수 있습니다. String(repeating:count:)으로 악보를 duration / sheet.count + 1 만큼 반복하여 매우 긴 중간 문자열을 생성한 후, prefix(duration)으로 잘라내고 있습니다. 예를 들어, 재생 시간이 1400분이고 악보 길이가 2라면, 약 1400 * 701 = 981400 길이의 문자열을 생성하게 됩니다. 이는 불필요한 메모리 할당과 연산을 유발합니다. 필요한 길이만큼만 멜로디를 생성하는 것이 훨씬 효율적입니다.

Suggested change
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] })

Comment on lines +35 to +44
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
}

Choose a reason for hiding this comment

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

medium

convertSheet 함수에서 while 루프를 사용하여 '#'이 포함된 음을 변환하는 방식은 문자열이 길고 '#'이 많을 경우 비효율적일 수 있습니다. 루프마다 firstIndex(of:)로 문자열을 탐색하고 removeSubrangeinsert로 문자열을 수정하는 과정은 많은 비용을 발생시킬 수 있습니다. 문제에서 변환해야 할 음표('#'이 붙는 음표)는 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")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants