Skip to content

Instantly share code, notes, and snippets.

@natecook1000
Forked from airspeedswift/joke.swift
Last active October 22, 2017 16:00
Show Gist options
  • Save natecook1000/db3c3996107ef818ba6bbba5df5ec952 to your computer and use it in GitHub Desktop.
Save natecook1000/db3c3996107ef818ba6bbba5df5ec952 to your computer and use it in GitHub Desktop.
Multiline Literals
func tellJoke(name: String, character: Character) {
let punchline = String(name.filter { $0 != character })
let n = name.count - punchline.count
let joke = """
Q: Why does \(name) have \(n) \(character)'s in their name?
A: I don't know, why does \(name) have \(n) \(character)'s in their name?
Q: Because otherwise they'd be called \(punchline).
"""
print(joke)
}
tellJoke(name: "Edward Woodward", character: "d")
func tellJoke(name: String) {
let ones = sequence(first: 1, next: { _ in 1 })
let frequencies = Dictionary(zip(name, ones), uniquingKeysWith: +)
let highest = frequencies.sorted { $0.value > $1.value }.first!.key
tellJoke(name: name, character: highest)
}
tellJoke(name: "Barack Obama")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment