Skip to content

Instantly share code, notes, and snippets.

View AdrianFerreyra's full-sized avatar

Adrián Ferreyra AdrianFerreyra

  • Sports Illustrated Play
  • Buenos Aires, Argentina
View GitHub Profile
func getMonkeysAsynchronously(completionHandler: ([Monkey]) -> Void) { ... }
import Foundation
var a = 0
let closure = {
return a
}
a = 1
import UIKit
import PlaygroundSupport
// https://gist.github.com/erica/6f13f3043a330359c035e7660f3fe7f5
// Original Video: https://www.youtube.com/watch?v=TTmWUSgNOHk
// Video: https://www.youtube.com/watch?v=hmAB3WJOQTU
// Video: https://www.youtube.com/watch?v=DWtavuvmKdw (with zoom and fade)
// String to animate and its attributes
var string = "Hello, playground"
let attributes: [String: Any] = [
@AdrianFerreyra
AdrianFerreyra / In-SituMergeSort.swift
Created October 31, 2017 03:25
In-Situ implementation of MergeSort
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:
@AdrianFerreyra
AdrianFerreyra / recursiveInsertionSort.swift
Created October 31, 2017 12:41
A brief recursive implementation of InsertionSort on generic Comparable element's arrays in Swift 4.
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]
@AdrianFerreyra
AdrianFerreyra / linkedListSwapElements.swift
Last active October 31, 2017 18:54
Return the head node of the singly linked list with each pair of nodes swapped. If there is a last odd node leave it in place. Example: Input: 1 -> 2 -> 3 -> 4 -> 5 Output: 2 -> 1 -> 4 -> 3 -> 5
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()))
@AdrianFerreyra
AdrianFerreyra / recursivePermutations.swift
Created November 1, 2017 19:52
Getting permutations of a Set of Strings recursively.
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
@AdrianFerreyra
AdrianFerreyra / equivalentStrings.swift
Created November 1, 2017 22:07
Resolution of a Facebook iOS Code Challenge.
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".
*/
@AdrianFerreyra
AdrianFerreyra / findCommonSuperviews.swift
Created November 1, 2017 22:19
How to detect a common superview (IOS Developer Interview Question)
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)
@AdrianFerreyra
AdrianFerreyra / resetZeros.swift
Created November 2, 2017 03:02
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]
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]