This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Converts an `EncodedMessage` to an alternate representation. | |
struct MorseTransformation<T> { | |
let dot, dash, markSeparator, symbolSeparator, termSeparator: T | |
func apply(to encodedMessage: EncodedMessage) -> [T] { | |
return encodedMessage.apply(self) | |
} | |
} | |
private extension EncodedMessage { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum TransmissionState { | |
typealias RelativeDuration = Int | |
case On(RelativeDuration) | |
case Off(RelativeDuration) | |
static func createStates(from encodedMessage: EncodedMessage) | |
-> [TransmissionState] { | |
let transformation = MorseTransformation( | |
dot: TransmissionState.On(1), | |
dash: TransmissionState.On(3), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func createMorseCodeText(from encodedMessage: EncodedMessage) -> String { | |
let transformation = MorseTransformation( | |
dot: ".", | |
dash: "-", | |
markSeparator: "", | |
symbolSeparator: " ", | |
termSeparator: "\n") | |
let characters = transformation.apply(to: encodedMessage) | |
return characters.joinWithSeparator("") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// HigherOrderFunctions | |
// | |
// Created by Joshua Smith on 12/6/15. | |
// Copyright © 2015 iJoshSmith. All rights reserved. | |
// | |
/* | |
This file contains simple implementations of several |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> foldr (-) 0 [3, 2, 1] | |
2 | |
> (3 - (2 - (1 - 0))) | |
2 | |
> foldl (-) 0 [3, 2, 1] | |
-6 | |
> (((0 - 3) - 2) - 1) |
NewerOlder