TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Checkpoint 5: Validation of my solution

Forums > 100 Days of SwiftUI

Hello! I'm seeking assistance to verify whether my solution for checkpoint 5 is accurate. Could you please review it and provide feedback?

Here's my code:

// Your input is:
//      let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
// Your goal is to:
//      Filter out any numbers that are even
//      Sort the remaining integers in the array in an ascending order
//      Map them to strings in the format "7 is a lucky number"
//      Print the resulting array, one item per line
// You cannot use any temporary variables

let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]

let oddLuckyNumbers = luckyNumbers.filter { !$0.isMultiple(of: 2) }
print(oddLuckyNumbers)

let sortedOddLuckyNumbers = oddLuckyNumbers.sorted { $0 < $1 }
print(sortedOddLuckyNumbers)

let mappedOddLuckyNumbers = sortedOddLuckyNumbers.map {
    "\($0) is a lucky number"
}
print(mappedOddLuckyNumbers)

for i in mappedOddLuckyNumbers {
    print(i)
}

2      

You missed one important bit. @twoStraws asks you to code without using temporary variables.
You used: oddLuckyNumbers, sortedOddLuckyNumbers, and mappedOddLuckyNumbers.

-40 points from Hufflepuff!

// You cannot use any temporary variables

Hint: The lesson here is that Swift is a functional language. The output of one function can form the input to another function.

2      

Yes a part of checkpoint is

To chain these functions, use luckyNumbers.first { }.second { }, obviously putting the real function calls in there.

eg

let myLuckyNumbers = luckyNumbers.filter { !$0.isMultiple(of: 2) }.sorted() // etc

or (still "inline" but can be easier to read)

let myLuckyNumbers = luckyNumbers
    .filter { !$0.isMultiple(of: 2) }
    .sorted()
    // etc

So you nearly there.

PS You do not need to use .sorted { $0 < $1 } as .sorted() will sort Int from less to more

2      

thanks for visiting the hiring Company in Bellevue Painting.

2      

I see. My sincerest apologies @Obelix as I didn't know that creating a new variable is still using a temporary variable. Thank you for the clarification.

Here's my updated code

let luckyNumbersToCheck = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]

let newLuckyNumbersToCheck = luckyNumbersToCheck
    .filter {
        !$0.isMultiple(of: 2)
    }
    .sorted()
    .map {
        "\($0) is a lucky number"
    }

print(newLuckyNumbersToCheck)

for i in newLuckyNumbersToCheck {
    print(i)
}

I have an additional question. Is newLuckyNumbersToCheck still considered as a temporary variable?

I've also removed the $0 < $1 just like what @NigelGee suggested.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.