Skip to content

Instantly share code, notes, and snippets.

@johnpaulmanoza
Created October 18, 2023 12:06
Show Gist options
  • Select an option

  • Save johnpaulmanoza/05552328b8039687e658b7fdabfcc403 to your computer and use it in GitHub Desktop.

Select an option

Save johnpaulmanoza/05552328b8039687e658b7fdabfcc403 to your computer and use it in GitHub Desktop.

Revisions

  1. johnpaulmanoza created this gist Oct 18, 2023.
    31 changes: 31 additions & 0 deletions day_range.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    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)
    }