Last active
October 31, 2023 21:44
-
-
Save lingoslinger/33cd28f892338d239e2d382eef2ef4f3 to your computer and use it in GitHub Desktop.
Check SOS Letters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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