-
Notifications
You must be signed in to change notification settings - Fork 0
[3차] 방금그곡 #30
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차] 방금그곡 #30
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 { | ||
|
|
||
| // 분 변환 | ||
| func toMinute(_ time: String) -> Int { | ||
| let t = time.split(separator: ":").map { Int($0)! } | ||
| return t[0] * 60 + t[1] | ||
| } | ||
|
|
||
| // 반음처리 | ||
| func halfStep(_ s: String) -> String { | ||
| return s | ||
| .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") | ||
| } | ||
|
Comment on lines
+10
to
+19
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.
func halfStep(_ s: String) -> String {
let replacements = [
"C#": "c", "D#": "d", "F#": "f", "G#": "g", "A#": "a", "B#": "C", "E#": "F"
]
return replacements.reduce(s) { $0.replacingOccurrences(of: $1.key, with: $1.value) }
} |
||
|
|
||
| let goal = halfStep(m) | ||
| var result = "(None)" | ||
| var maxTime = -1 | ||
|
|
||
| for info in musicinfos { | ||
| let parts = info.split(separator: ",").map { String($0) } | ||
| let start = toMinute(parts[0]) | ||
| let end = toMinute(parts[1]) | ||
| let playTime = end - start | ||
| let title = parts[2] | ||
| let melody = Array(halfStep(parts[3])) | ||
|
|
||
| if melody.isEmpty { continue } | ||
|
|
||
| var played = "" | ||
| for i in 0..<playTime { | ||
| played.append(melody[i % melody.count]) | ||
| } | ||
|
Comment on lines
+35
to
+38
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.
let melodyString = String(melody)
let repeatedCount = playTime / melody.count
let remainingCount = playTime % melody.count
let played = String(repeating: melodyString, count: repeatedCount) + melodyString.prefix(remainingCount) |
||
|
|
||
| if played.contains(goal) { | ||
| if playTime > maxTime { | ||
| maxTime = playTime | ||
| result = title | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
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.
toMinute함수에서Int($0)!를 사용하여 강제 언래핑을 하고 있습니다. 이는 입력값이 항상 "HH:MM" 형식의 유효한 정수임을 가정하지만, 만약 그렇지 않은 경우 런타임 오류를 발생시켜 앱을 중단시킬 수 있습니다.compactMap을 사용하여 옵셔널을 안전하게 해제하고,guard문으로 배열의 크기를 확인하여 코드를 더 견고하게 만드는 것이 좋습니다.