Skip to content

Instantly share code, notes, and snippets.

@msepcot
msepcot / HealthKitPRSearch.swift
Created March 3, 2021 01:07
Search HealthKit data for running PRs.
//
// ViewController.swift
// WatchAndLearn
//
// Created by Michael Sepcot on 9/18/19.
// Copyright © 2019 Michael Sepcot. All rights reserved.
//
import UIKit
import HealthKit
@msepcot
msepcot / RandomAccessCollection+BinarySearch.swift
Created November 6, 2021 00:15
Binary Search extension on RandomAccessCollection that lets the caller do the compare on each element.
public extension RandomAccessCollection {
func binarySearch(comparing: (Element) -> ComparisonResult) -> Element? {
var lowerBound = startIndex
var upperBound = endIndex
while lowerBound < upperBound {
let size = distance(from: lowerBound, to: upperBound)
let midIndex = index(lowerBound, offsetBy: size / 2)
switch comparing(self[midIndex]) {
case .orderedSame:
@msepcot
msepcot / AgeGradeCalculator.swift
Last active April 6, 2021 03:16
A port of the Runner's World Age Grading Calculator to Swift
import Foundation
enum Gender {
case male, female // no non-binary data :/
}
enum Distance {
case fiveKm, sixKm, fourMi, eightKm, fiveM, tenKm, twelveKm, fifteenKm, tenMi, twentyKm, halfMarathon, twentyFiveKm, thirtyKm, marathon, fiftyKm, fiftyMi, oneHundredKm, oneHundredFiftyKm, oneHundredMi, twoHundredKm
}
@msepcot
msepcot / MarathonCalculator.swift
Created March 3, 2021 01:04
Exploring different marathon race time calculations.
import Foundation
class Calculator {
class Constants {
static let kilometersPerMile = 1.60934
static let metersPerMile = 1609.34
}
enum Distance: Double { // rawValue in miles
case marathon = 26.21875
@msepcot
msepcot / TheFatedSky.rb
Created December 7, 2018 20:40
The Fated Sky: A Lady Astronaut Novel, Chapter 16 cypher text.
PLAINTEXT = %w(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
CRYPTTEXT = %w(E L P H A N T B C D F G I J K M O Q R S U V W X Y Z)
input = "Cj qarmkjra sk ykuq gers gassaq, C ei waeqcjt iy ngctbs rucs lus hk jks beva e lqe kj. Cn yku waqa baqa, C wkugh sefa yku cjsk sba teqhaj ikhuga ejh gaej kvaq sba skiesk lahr rk sbes er yku skkf ia nqki labcjh, iy nepa wkugh la mqarrah cjsk sba nqetqejs tqaaj gaevar wcsb aepb sbqurs.".upcase
input.split("").each do |character|
if position = CRYPTTEXT.find_index(character)
print PLAINTEXT[position]
else
print character
@msepcot
msepcot / CallDirectoryHandler.swift
Created April 15, 2017 06:05
Exploring the CallKit blocking feature
//
// CallDirectoryHandler.swift
// SelectiveHearing.BlockEveryone
//
// Created by Michael Sepcot on 4/14/17.
// Copyright © 2017 Michael Sepcot. All rights reserved.
//
import Foundation
import CallKit
require 'dfp_api'
require 'httparty'
require 'oauth2'
CLIENT_ID = 'MY CLIENT ID'
CLIENT_SECRET = 'MY CLIENT SECRET'
NETWORK_CODE = 'MY NETWORK CODE'
dfp = DfpApi::Api.new({
authentication: {
@msepcot
msepcot / FadeableButton.swift
Last active September 6, 2017 06:05
Implement Mail.app's filter button in code
//
// FadeableButton.swift
// Octomino
//
// Created by Michael Sepcot on 9/5/17.
// Copyright © 2017 Head Down Development. All rights reserved.
//
import UIKit
@msepcot
msepcot / branches.sh
Last active June 27, 2016 21:03
Bash script to output a list of git branches sorted by last commit date, include last commit author's name.
#!/bin/bash
$(git fetch --prune)
for branch in $(git branch -r); do
if ([ "$branch" == "origin/HEAD" ] || [ "$branch" == "origin/master" ] || [ "$branch" == "origin/develop" ] || [ "$branch" == "->" ] || [[ "$branch" =~ origin\/[0-9]+\.[0-9](\.[0-9])?$ ]]); then
continue
fi
printf "$(git log $branch -n1 --pretty=format:"%an (%ad): $branch" --date=short)\n";
done | sort
@msepcot
msepcot / enumerable-deep_find.rb
Created May 22, 2013 18:35
Add deep_find ability to Enumerable to recursively search for a Hash key. Returns the first key found.
module Enumerable
def deep_find(key)
if respond_to?(:key?) && key?(key)
self[key]
else
found = nil
find { |*a| v = a.last; found = v.is_a?(Enumerable) ? v.deep_find(key) : nil }
found
end
end