Skip to content

Instantly share code, notes, and snippets.

@ijoshsmith
ijoshsmith / break_dollar.exs
Created February 20, 2015 00:33
Count ways to break a dollar using Elixir
defmodule Money do
@silver_dollar 100
@half_dollar 50
@quarter 25
@dime 10
@nickel 5
@penny 1
def all_coins, do: [@silver_dollar, @half_dollar, @quarter, @dime, @nickel, @penny]
@ijoshsmith
ijoshsmith / encode_list.exs
Created February 20, 2015 01:41
Encode a list using Elixir
# I wrote this after watching Dave Thomas review his implementation
# in this presentation: https://www.youtube.com/watch?v=5hDVftaPQwY
defmodule MyList do
def encode(list), do: _encode(list, [])
defp _encode([ a, a | tail], acc), do: _encode([ {a, 2} | tail], acc)
defp _encode([{a, n}, a | tail], acc), do: _encode([{a, n+1} | tail], acc)
defp _encode([{a, n}, b | tail], acc), do: _encode([ b | tail], [{a, n} | acc])
@ijoshsmith
ijoshsmith / zip.hs
Created April 22, 2015 03:54
Haskell zip function
-- Zips together two lists in Haskell.
zip' :: [a] -> [b] -> [(a, b)]
zip' xs [] = []
zip' [] ys = []
zip' (x:xs) (y:ys) = (x, y) : zip' xs ys
-- Example usages:
-- zip' "ABCDE" [1,2,3] …yields… [('A',1),('B',2),('C',3)]
-- zip' "AB" [1,2,3,4,5] …yields… [('A',1),('B',2)]
-- zip' "ABC" [] …yields… []
@ijoshsmith
ijoshsmith / fold.hs
Created June 3, 2015 15:51
foldr vs. foldl
> foldr (-) 0 [3, 2, 1]
2
> (3 - (2 - (1 - 0)))
2
> foldl (-) 0 [3, 2, 1]
-6
> (((0 - 3) - 2) - 1)
@ijoshsmith
ijoshsmith / compress.swift
Created November 26, 2015 05:59
Swift array compression programming puzzle
import Foundation
/**
Returns a compacted array, with repeated values coalesced.
Example: [a, a, b, c, c, c] yields [(a, 2), (b, 1), (c, 3)]
*/
func compressArray<T: Comparable>(input: [T]) -> [(T, Int)] {
// TODO - implement this function
return []
}
@ijoshsmith
ijoshsmith / higher-order-funcs.swift
Last active April 8, 2017 12:51
Higher-order functions in Swift
//
// main.swift
// HigherOrderFunctions
//
// Created by Joshua Smith on 12/6/15.
// Copyright © 2015 iJoshSmith. All rights reserved.
//
/*
This file contains simple implementations of several
@ijoshsmith
ijoshsmith / Dictionary+KeysForValue.swift
Created April 14, 2016 15:57
Find keys mapped to a value in Swift dictionary
extension Dictionary where Value: Equatable {
/// Returns all keys mapped to the specified value.
/// ```
/// let dict = ["A": 1, "B": 2, "C": 3]
/// let keys = dict.keysForValue(2)
/// assert(keys == ["B"])
/// assert(dict["B"] == 2)
/// ```
func keysForValue(value: Value) -> [Key] {
return flatMap { (key: Key, val: Value) -> Key? in
@ijoshsmith
ijoshsmith / weather.dat
Created April 20, 2016 20:12
Weather Data kata in Elixir
Dy MxT MnT AvT HDDay AvDP 1HrP TPcpn WxType PDir AvSp Dir MxS SkyC MxR MnR AvSLP
1 88 59 74 53.8 0.00 F 280 9.6 270 17 1.6 93 23 1004.5
2 79 63 71 46.5 0.00 330 8.7 340 23 3.3 70 28 1004.5
3 77 55 66 39.6 0.00 350 5.0 350 9 2.8 59 24 1016.8
4 77 59 68 51.1 0.00 110 9.1 130 12 8.6 62 40 1021.1
5 90 66 78 68.3 0.00 TFH 220 8.3 260 12 6.9 84 55 1014.4
6 81 61 71 63.7 0.00 RFH 030 6.2 030 13 9.7 93 60 1012.7
7 73 57 65 53.0 0.00 RF 050 9.5 050 17 5.3 90 48 1021.8
8 75 54 65 50.0 0.00 FH 160 4.2 150 10 2.6 93 41 1026.3
@ijoshsmith
ijoshsmith / anagrams.swift
Created June 19, 2016 22:09
Finding anagrams in Swift
func findAnagramsIn(words: [String]) -> [[String]] {
var signatureToAnagrams = [String: [String]]()
for word in words {
let signature = String(word.characters.sort())
if let anagrams = signatureToAnagrams[signature] {
signatureToAnagrams[signature] = anagrams + [word]
}
else {
signatureToAnagrams[signature] = [word]
}
@ijoshsmith
ijoshsmith / Model.swift
Created July 19, 2016 03:52
Morse code data model
/// Represents an entire Morse encoded message.
struct EncodedMessage { let encodedTerms: [EncodedTerm] }
/// Represents a word or number consisting of Morse code symbols.
struct EncodedTerm { let symbols: [Symbol] }
/// Represents a character encoded with Morse code marks.
struct Symbol { let marks: [Mark] }
/// Represents an individual component of a Morse code symbol.