Skip to content

Instantly share code, notes, and snippets.

@Ceri-anne
Last active September 13, 2023 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ceri-anne/4c9e7841250c528361b7510b9d8f5ecd to your computer and use it in GitHub Desktop.
Save Ceri-anne/4c9e7841250c528361b7510b9d8f5ecd to your computer and use it in GitHub Desktop.
import Foundation
// Update these fields
let nextRetroDate = "01/01/2021"
let team = [
"Name 1",
"Name 2",
"Name 3"
]
struct RetroRota {
let nextRetroDate: String
let team: [String]
let sprintLengthInWeeks: Int = 2
func generate() {
generateRota(for: team, from: nextRetroDate)
}
// Function to convert date from string on format dd/mm/YYYY to Date
private func getDate(from string: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
let date = dateFormatter.date(from: string)
assert(date != nil, "Cannot convert date, needs to be input as dd/mm/YYYY format")
return date!
}
// Function to produce an array of specific size with dates at fortnightly intervals
private func getFortnights(from start: Date, count: Int) -> [Date] {
var result = [start]
for i in 0..<(count-1) {
let current = result[i]
let next = Calendar.current.date(byAdding: .weekOfYear, value: sprintLengthInWeeks, to: current)
assert(next != nil, "Cannot create next date")
result.append(next!)
}
return result
}
// Function to convert a date into a string with format dd/mm/YYYY
private func getString(from date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
return dateFormatter.string(from: date)
}
// Function to generate random rota with dates and output as string
@discardableResult
private func generateRota(for team: [String], from dateString: String) -> String {
// Randomly shuffle team array
let retroRota = team.shuffled()
// Create correct number of dates
let date = getDate(from: dateString)
let dates = getFortnights(from: date, count: retroRota.count).compactMap { getString(from: $0) }
assert(retroRota.count == dates.count, "Expected dates and retroRota arrays to be same size")
// Generate string to output
var string = "*Date // Chair *\n"
for (index, date) in dates.enumerated() {
string += "\(date) // \(retroRota[index])\n"
}
print(string)
return string
}
}
// Generate rota and print
RetroRota(nextRetroDate: nextRetroDate, team: team).generate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment