-
Notifications
You must be signed in to change notification settings - Fork 0
[3차] 방금그곡 #29
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?
[3차] 방금그곡 #29
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,49 @@ | ||
| func solution(_ m:String, _ musicinfos:[String]) -> String { | ||
| var result = "(None)" | ||
| var resultPlayTime = 0 | ||
| var resultStartTime = 0 | ||
|
|
||
| for info in musicinfos { | ||
| let info = info.components(separatedBy: ",") | ||
| let startTime = toInt(info[0]) | ||
| let endTime = toInt(info[1]) | ||
| let playTime = endTime - startTime | ||
| let musicTitle = info[2] | ||
| let melody = info[3] | ||
| var music = "" | ||
|
|
||
| while music.count < playTime { | ||
| music += toShortString(melody) | ||
| } | ||
|
Comment on lines
+15
to
+17
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. This let shortMelody = toShortString(melody)
if !shortMelody.isEmpty {
while music.count < playTime {
music += shortMelody
}
} |
||
|
|
||
| if music.prefix(playTime).contains(toShortString(m)) { | ||
|
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. There's a potential crash vulnerability here: if if playTime > 0, music.prefix(playTime).contains(toShortString(m)) { |
||
| if playTime > resultPlayTime { | ||
| result = musicTitle | ||
| resultPlayTime = playTime | ||
| resultStartTime = startTime | ||
| } else if playTime == resultPlayTime { | ||
| if startTime < resultStartTime { | ||
| result = musicTitle | ||
| resultPlayTime = playTime | ||
| resultStartTime = startTime | ||
| } | ||
| } | ||
|
Comment on lines
+20
to
+30
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. 문제의 조건에 따르면 재생 시간이 같을 경우, 먼저 입력된 음악 제목을 반환해야 합니다. 현재 코드는 if playTime > resultPlayTime {
result = musicTitle
resultPlayTime = playTime
} |
||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func toInt(_ timeString: String) -> Int { | ||
| let components = timeString.components(separatedBy: ":") | ||
| return (Int(components[0])! * 60) + Int(components[1])! | ||
|
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. This code exhibits insecure data handling. Force unwrapping ( guard components.count >= 2, let hours = Int(components[0]), let minutes = Int(components[1]) else { return 0 }
return (hours * 60) + minutes |
||
| } | ||
|
|
||
| func toShortString(_ string: String) -> String { | ||
| return string.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") | ||
| .replacingOccurrences(of: "B#", with: "C") | ||
| .replacingOccurrences(of: "E#", with: "F") | ||
| } | ||
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.
루프 변수인
info와 동일한 이름으로 새로운 상수info를 선언하고 있습니다 (변수 섀도잉). 이는 코드를 읽고 이해하기 어렵게 만들 수 있습니다. 파싱된 컴포넌트에 대해infoComponents와 같이 다른 이름을 사용하는 것이 가독성을 높이는 데 도움이 됩니다. 이 제안을 적용하면 이어지는 코드에서info를infoComponents로 변경해야 합니다.