Skip to content

Instantly share code, notes, and snippets.

import UIKit
enum Dimension {
case X, Y
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
- (void)replaceFileNamesOfImagesWithUUIDsAtPath:(NSString *)path {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *directoryURL = [NSURL fileURLWithPath:path];
NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];
NSDirectoryEnumerator *enumerator = [fileManager
enumeratorAtURL:directoryURL
includingPropertiesForKeys:keys
options:0
errorHandler:^(NSURL *url, NSError *error) {
@simonbromberg
simonbromberg / histogram.m
Last active May 26, 2016 16:09
Calculate histogram data
- (void)getMax:(float *)max min:(float *)min on:(NSArray *)arr {
for (NSNumber *num in arr) {
*max = MAX(*max, num.floatValue);
*min = MIN(*min, num.floatValue);
}
}
- (int)indexForResponse:(float)response max:(float)max min:(float)min bucketCount:(int)count {
float bucketWidth = (max - min) / count;
return floorf((response - min) / bucketWidth);
'a simple, (and pretty inefficient, but easy) macro to flip the values in a horizontal or vertical range. Saves a lot of time when you just want to swap two values or flip a list.
Sub flipRange()
'
' flipRange Macro
'
'
Dim i As Long
Dim num As Long
@simonbromberg
simonbromberg / toggleEqualSigns.vba
Created May 26, 2016 16:13
Replaces all the "=" with "#" or vice versa depending on first cell
' Replaces all the "=" with "#" or vice versa depending on first cell
' This is useful for copying cells that contain formulas with cell references that you don't want to change when you move them. For example, transposing, copying between workbooks
'
Sub toggleEqualsSign()
'
' toggleEqualsSign Macro
Dim replaceWhat As String
Dim replaceWithThat As String
Sub extractNumbersFromCells()
'
'
' Extract numbers from cells in selected range
'
Dim rng As Range
Dim Last As Long
Last = ActiveSheet.UsedRange.Rows.Count
For Each rng In Selection
- (NSString *)addSpacesToCharacters:(NSArray *)characters {
if (characters.count <= 1) {
return characters.firstObject;
}
// bounding rect of base string, divide by length, calculate optimium number of spaces
CGRect boundingRect = CGRectZero;
CGSize constraintSize = self.view.frame.size;
NSString *string = [characters componentsJoinedByString:@""];
@simonbromberg
simonbromberg / Quicksort.swift
Last active July 6, 2016 18:01
Quicksort playground
import Darwin
func quickSort(inout data:[Int], _ left:Int, _ right:Int) {
let index = partition(&data, left, right)
if left < index - 1 {
quickSort(&data, left, index - 1)
}
if index < right {
quickSort(&data, index, right)
@simonbromberg
simonbromberg / Palindrome.swift
Created August 11, 2016 02:48
Swift 2 function to test if a string is a palindrome
func isPalindrome(string:String) -> Bool {
if string.isEmpty || string.characters.count == 1 {
return true
}
var fromStart = string.startIndex
var fromEnd = string.endIndex.predecessor()
while fromStart != fromEnd {
if string[fromStart] != string[fromEnd] {
@simonbromberg
simonbromberg / NSObject+ClassName.swift
Created January 1, 2017 04:54
Extension for getting string of class name in Swift (could extend any type of class really, perhaps even AnyObject); replacement for NSStringFromClass() in Objective C
import Foundation
extension NSObject {
var className: String {
return String(describing: type(of: self))
}
class var className: String {
return String(describing: self)
}