Skip to content

Instantly share code, notes, and snippets.

@johnpaulmanoza
Created October 18, 2023 12:06
Show Gist options
  • Save johnpaulmanoza/05552328b8039687e658b7fdabfcc403 to your computer and use it in GitHub Desktop.
Save johnpaulmanoza/05552328b8039687e658b7fdabfcc403 to your computer and use it in GitHub Desktop.
Number to Day of the Week
func solution(startAtDay: String, num: Int) -> String? {
let days: [String] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
guard days.contains(startAtDay) else { return nil }
let index = (days.firstIndex(of: startAtDay) ?? 0) + 1
let value = index + num
let normalizedNo = (value - 1) % 7 + 1
switch normalizedNo {
case 1:
return days[0]
case 2:
return days[1]
case 3:
return days[2]
case 4:
return days[3]
case 5:
return days[4]
case 6:
return days[5]
case 7:
return days[6]
default:
return nil
}
}
if let answer = solution(startAtDay: "Sun", num: 23) {
print("Day -> ", answer)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment