Skip to content

Instantly share code, notes, and snippets.

@proxpero
proxpero / Iso3166_1a2.swift
Last active February 20, 2024 16:44
A Swift 4.0 enum representing ISO 3166-1 alpha-2 (two letter country codes) Includes properties for country name and unicode flag.
// taken 2018-03-19 from wikipedia. https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
enum Iso3166_1a2: String {
case af = "AF"
case ax = "AX"
case al = "AL"
case dz = "DZ"
case `as` = "AS"
case ad = "AD"
case ao = "AO"
@proxpero
proxpero / Permutations.swift
Last active September 18, 2023 13:17
Generate the permutations of a Swift array.
//: Permutations
// based on https://www.objc.io/blog/2014/12/08/functional-snippet-10-permutations/
// but updated for Swift 2.0 (Xcode 7.0)
extension Array {
func decompose() -> (Generator.Element, [Generator.Element])? {
guard let x = first else { return nil }
return (x, Array(self[1..<count]))
}
}
@proxpero
proxpero / Iso369_1.swift
Created March 19, 2018 13:18
A Swift 4.0 enum representing ISO 639-1 codes (used to classify languages)
// taken 2018-03-19 from wikipedia. https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
public enum Iso639_1: String {
case ab = "AB"
case aa = "AA"
case af = "AF"
case ak = "AK"
case sq = "SQ"
case am = "AM"
case ar = "AR"
@proxpero
proxpero / NSColor+RGB
Created January 18, 2016 13:32
An simple extension to NSColor to create a color from a six character RGB string. Written in Swift.
// Swift 2.2
extension NSColor {
convenience init(rgb: String) {
guard rgb.characters.count == 6 else { fatalError("Invalid rgb value: \(rgb)") }
let scanner = NSScanner(string: rgb)
var hexValue: UInt32 = 0
@proxpero
proxpero / MergingOverlappingIntervals.swift
Last active September 25, 2022 17:06
Merging Overlapping Intervals (`Range<Int>`s) in Swift
//: [Merging Overlapping Intervals](https://www.shiftedup.com/2015/05/17/programming-challenge-merging-overlapping-intervals)
//: Xcode 7.0, Swift 2.0
/*: Given a collection of intervals, write a function that merges all overlapping intervals and prints them out.
For example, given [1, 3], [2, 6], [8, 10], and [7, 11], the function should print [1, 6], [7, 11]. Or given [5, 12], and [8, 10] the function should print [5, 12].
You can assume that the first element of each interval is always less or equal than the second element of the interval.
*/
//: Note: The challenge suggests that each interval is an `Array<Int>` where `interval.count == 2` and `interval[0] <= interval[1]`. In Swift, these intervals should be modeled by the `Range<Int>` type. I am altering the challenge to suit the language, I know. [TKO]
public func ack(_ m: Int, _ n: Int) -> Int {
if m == 0 { return n + 1 }
if n == 0 { return ack(m - 1, 1) }
return ack(m - 1, ack(m, n - 1))
}
for i in (0...5) {
for j in (0...5) {
print("ack(\(i), \(j)) is \(ack(i, j))")
}
@proxpero
proxpero / Matrix.swift
Last active December 17, 2021 04:31
Rotating a matrix 90 degrees in place in Swift
//: Rotate a matrix 90 degrees in place
// "Write a function to rotate an NxN matrix by 90 degrees. You should rotate it in place, meaning you can't use another matrix to perform the rotation, but instead you have to use the same given structure."
// https://www.shiftedup.com/2015/05/10/programming-challenge-rotating-a-matrix-90-degrees-in-place
// This implementation of a matrix is taken from Apple's own.
// https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Subscripts.html
// The extensions are my own.
// Xcode 7.0, Swift 2.0
@proxpero
proxpero / zip3.swift
Created December 28, 2017 18:49
An implementation of zip that creates a sequence of three-item tuples from the combination of three sequences. Much like the standard library's `zip` function for a pair of sequences.
/// Creates a sequence of tuple-3s built out of three underlying sequences.
/// Based on Zip2Seqence from the Swift Standard Library.
/// https://github.com/apple/swift/blob/9361a6b66f6f8351e89c090f604d7e1f42e2a045/stdlib/public/core/Zip.swift
///
/// - Parameters:
/// - sequence1: The first sequence or collection to zip.
/// - sequence2: The second sequence or collection to zip.
/// - sequence3: The third sequence or collection to zip.
/// - Returns: A sequence of tuple triples, where the elements of each triplet are
/// corresponding elements of `sequence1`, `sequence2`, and `sequence3`.
@proxpero
proxpero / Barcode.swift
Last active May 12, 2019 21:26
Encoding and Decoding Enums with Associated Values
// See http://proxpero.com/2017/07/11/encoding-and-decoding-custom-enums-with-associated-values-in-swift-4/
enum Barcode {
case upc(Int, Int, Int, Int)
case qrCode(String)
}
extension Barcode: Codable {
init(from decoder: Decoder) throws {
self = try Barcode.Coding.init(from: decoder).barcode()
}
@proxpero
proxpero / aoc2018d1.swift
Last active December 2, 2018 06:59
Advent of Code 2018, day 1
// prepare the puzzle input
let input = loadFile(named: "day1") // convenience function in resources
.split(separator: "\n")
.compactMap(Int.init) // extension on Int to init? with String.Subsequence
// part 1
let part1 = input.reduce(0, +)
// part 2