This is the first code post on the new blog!

I’ve been developing apps for iOS since 2009. I have no formal education in software development, but over the last decade I’ve accumulated experience writing apps in Objective-C, Swift, one Windows app in C#, and, recently, building Singapore Transit’s server in JavaScript (which wasn’t pleasant).

Through the small amount of work I’ve contributed to NetNewsWire and following Brent Simmons weblog[1], I’ve been introduced to LeetCode—which is apparently “the best platform to help you enhance your skills, expand your knowledge and prepare for technical interviews”. I had no idea a resource like this existed. It’s huge.

For the moment, I’m not ready to subscribe to Premium, but I’m going to progress with the monthly coding challenges as a way to improve my coding skills. I signed up just in time for the May 8th challenge:

You are given an array coordinates, coordinates[i] = [x,y], where [x,y] represents the coordinate of a point. Check if these points make a straight line in the XY plane.

Here’s my solution, written in Swift:

class Solution {
    func checkStraightLine(_ coordinates: [[Int]]) -> Bool {
       if coordinates.count <= 2 {
            return true
        }
        
        // Calc initial tangent
        let o = coordinates[1][1] - coordinates[0][1]
        let a = coordinates[1][0] - coordinates[0][0]
        let tangent = tan(Double(o)/Double(a))
        
        for (idx, _) in coordinates.enumerated() {
            if idx == coordinates.count - 1 { break }
            
            let o = coordinates[idx+1][1] - coordinates[0][1]
            let a = coordinates[idx+1][0] - coordinates[0][0]
            let t = tan(Double(o)/Double(a))

            if t != tangent {
                return false
            }
        }
        
        return true
    }
} 

  1. Congratulations to Brent for getting a new job. ↩︎