Skip to content

Instantly share code, notes, and snippets.

@cruffenach
Created November 6, 2014 21:27
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 cruffenach/d729792ed98ae594bb52 to your computer and use it in GitHub Desktop.
Save cruffenach/d729792ed98ae594bb52 to your computer and use it in GitHub Desktop.
Interview Prep
// Playground - noun: a place where people can play
import Foundation
import UIKit
//struct Square {
// var x, y, width, height : Float
//}
//
//func overlap(start1 : Float, length1 : Float, start2 : Float, length2 : Float) -> Float {
// let maxStart = max(start1, start2);
// let minEnd = min(start1+length1, start2+length2)
// return max(0.0, minEnd-maxStart)
//}
//
//func overlap(s1 : Square, s2 : Square) -> Float {
// let overlap1 = overlap(s1.x, s1.width, s2.x, s2.width)
// let overlap2 = overlap(s1.y, s1.height, s2.y, s2.height)
// return overlap1 * overlap2
//}
//
//let square1 = Square(x: 0, y: 0, width: 5, height: 5)
//let square2 = Square(x: 0, y: 0, width: 5, height: 5)
//
//println(overlap(square1, square2))
//struct Stack <T> {
// var data = [T]()
//
// mutating func push(obj : T) {
// data.append(obj)
// }
//
// mutating func pop() -> T? {
// return data.removeLast()
// }
//
// func isEmpty() -> Bool {
// return self.data.isEmpty
// }
//}
//
//let openersAndClosers = [
// "(" : ")",
// "{" : "}",
// "[" : "]"
//]
//
//func isOpener(c : Character) -> Bool {
// return c == "(" ||
// c == "{" ||
// c == "["
//}
//
//func isCloser(c : Character) -> Bool {
// return c == ")" ||
// c == "}" ||
// c == "]"
//}
//
//func valid(opener : Character, closer : Character) -> Bool {
// if (!(isOpener(opener) && isCloser(closer))) {
// return false
// }
//
// switch (opener) {
// case "(":
// return closer == ")"
// case "{":
// return closer == "}"
// case "[":
// return closer == "]"
// default:
// return false
// }
//}
//
//extension Stack {
// mutating func open(obj : T) -> Bool {
// self.push(obj)
// return true
// }
//
// mutating func close(obj : T) -> Bool {
// if (self.data.count == 0) {
// return false
// }
//
// if (valid(self.data.last as Character, obj as Character)) {
// self.pop()
// return true
// }
//
// return false
// }
//}
//
//func validNest(s : String) -> Bool{
//
// var stack = Stack<Character>()
// for c in s {
// if (isOpener(c)) {
// if (!stack.open(c)) {
// return false
// }
// } else if (isCloser(c)) {
// if (!stack.close(c)) {
// return false
// }
// } else {
// return false
// }
// }
// return true
//}
//
//validNest("[][](({{}}))[][][](({{}}))[][][](({{}}))[][][](({{}}))[][][](({{}}))[]")
//class Node <T> {
// var value : T?
// var parent : Node?
// var left : Node?
// var right : Node?
//
// func printString() -> String {
// var printString = ""
// if let nodeValue = self.value {
// printString += "\(nodeValue)"
// }
//
// if let leftNode = self.left {
// printString += leftNode.printString()
// }
//
// if let rightNode = self.right {
// printString += rightNode.printString()
// }
// return printString
// }
//
// func print() {
// println(self.printString())
// }
//
// func isLeaf() -> Bool {
// return self.left == nil && self.right == nil
// }
//
// func validBST() -> Bool {
// typealias NodeInfo = (Node<T>, Int, Int)
// var stack = Stack<NodeInfo>()
// var tuple : NodeInfo = (self, Int.min, Int.max)
// stack.push(tuple)
// while (!stack.isEmpty()) {
// var nodeInfo = stack.pop()!
// var node = nodeInfo.0
// var min = nodeInfo.1
// var max = nodeInfo.2
// println("Node Info: Node with value \(node.value!), Min: \(min), Max: \(max)")
//
// if let nodeValue : Int = node.value as? Int {
// min
// max
// nodeValue
//
// if (nodeValue < min || nodeValue > max) {
// return false
// }
//
// if let leftNode = node.left {
// stack.push((node.left!, min, nodeValue))
// }
//
// if let rightNode = node.right {
// stack.push((node.right!, nodeValue, max))
// }
// } else {
// return false
// }
// }
//
// return true
// }
//
// func balancedBST() -> Bool {
// typealias NodeInfo = (Node, Int)
// var stack = Stack<NodeInfo>()
// stack.push((self, 0))
//
// var depths = Dictionary<Int, Int>()
//
// while(!stack.isEmpty()) {
// if let info = stack.pop() {
// var node = info.0
// var depth = info.1
// if (node.isLeaf()) {
//
// if let value = depths[depth] {
// depths[depth] = value+1
// } else {
// depths[depth] = 1
// }
//
// var count = countElements(depths)
// if (count == 1) {
// //Continue
// } else if (count == 2) {
// for (key, value) in depths {
// key
// value
// }
// } else if (count > 2) {
// return false
// }
// } else {
// if let leftNode = node.left {
// stack.push((leftNode, depth+1))
// }
// if let rightNode = node.right {
// stack.push((rightNode, depth+1))
// }
// }
// }
// }
// return true
// }
//}
//var leftLeaf1 = Node<Int>()
//leftLeaf1.value = 10
//var leftLeaf2 = Node<Int>()
//leftLeaf2.value = 8
//var leftLeaf3 = Node<Int>()
//leftLeaf3.value = 11
//leftLeaf1.left = leftLeaf2
//leftLeaf1.right = leftLeaf3
//var rightLeaf1 = Node<Int>()
//rightLeaf1.value = 14
//var rightLeaf2 = Node<Int>()
//rightLeaf2.value = 13
//var rightLeaf3 = Node<Int>()
//rightLeaf3.value = 15
//rightLeaf1.left = rightLeaf2
//rightLeaf1.right = rightLeaf3
//var root = Node<Int>()
//root.value = 12
//root.left = leftLeaf1
//root.right = rightLeaf1
//root.validBST()
//root.balancedBST()
//
//var numbers = [1, 1, 1, 4, 5, 8, 3, 4, 8, 0, 2, 4, 8, 5, 0, 7]
//numbers.reduce([Int](),
// combine: { array, value in
// var result = array
// if(find(result, value) == NSNotFound) {
// result.append(value)
// }
// return result
//})
//
//numbers
//Reversing String
//var string = "Hello"
//
//func reverse(s : String) -> String {
// var result = ""
// for c in s {
// result = "\(c)\(result)"
// }
// return result
//}
//
//reverse(string)
//var a = [9, 8, 7, 6, 5, 0, 1, 2, 3, 4, 3, 5, 4, 7, 100]
//
//func median(a : [Int]) -> Int {
// if (a.isEmpty) {
// return 0
// }
//
// var lastValue = a[0]
// var lastIndex = 0
// var result = [lastValue]
// for i in 1...countElements(a)-1 {
// result
// var current = a[i]
// current
// if lastValue <= current {
// print(1)
// result.insert(current, atIndex: lastIndex+1)
// lastIndex++
// } else {
// print(2)
// result.insert(current, atIndex: lastIndex)
// }
// lastValue = current
// }
// return result[Int(floor(Double(countElements(result))/2.0))]
//}
//
//median(a)
//class LinkedListNode <T : Printable> : Printable {
// var next : LinkedListNode?
// var data : T
//
// init(data : T) {
// self.data = data
// }
//
// var description : String {
// get {
// var result = ""
// var current : LinkedListNode? = self
// while (current != nil) {
// if let _current = current {
// result += _current.data.description
// current = _current.next
// if (current != nil) {
// result += "-->"
// }
// }
// }
// return result
// }
// }
//
// func reverse() -> LinkedListNode {
// var last : LinkedListNode = self
// var current : LinkedListNode? = last.next
// last.next = nil
// while (current != nil) {
// if let _current = current {
// if let next = _current.next {
// next.description
// _current.next = last
// current = next
// last = _current
// } else {
// _current.next = last
// }
// _current.description
// }
// }
// return current!
// }
//}
//
//var node = LinkedListNode<Int>(data: 1)
//node.next = LinkedListNode<Int>(data: 2)
//node.next!.next = LinkedListNode<Int>(data: 3)
//node.next!.next!.next = LinkedListNode<Int>(data: 4)
//node.next!.next!.next!.next = LinkedListNode<Int>(data: 5)
//node.description
//node.reverse().description
//func fibRecursive(i : Int) -> Int {
// if (i < 0) {
// return 0
// } else if (i == 0) {
// return 0
// } else if (i == 1) {
// return 1
// } else {
// return fibRecursive(i-1)+fibRecursive(i-2)
// }
//}
//
//func fibIterative(i : Int) -> Int {
// if (i < 0) {
// return 0
// } else if (i == 0) {
// return 0
// } else if (i == 1) {
// return 1
// }
//
// var lastLast = 0
// var last = 1
// var solution = 0
//
// for count in 2...i {
// solution = lastLast + last
// lastLast = last
// last = solution
// }
//
// return solution
//}
//
//func fib(i : Int, recursive : Bool) -> Int {
// if (recursive) {
// return fibRecursive(i)
// } else {
// return fibIterative(i)
// }
//}
//
//
//fib(11, false)
//fib(2)
//fib(3)
//fib(4)
//fib(5)
//fib(6)
//fib(7)
//enum Direction : Int {
// case North = 0
// case East = 1
// case South = 2
// case West = 3
//}
//
//extension Direction : Printable {
// var description : String {
// switch (self) {
// case .North:
// return "North"
// case .East:
// return "East"
// case .South:
// return "South"
// case .West:
// return "West"
// }
// }
//}
//
//extension Direction {
// static var allCases : [Direction] {
// return Array(
// SequenceOf {
// () -> GeneratorOf<Direction> in
// var cur = 0
// return GeneratorOf<Direction> {
// return Direction(rawValue: cur++)
// }
// }
// )
// }
//}
//
//print(Direction.North.description)
//for c in Direction.allCases {
// c.description
//}
//
//var s = 0..<5
//var g = s.generate()
//while var i = g.next() {
// i
//}
//
//class Fib : SequenceType {
// func generate() -> GeneratorOf<Int> {
// var c = 0, n = 1
// return GeneratorOf <Int> {
// var ret = c
// c = n
// n = c + ret
// return ret
// }
// }
//}
//
//var a = filter(Fib()) { $0 % 2 == 0 }
//var g = a.generate()
//for _ in 0..<10 {
// println(g.next())
//}
//func checkSomething(f : @autoclosure () -> BooleanType) -> Bool {
// return f() ? true : false
//}
//
//checkSomething(Int.max == Int.max)
//var s = "Hello World"
//var g = s.generate()
//while var c = g.next() {
// print("\(c) \n")
//}
//func sometimesNil() -> String? {
// return (Int(NSDate().timeIntervalSince1970) % 2) == 1 ? nil : "1"
//}
//
//(sometimesNil() as NSString?)?.integerValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment