Skip to content

Instantly share code, notes, and snippets.

View jamesrochabrun's full-sized avatar
🇵🇪

James Rochabrun jamesrochabrun

🇵🇪
View GitHub Profile
@jamesrochabrun
jamesrochabrun / Threesumtozero.swift
Created May 3, 2017 02:06
Problem: Given a array of distinct integers, how many triple integers sum to exactly zero
//
// ComputationalGeometry.swift//
// Created by James Rochabrun on 5/2/17.
//
//
import Foundation
//types of algorhitms
//1) constant -> 1 (1)
@jamesrochabrun
jamesrochabrun / BinarySearchString.swift
Last active April 26, 2017 05:35
binary search in array of strings sorted alphabetically
var arr = ["a", "abc", "aabc", "aabbc", "aaabbbcc", "bacc", "bbcc", "bbbccc", "cb", "cbb", "cbbc", "d" , "defff", "deffz"]
func binarySearch(_ array: [String], value: String) -> String {
var firstIndex = 0
var lastIndex = array.count - 1
var wordToFind = "Not founded"
var count = 0
//1 Download the assets
https://www.yelp.com/developers/display_requirements
//2 enum to enumerate assets
enum ReviewIcon: NSNumber {
case zeroStar
case oneStar
case oneAndHalfStar
case twoStar
//: Playground - noun: a place where people can play
import UIKit
//GENERICS
//passing the reference in a method using the inout keyword
func swapInts(_ a: inout Int, _ b: inout Int) {
let tempA = a
a = b
//Given this classes
class Address {
var buildingNumber: String?
var street: String?
}
class Residence {
var numberOfRooms = 1
var address: Address?
}
class Resident {
//OPTIONAL BINDING
let weatherDictionary: [String: String] = ["currentWeather" : "22.4", "weeklyWeather" : "25.9"]
if let weekly = weatherDictionary["weeklyWeather"] {
print(weekly)
}
//prints 25.9
//DOWNSIDES OF USING IF LET AND WHY GUARD SOLVE THIS
struct Friend {
class Person {
let name: String
var apartment: Apartment?
init(name: String) {
self.name = name
}
deinit {
print("\(name) is being deinitialized, memory deallocated")
//: Playground - noun: a place where people can play
import UIKit
//fizbuzz
var oneHundred = [Int]()
for i in 1...100 {
oneHundred.append(i)
}
//1) What is ARC and how does it work?
//Every time you create a new instance of a class, ARC allocates a chunk of memory to store information about that instance.
//This memory holds information about the type of the instance, together with the values of any stored properties associated
//with that instance.
//Additionally, when an instance is no longer needed, ARC frees up the memory used by that instance so that the memory
//can be used for other purposes instead. This ensures that class instances do not take up space in memory when they are
//no longer needed.
//However, if ARC were to deallocate an instance that was still in use, it would no longer be possible to access that
//instance’s properties, or call that instance’s methods. Indeed, if you tried to access the instance, your app would most likely crash.
@jamesrochabrun
jamesrochabrun / Comparable.swift
Created March 27, 2017 22:15
Conforming to comparable to sort array by dates
class Person: Comparable {
var birthDate: NSDate?
let name: String
init(name: String) {
self.name = name
}
static func ==(lhs: Person, rhs: Person) -> Bool {
return lhs.birthDate === rhs.birthDate || lhs.birthDate?.compare(rhs.birthDate as! Date) == .orderedSame