Member-only story
Swift: Playgrounds are Great for LeetCode Challenges!
Yeah they are!
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 className.defaultTestSuite.run()
.
We need an example in order to see this properly, so in steps LeetCode.
The problem
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…