Skip to content

Instantly share code, notes, and snippets.

@lingoslinger
Last active October 31, 2023 21:44
Show Gist options
  • Save lingoslinger/33cd28f892338d239e2d382eef2ef4f3 to your computer and use it in GitHub Desktop.
Save lingoslinger/33cd28f892338d239e2d382eef2ef4f3 to your computer and use it in GitHub Desktop.
Check SOS Letters
// I publicly share any answers to "coding challenges" I receive as a part of the interview process
// copy/paste this into an Xcode playground or the code challenge repl of choice
// in a string of what should be repeating instances of "SOS", detect any errors in the SOS sequence and report the total
// number of errors
func checkSOSLettere(_ message: String) -> Int {
var totalErrors = 0
var currentLetterNumber = 1
for char in message {
let even = currentLetterNumber % 2 == 0
if even {
if String(char) != "O" { totalErrors += 1 }
} else {
if String(char) != "S" { totalErrors += 1 }
}
currentLetterNumber += 1
if currentLetterNumber > 3 { currentLetterNumber = 1 }
}
return totalErrors
}
checkSOSLettere("SOSSSSOOO")
// 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment