|
I kind of understand hoe to make the logic but i don't know how to do it in a single line |
|
I haven't looked at the particular challenge in question. So my comment here is in a general context and not about this particular quesiton (which I'll also go look at when time permits): As someone who's been doing this for over 3 decades, there's a universal truth about "clever" solutions: single line "solutions" to complex problems usually end up being more trouble than they're worth in terms of how long it takes to figure it out, code readability and maintainability, friendliness to the next developer (which might be you in a year after you've forgotten all that cleverness), and changing specs or functionalities. There. That's my cranky old guy warning. :) Now I'll go look up this particular thing and see if I can help :) |
|
|
|
Follow Vince's advice and break down the problem one step at a time. Don't peek! But here's a previously posted solution: |
|
I used a closure since it's supposed to be one line. Looks like the code works but not sure if my reasoning in the comments is correct.
|
|
Phew! Inspired to post for the first time after completing this checkpoint. For the first time in the first fortnight, I had to watch every video again, after I'd made a first attempt at the checkpoint, before I got to my solution. Exactly as Vince said, breaking it down step by step helped immeasurably. First, I thought I'd create optional arrays to test the conditions of the checkpoint:
Then I thought I'd practice using them to check one of the conditions - does the optional exist. First I wrote an
I took my ages to get to that function! But I thought it would be easy enough to reverse the order of the function with a
I was fairly sure the second condition would be easy to implement with
When that worked I thought I could nail the one-line function:
Then I thought I could be clever and pass my three arrays into the function in a loop. This caused ten minutes of head scratching until I realised the
I loved this challenge, even though it took me three times longer than any other so far! |
SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.