|
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 :) |
|
Try breaking it down one step at a time. First, write out your code using multiple lines. Then see what parts you can eliminate and what parts you can combine. Post what you have so far, if you're stuck. |
|
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! |
|
This one hurt my brain a bit, but I think I got there.
|
|
Nick did a nice job whilst sustaining a wee brain bruise:
Two thoughts for you as you progress on your journey. Does this sound like a natural sentence?
Natural language codingConsider changing the function name and its parameters to help your code read like a natural sentence.
There are many variations! My answer is often not the correct one. Pick function names and parameters that help your team read your code effortlessly. Your intentions should be very clear. Second hint: because your function only contains one line of code and your function must return an
Cool stuff! Keep posting questions and your progress. |
|
Here is my take...
|
|
This one was a head-scratcher and stare-at-monitor kind of challenge.
|
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and more!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.