Skip to content

Instantly share code, notes, and snippets.

class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = -1.0
attributes?.forEach { layoutAttribute in
if layoutAttribute.frame.origin.y >= maxY {
leftMargin = sectionInset.left
@possen
possen / SudokuValidator.swift
Last active April 14, 2017 04:56
Validates a sudoku puzzle.
import Foundation
func validateSudoku(array : [Int]) -> Bool {
let width = 9
func valueAtPos(x: Int, y: Int, array: [Int]) -> Int {
return array[y * width + x]
}
@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
//
@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 / 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
//
// 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 / 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.
@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 / 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 / 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