extension String { func insert(_ value: String, interval: Int) -> String { guard interval > 0 else { return self } var new = Array(self).map(String.init) (1..<count).filter { $0 % interval == 0 } .reversed() .forEach { new.insert(value, at: $0) } return new.joined() } }
- extension String {
- func insert(_ value: String, interval: Int) -> String {
guard interval > 0 && interval < count else { return self }let chars = Array(self)var result = ""var i = 0while i+interval<count {result.append(String(chars[i..<min(count,i+interval)])+value)i += interval}result.append(String(chars[i..<min(count,i+interval)]))return result- guard interval > 0 else { return self }
- var new = Array(self).map(String.init)
- (1..<count).filter { $0 % interval == 0 }
- .reversed()
- .forEach { new.insert(value, at: $0) }
- return new.joined()
- }
- }
func gradeCalc(_ score: Int) -> String { (0...100).contains(score) ? ["F","F","F","F","F","F","D","C","B","A","A"][score / 10] : "Not a grade" }
- func gradeCalc(_ score: Int) -> String {
(score < 0 || score > 100) ? "Not a grade" : ["F","F","F","F","F","F","D","C","B","A","A"][score / 10];- (0...100).contains(score) ? ["F","F","F","F","F","F","D","C","B","A","A"][score / 10] : "Not a grade"
- }