Swift: Playgrounds are Great for LeetCode Challenges!
I’ve often used creating a Command-line Mac OS application to complete LeetCode problems. This means that I’ve been able to use breakpoints in my answers and make sure that I’m thinking in a more strategic way.
But why don’t I use Playgrounds?
The main reason is understanding what setup is required. This article is here to explain how to setup playgrounds, and guide you through the process.
The supporting video is @ this link
Setting up Playgrounds
In order to use testing in Playgrounds, we need to
import XCTest and create a class that conforms to the
XCTestCase protocol, and then run
We need an example in order to see this properly, so in steps LeetCode.
LeetCode has a problem: 1619
So the problem is
1619. Mean of Array After Removing Some Elements.
This problem asks us to remove the smallest 5% and largest 5% of elements, and then calculate the mean.
However, one wrinkle in this is we need to make sure that the accuracy is within 10E-5, maning we need to use the accuracy parameter of
This leads us to the following solution:
I’m actually not that much of a big fan about Swift Playgrounds, but that might well be because I’m caught on a 2-core Mac so it could well be that it is just a little slow on my machine.
If you are interested in LeetCode problems I’ve plenty more content, so I hope that this is just one of my articles that you end up reading!
If you’d like to see see the supporting video is @ this link
I hope this article has really helped you out!
If you’ve any questions, comments or suggestions please hit me up on Twitter