Skip to content

Instantly share code, notes, and snippets.

@EvolvingParty
Created November 16, 2022 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EvolvingParty/90dfa4f01843473a68d728c82918e867 to your computer and use it in GitHub Desktop.
Save EvolvingParty/90dfa4f01843473a68d728c82918e867 to your computer and use it in GitHub Desktop.
DAY 9: 100 Days of SwiftUI – Hacking with Swift. Checkpoint 5
import Cocoa
///You’ve already met sorted(), filter(), map(), so I’d like you to put them together in a chain – call one, then the other, then the other back to back without using temporary variables.
///Your input is this: let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
///Your job is to:
///Filter out any numbers that are even
///Sort the array in ascending order
///Map them to strings in the format “7 is a lucky number”
///Print the resulting array, one item per line
func getLuckyNumbers(for numbers: [Int]) {
let newArray = numbers.filter { !$0.isMultiple(of: 2) }
let sortedArray = newArray.sorted { $0 < $1 }
let newStrings = sortedArray.map { "\($0) is a lucky number" }
for result in newStrings {
print(result)
}
}
let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
getLuckyNumbers(for: luckyNumbers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment