This file contains hidden or 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
//: SICP Exercise 1.11 | |
//: Xcode 7.0, Swift 2.0 | |
//: A function f is defined by the rule that f(n)=n if n<3 and f(n)=f(n−1)+2f(n−2)+3f(n−3) if n≥3. Write a procedure that computes f by means of a recursive process. Write a procedure that computes f by means of an iterative process. | |
func recursive(n: Int) -> Int { | |
if n < 3 { return n } | |
else { return recursive(n-1) + 2 * recursive(n-2) + 3 * recursive(n-3) } | |
} | |
for n in (0...10) { |
This file contains hidden or 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
//: [Merging Overlapping Intervals](https://www.shiftedup.com/2015/05/17/programming-challenge-merging-overlapping-intervals) | |
//: Xcode 7.0, Swift 2.0 | |
/*: Given a collection of intervals, write a function that merges all overlapping intervals and prints them out. | |
For example, given [1, 3], [2, 6], [8, 10], and [7, 11], the function should print [1, 6], [7, 11]. Or given [5, 12], and [8, 10] the function should print [5, 12]. | |
You can assume that the first element of each interval is always less or equal than the second element of the interval. | |
*/ | |
//: Note: The challenge suggests that each interval is an `Array<Int>` where `interval.count == 2` and `interval[0] <= interval[1]`. In Swift, these intervals should be modeled by the `Range<Int>` type. I am altering the challenge to suit the language, I know. [TKO] |
This file contains hidden or 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
//: Rotate a matrix 90 degrees in place | |
// "Write a function to rotate an NxN matrix by 90 degrees. You should rotate it in place, meaning you can't use another matrix to perform the rotation, but instead you have to use the same given structure." | |
// https://www.shiftedup.com/2015/05/10/programming-challenge-rotating-a-matrix-90-degrees-in-place | |
// This implementation of a matrix is taken from Apple's own. | |
// https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Subscripts.html | |
// The extensions are my own. | |
// Xcode 7.0, Swift 2.0 |
This file contains hidden or 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
//: Problem 4 | |
// "Write a function that given a list of non-negative integers, arranges them such that they form the largest possible number. For example, given [50, 2, 1, 9], the largest formed number is 95021." | |
// https://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
// This solution first finds the permutations of the integers in the given array. A string is made out of the digits of each permutation, the string is converted to an integer, and the maximum of all these integers is returned. | |
// It is expensive! but it's short and guaranteed to work every time. | |
// I updated objc.io's permutation snippet to Swift 2.0 and used it in my solution | |
// https://www.objc.io/blog/2014/12/08/functional-snippet-10-permutations/ |
This file contains hidden or 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
//: Problem 3 | |
// "Write a function that computes the list of the first 100 Fibonacci numbers. By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two. As an example, here are the first 10 Fibonnaci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34." | |
// https://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
// Xcode 7.0, Swift 2.0 | |
func fibonacci(count: Int) -> [Double] { | |
var n1: Double = 0 | |
var n2: Double = 1 |
This file contains hidden or 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
//: Problem 2 | |
// "Write a function that combines two lists by alternatingly taking elements. For example: given the two lists [a, b, c] and [1, 2, 3], the function should return [a, 1, b, 2, c, 3]." | |
// https://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
// Xcode 7.0, Swift 2.0 | |
// Assumption: homogeneous arrays, same length | |
let first = ["a", "b", "c"] |
This file contains hidden or 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
//: Problem 1 | |
// "Write three functions that compute the sum of the numbers in a given list using a for-loop, a while-loop, and recursion." | |
// https://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
// Xcode 7.0, Swift 2.0 | |
let series = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] | |
let realSum = series.reduce(0, combine: +) | |
This file contains hidden or 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
//: Permutations | |
// based on https://www.objc.io/blog/2014/12/08/functional-snippet-10-permutations/ | |
// but updated for Swift 2.0 (Xcode 7.0) | |
extension Array { | |
func decompose() -> (Generator.Element, [Generator.Element])? { | |
guard let x = first else { return nil } | |
return (x, Array(self[1..<count])) | |
} | |
} |
NewerOlder