Skip to content

Instantly share code, notes, and snippets.

@chriswebb09
Created December 20, 2016 22:03
Show Gist options
  • Save chriswebb09/3b71b1a86e7b65d8485e5b0468ed744c to your computer and use it in GitHub Desktop.
Save chriswebb09/3b71b1a86e7b65d8485e5b0468ed744c to your computer and use it in GitHub Desktop.
func permutations(input:String, builder: String = "") {
var array = input.characters.map { $0 }
var length = input.characters.count
if length == 0 { print(builder) }
for i in 0 ..< length {
var lhs = String(array[0 ..< i])
var rhs = String(array[i+1 ..< length])
permutations(input: "\(lhs)\(rhs)", builder: builder + String(array[i]))
}
}
func pyramid(range : Int) {
let p = 1...range
var strings = ""
var ntype = range
for p in 1...range {
strings = "#\(strings)"
var spaces = ""
for n in 1...ntype {
if n == 1 { spaces = "\(spaces)" } else { spaces = " \(spaces)" }
}
ntype -= 1
if p == range - 1 { print("\(spaces)\(strings)") } else { print("\(spaces)\(strings)") }
}
}
pyramid(range: n)
func reverseString(string:String) {
var stingArray = string.components(separatedBy: " ")
var newString = ""
stingArray.forEach {
newString = "\($0) \(newString)"
}
print(newString)
}
func isPrime(num:Int) -> Bool {
guard num % 2 != 0 else { return false }
for i in 2 ..< num {
if num % i == 0 { return false }
}
return true
}
func factorial(N:Int) -> Int {
var intVal = N
if intVal == 0 {
return 1
} else {
return intVal * factorial(N: intVal - 1)
}
}
print(factorial(N: 6))
isPrime(num: 27)
reverseString(string: "Hello dear")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment