Last active
October 5, 2015 12:44
-
-
Save proxpero/80ce553fcb26b60f1703 to your computer and use it in GitHub Desktop.
Solution to problem 2 from "5 Programming Problems, 1 Hour"
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"] | |
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