arrow-left arrow-right brightness-2 chevron-left chevron-right facebook-box facebook loader magnify menu-down rss-box star twitter-box twitter white-balance-sunny window-close
Improving My Coding Skills
1 min read

Improving My Coding Skills

Improving My Coding Skills

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. ↩︎

Comments

Become a Stuart Breckenridge member below to join the conversation (it's free!). As a member, you will also receive new posts by email (you can unsubscribe at any time).

Enjoying these posts? Want the newsletter?

Subscribe now for free
Already have an account? Sign in
You've successfully subscribed to Stuart Breckenridge.
Success! Your account is fully activated, you now have access to all content.