This file contains 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
/* | |
Given a tree of nodes that looks like this: | |
A | |
/ \ | |
B G | |
/ \ \ | |
C D H | |
/ \ | |
E F |
This file contains 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
/* | |
Given an array that contains numbers and/or other nested arrays, write an algorithm to come up with a sum of these elements, multiplied by the depth (or how many arrays deep) you are. | |
For example, what would you do with an input array that looks like: | |
[ 2, 3, [ 9, [ 1, 2 ]], 4] | |
*/ | |
import Foundation |
This file contains 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
import Foundation | |
func sum(_ lhs: String,_ rhs: String, carry: Int = 0) -> String { | |
guard let lhsLast = lhs.last else { | |
if rhs.count > 0 { | |
return rhs | |
} else { | |
return carry == 1 ? "1" : "" |
This file contains 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
import Foundation | |
/*Given an array of numbers, reset the array to put all the non-zero numbers in front of all the zeros in the array, then return the count of non-zero numbers. | |
e.g., for an input array of [3,0,2,0,0,1,0,4], you’ll end up with a return value of 4 and an array of [3,2,1,4,0,0,0,0] | |
*/ | |
//idea | |
//get first zero | |
//return func(array.removeZero) + [0] |
This file contains 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
func commonSuperviews(between lhs: UIView, and rhs: UIView) -> [UIView] { | |
func getSuperviews(for view: UIView) -> [UIView] { | |
guard let superview = view.superview else { | |
return [] | |
} | |
return [superview] + getSuperviews(for: superview) |
This file contains 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
import Foundation | |
/* | |
A telephone keypad has letters associated with each number (e.g. 2 = abc, 3 = def). | |
Given a passphrase of "fb1" (e.g. one that you might use to log into a bank account), | |
come up with an algorithm that would assemble an array that contains all the different | |
possible letter combinations that, when typed into a telephone dial pad, would be equivalent | |
to the original passphrase. That is, "fb1" equals "321" numerically; matching equivalent | |
combinations include: "da1", "db1", "dc1", "ea1", "eb1", "ec1", "fa1" and "fc1". | |
*/ |
This file contains 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
import Foundation | |
func permutations(_ set: Set<String>) -> Set<String> { | |
guard set.count != 0 else { | |
return Set<String>([""]) | |
} | |
return set.reduce(Set<String>()) { accumulator, element in |
This file contains 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
enum LinkedList { | |
case empty | |
indirect case node(T, LinkedList) | |
func swapTwoFirst() -> LinkedList { | |
switch self { | |
case .node(let value, .node(let nextValue, let next)): | |
return .node(nextValue, .node(value, next.swapTwoFirst())) |
This file contains 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
import Foundation | |
//InsertionSort | |
///Inserts a value into a sorted array | |
func insert<T: Comparable>(value: T,into array: [T]) -> [T] { | |
guard let last = array.last else { | |
return [value] |
This file contains 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
import Foundation | |
//Merge Sort | |
func merge<T: Comparable>(_ lhs: [T],_ rhs: [T]) -> [T] { | |
switch (lhs,rhs) { | |
case (let lhs, let rhs) where lhs.count == 0: | |
return rhs | |
case (let lhs, let rhs) where rhs.count == 0: |
NewerOlder