Skip to content

Instantly share code, notes, and snippets.

@proxpero
Last active October 5, 2015 12:44
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 proxpero/80ce553fcb26b60f1703 to your computer and use it in GitHub Desktop.
Save proxpero/80ce553fcb26b60f1703 to your computer and use it in GitHub Desktop.
Solution to problem 2 from "5 Programming Problems, 1 Hour"
//: 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"]
let second = ["1", "2", "3"]
let result = ["a", "1", "b", "2", "c", "3"]
func combine<T>(first: [T], second: [T]) -> [T] {
var result = [T]()
for i in (0..<first.count) {
result.append(first[i])
result.append(second[i])
}
return result
}
assert(result == combine(first, second: second))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment