Skip to content

Instantly share code, notes, and snippets.

@possen
possen / BlockNotificationSample.swift
Last active November 9, 2015 04:01
Example Implementation of a closure based api similar to NSNotificationCenter -- should support multiple threads by serializing access, allows adding observers for a particular key where all blocks will get called back. for that key
//
// Description: Example implementation similar to NSNotificationCenter
// Language: Swift 2.1
// Date: November 7, 2015
//
// Author: Paul Ossenbruggen
//
// should support multiple threads by serializing access.
//
// allows posting to observers for a particular key where all blocks will get
@possen
possen / SwiftTrie.swift
Created November 7, 2015 23:59
Implementation of a Trie In Swift Sample
// Description: Implementation of a Trie
//
// Language: Swift 2.1
// Author: Paul Ossenbruggen
// Nov 7, 2015
import Foundation
class Trie {
var last : Bool = false
@possen
possen / SwitchProblemSolution.swift
Last active September 11, 2022 22:07
Prisoners visit a room where there is a light switch that is not connected to anything, when all the prisoners have visited the room they can confidently know that they were all in there saving them from the crocodiles.
//
// Description Switch problem in Swift solution
//
// Author: Paul Ossenbruggen
// Date: Nov 8, 2015
// Language: Swift 2.1
// See comment below for problem statement. This solves the second version where we don't know the state
// of the switch at the beginning.
//
@possen
possen / SillyHatVillage.swift
Created November 8, 2015 21:09
Silly Hat village CS problem where through deduction, the town saves its self from having to tell jokes for 100-1 years.
//
// Description Silly Hat village in Swift solution
//
// Author: Paul Ossenbruggen
// Date: Nov 8, 2015
// Language: Swift 2.1
// See comment below for problem statement.
//
import Foundation
@possen
possen / Change.swift
Last active July 9, 2020 04:35
Various solutions for finding number change possibilities and minimum number of change possibilitis
import Foundation
//
// Description; Various solutions for finding number change possibilities and minimum number of change possibilitis.
//
// Author: Paul Ossenbruggen
// Date: Nov 8, 2015
// Updated: June 8, 2020
// Language: Swift 5.3
// See comments below for problem statement.
//
// Description: Finds the sum of all elements that add up to the target value using dynamic programming.
// Note: input array must be non negative and constrained to a reasonable ranage because it uses target+1 memory
//
// Language: Swift
//
// Author: Paul Ossenbruggen
// Date: Nov 10, 2015
//
@possen
possen / ShortestDistanceGraph.swift
Last active November 17, 2015 15:59
Description: Shortest distance graph. Calculates the distances from a start node to any node in the graph
//
// Description: Shortest distance graph. Calculates the distances from a start node to any node in the graph.
// Date: Nov 18, 2015
//
// Language: Swift
//
// Author: Paul Ossenbruggen
//
struct Edge : Equatable, Hashable, CustomStringConvertible {
let weight : Int
@possen
possen / RandomArray.swift
Created November 20, 2015 22:28
RandomArrays: pick randomly to build new array in two different ways
//
// Description: RandomArrays: pick randomly to build new array in two different ways. One a shuffle the other
// picking from the array. Shuffle uses Fischer-Yates algorithm to generate uniform randomness. Allthough it
// might be better to use Apple's Game kit function GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(array)
// if available in the OS release. Also this only works on arrays, if you want to shuffle any colletion type
// it is possible to extend MutableCollectionType.
//
// Author: Paul Ossenbruggen
//
// Date: Nov 20, 2015
@possen
possen / IsSingleRiffle.swift
Last active November 22, 2015 18:38
Description: Single Riffle detection, try to detct if a deck of cards has been riffled only once
//
// Description: Single Riffle detection, try to detct if a deck of cards has been riffled only once.
//
// Language: Swift 2.1 (playground)
// Author: Paul Ossenbruggen
// Date: Nov 21, 2015
//
// Uses a stack approach because it is easy to get the next element. It could be done with iterators too which
// be non destructive and might be faster, since swift copies the array a distructive approach should work.
//
@possen
possen / AnagramFinder.swift
Last active May 17, 2020 06:22
takes a list of anagrams and outputs the words in the same order as they appear in original string. Also outputs all the matched words after that in the order they appear after that.
//
// Anagram finder.
//
// Description: takes a list of anagrams and outputs the words in the same order as they appear in original string.
// Also outputs all the matched words after that in the order they appear after that.
//
// Language: Swift 5
// Author: Paul Ossenbruggen
// Date: Dec 4, 2015
//