Skip to content

Instantly share code, notes, and snippets.

@tiusender
Last active March 25, 2020 21: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 tiusender/26bab8e233743d287c8f114a6a88d3a9 to your computer and use it in GitHub Desktop.
Save tiusender/26bab8e233743d287c8f114a6a88d3a9 to your computer and use it in GitHub Desktop.
Clever solutions found on Codewars
/*
DISCLAIMER
==========
I do not own or have created the snippets below.
I'm including them here for my own reference and knowledge improvement.
*/
//Triangular
func triangular(_ n: Int) -> Int{
return stride(from: 1, through: n, by: 1).reduce(0, +)
}
func triangular(_ n: Int) -> Int{
guard n > 0 else {
return 0
}
return n+triangular(n-1)
}
func triangular(_ n: Int) -> Int{
return n > 0 ? (1...n).reduce(0,+) : 0
}
//Disemvowel
func disemvowel(_ s: String) -> String {
return s.replacingOccurrences(of: "[aeiou]", with: "", options: [.regularExpression, .caseInsensitive])
}
func disemvowel(_ s: String) -> String {
let vowels: [Character] = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
return String(s.characters.filter { !vowels.contains($0) })
}
func disemvowel(_ s: String) -> String {
return s.components(separatedBy: CharacterSet(charactersIn: "aeiouAEIOU")).joined()
}
func disemvowel(_ s: String) -> String {
return s.map{ "\($0)" }.filter{ !("ieaou".contains($0.lowercased())) }.joined()
}
func disemvowel(_ s: String) -> String {
return s.filter { !"aeiouAEIOU".contains($0) }
}
//Which are in
func inArray(_ a1: [String], _ a2: [String]) -> [String] {
return Set(a1.filter { s1 in a2.contains { $0.contains(s1) } }).sorted()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment