Skip to content

Instantly share code, notes, and snippets.

View mklbtz's full-sized avatar

Michael Bates mklbtz

View GitHub Profile
# O(nk)
class Array
def unzip ary
return [] if ary.empty?
count = ary.first.count
(0...count).map { |i|
ary.map { |tuple|
raise 'inconsistent tuple size' if tuple.count != count
tuple[i]
}
@mklbtz
mklbtz / circles.py
Created July 4, 2015 21:54
circle-finder.py
# The MIT License (MIT)
#
# Copyright (c) 2015 Michael Bates
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@mklbtz
mklbtz / Array+partition.swift
Last active September 2, 2017 21:00
New array method: partition
// Here, I’ve added a function to Array called `partition`.
// In short, the function will partition the Array into a Dictionary based on a function
// which takes an Element of the Array and returns its corresponding Key into the Dictionary.
extension Array {
func partition<Key>(key: (Element)->Key) -> [Key: [Element]] {
var groups = [Key: [Element]]()
for element in self {
let key = key(element)
var group = groups[key] ?? [Element]()