Skip to main content

Dealing with depression as a software engineer | analyst

Yes there’s been an unfortunate turn of events and my girlfriend and I have split up. The last 6 had been somewhat trying for us and I just didn’t think that she would leave when things were looking up for her. Obviously, I was/am devastated about it. Hence, I had to find something to make me feel better and in this post, I will talk about that. Ahh, not in the mood for suspense, anyway, so I solved a bunch of recursive problems in Swift.

Background

The relationship ended, I was devastated and the world around me was crumbling. Naturally, the first thing I did to get my mind off all those thoughts was to play my Xbox one. However, playing Far Cry 5 for a couple of hours, depressed me more. Nothing wrong with Far Cry 5, I mean, it’s an amazing game with phenomenal graphics and some gameplay tweaks. I miss certain aspects of Far Cry 3, 4 and Primal but I think the game is great. What made me sad is the realisation that Ubisoft knows their players and has been making the same game for years. Yet people like me still buy, Ubisoft clearly knows their players. It’s like they have this generic template for making open-world games that takes a bunch of parameters and generates an open-world.
Anyway, then I remembered what I did almost 6 years ago when that horrible life event that gave me a brain injury. At that time, I solved a bunch of recursive problems in Java from CodingBat.com to confirm (successfully) the presence of my problem solving skills. So I thought hmmm maybe I could try that this time too and solve them all in Swift.
p.s. I will write another post, where I will try to analyse the Ubisoft open-world template

Recursive code

There’s something about writing recursive code that I just find refreshing. It’s a great way to isolate your mind from everything else and just think of solutions. At a time of a breakup, it’s a great pass time activity. The website CodingBat is awesome and it presents a great set of questions (easy to hard) on Recursion. It’s aimed at solving the problems in Java, however the problems are defined as generic enough to be solved in any language. Given my recent experience working with iOS/Swift, I decided to solve some of the easier Recursion problems in Swift. Here’s all my code to the solutions for all the problems. You can find the Xcode playgrounds source code on my Github. Please remember, my purpose in solving these was to get my mind off my ex, that’s all. Therefore the way I have solved these may not be the most efficient way of doing things.
class RecursiveProblems {

    func test(limit: Int) {
        if limit != 0 {
            print(limit)
            test(limit: limit - 1)
        } else {
            return
        }
    }
    func factorial(n: Int) -> Int {
        if n > 1 {
            return n * (factorial(n: n-1))
        }
        return 1
    }
    func bunnyEars(n: Int) -> Int {
        if n == 1 {
            return 2
        }
        return 2 + (bunnyEars(n: n - 1))
    }
    func fibonacci(n: Int) -> Int {
        if n == 0 {
            return 0
        }
        if n == 1 {
            return 1
        }
        return fibonacci(n: n - 1) + fibonacci(n: n-2)
    }
    func bunnyEars2(n: Int) -> Int {
        if n == 1 {
            return 3
        }
        if n%2 == 0 {
            return 3 + bunnyEars2(n: n-1)
        } else {
            return 2 + bunnyEars2(n: n-1)
        }
    }
    func triangle(n: Int) -> Int {
        if n == 0 {
            return 0
        }
        return n + triangle(n: n-1)
    }
    func sumDigits(n: Int) -> Int {
        if n == 0 {
            return 0
        }
        return (n % 10) + sumDigits(n: (n / 10))
    }
    func count7(n: Int) -> Int {
        if n == 0 {
            return 0
        }
        if n % 10 == 7 {
            return 1 + count7(n: (n / 10))
        } else {
            return count7(n: (n / 10))
        }
    }
    func count8(n: Int) -> Int {
        if n == 0 {
            return 0
        }
        let rightDigit = n % 10
        if rightDigit == 8 && (n/10) % 10 == 8 {
            return 2 + count8(n: n/10)
        } else if rightDigit == 8 {
            return 1 + count8(n: n/10)
        } else {
            return count8(n: n/10)
        }
    }
    func powerN(base: Int, n: Int) -> Int {
        if n == 1 {
            return base
        }
        return base * powerN(base: base, n: (n-1))
    }
    func countX(str: String) -> Int {
        if str.isEmpty {
            return 0
        }
        let last = str.last!
        if last == "x" && last.isLowercase {
            return 1 + countX(str: String(str.dropLast()))
        } else {
            return countX(str: String(str.dropLast()))
        }
    }
    func countHi(str: String) -> Int {
        if str.isEmpty || str.count == 1 {
            return 0
        }
        let last = str.last!
        if last == "i" && last.isLowercase {
            let lastDropped = str.dropLast()
            let beforeLast = lastDropped.last!
            if beforeLast == "h" && beforeLast.isLowercase {
                return 1 + countHi(str: String(lastDropped.dropLast()))
            } else {
                return countHi(str: String(lastDropped.dropLast()))
            }
        } else {
            return countHi(str: String(str.dropLast()))
        }
    }
    func changeXY(str: String) -> String {
        if str.isEmpty {
            return ""
        }
        let last = str.last!
        if last.isLowercase && last == "x" {
            return changeXY(str: String(str.dropLast())) + "y"
        } else {
            return changeXY(str: String(str.dropLast())) + String(last)
        }
    }
    func changePi(str: String) -> String {
        if str.isEmpty {
            return ""
        }
        let last = str.last!
        let newStr = String(str.dropLast())
        if last.lowercased() == "i" {
            let secondLast = newStr.last!
            if secondLast.lowercased() == "p" {
                return changePi(str: String(newStr.dropLast())) + "3.14"
            } else {
                return changePi(str: newStr) + String(last)
            }
        } else {
            return changePi(str: newStr) + String(last)
        }
    }
    func noX(str: String) -> String {
        if str.isEmpty {
            return ""
        }
        let last = str.last!.lowercased()
        if last == "x" {
            return noX(str: String(str.dropLast()))
        } else {
            return noX(str: String(str.dropLast())) + last
        }
    }
    func array6(num: [Int], index: Int) -> Bool {
        if num.isEmpty {
            return false
        }
        if index == num.count {
            return false
        }
        if num[index] == 6 {
            return true
        } else {
            return array6(num: num, index: index + 1)
        }
    }
    func array11(nums: [Int], index: Int) -> Int {
        if nums.isEmpty || nums.count == index {
            return 0
        }
        if nums[index] == 11 {
            return 1 + array11(nums: nums, index: index + 1)
        } else {
            return array11(nums: nums, index: index + 1)
        }
    }
    func array220(nums: [Int], index: Int) -> Bool {
        if nums.isEmpty || nums.count < 2 {
            return false
        }
        let val = nums[index]
        let nextVal = nums[index + 1]
        if (val * 10) == nextVal {
            return true
        }
        return array220(nums: nums, index: index+1)
    }
    func allStar(str:String) -> String {
        if str.isEmpty {
            return ""
        }
        let last = String(str.last!)
        return allStar(str: String(str.dropLast())) + "*" + String(last)
    }
    func pairStar(str: String) -> String {
        if str.isEmpty {
            return ""
        }
        if str.count == 1 {
            return str
        }
        let last = str.last!.lowercased()
        let allButLast = String(str.dropLast())
        let secondLast = allButLast.last!.lowercased()
        if last == secondLast {
            return pairStar(str: allButLast) + "*" + last
        } else {
            return pairStar(str: allButLast) + last
        }
    }
    func endX(str: String) -> String {
        if str.isEmpty {
            return ""
        }
        let lastChar = str.last!
        if lastChar.isLowercase && lastChar == "x" {
            //move it to the end
            return endX(str: String(str.dropLast())) + String(lastChar)
        } else {
            return String(lastChar) + endX(str: String(str.dropLast()))
        }
    }
    func countPairs(str: String) -> Int {
        if str.isEmpty || str.count == 2 {
            return 0
        }
        let last = str.last!
        let allButLastCharStr = String(str.dropLast())
        let secLastIdx = allButLastCharStr.index(allButLastCharStr.startIndex, offsetBy: (allButLastCharStr.count - 2))
        let secondLastChar = allButLastCharStr[secLastIdx]
        if secondLastChar == last {
            return 1 + countPairs(str: allButLastCharStr)
        } else {
            return countPairs(str: allButLastCharStr)
        }
    }
    func countAbc(str: String) -> Int {
        if str.isEmpty || str.count < 3 {
            return 0
        }
        let lastChar = str.last!
        let allButLast = String(str.dropLast())
        let secondLastChar = allButLast.last!
        let thirdFromLast = String(allButLast.dropLast()).last!
        if (lastChar == "c" || lastChar == "a")
        && secondLastChar == "b"
        && (thirdFromLast == "a") {
            return 1 + countAbc(str: allButLast)
        } else {
            return countAbc(str: allButLast)
        }
    }
    func count11(str: String) -> Int {
        if str.isEmpty {
            return 0
        }

        let lastChar = str.last!
        let allButLast = String(str.dropLast())
        let scndLastChar = allButLast.last!
        if lastChar == "1" && scndLastChar == "1" {
            return 1 + count11(str: String(allButLast.dropLast()))
        } else {
            return count11(str: allButLast)
        }
    }
    func stringClean(str: String) -> String {
        if str.isEmpty {
            return ""
        }
        if str.count == 1 {
            return str
        }
        let lastChar = str.last!
        let allButLast = String(str.dropLast())
        let secondLastChar = allButLast.last!
        if lastChar == secondLastChar {
            return stringClean(str: String(allButLast.dropLast()) + String(lastChar))
        } else {
            return stringClean(str: allButLast) + String(lastChar)
        }
    }
    func countHi2(str: String) -> Int {
        if str.isEmpty || str.count < 3 {
            return 0
        }
        let lastChar = str.last!
        let allButLast = String(str.dropLast())
        let secondLast = allButLast.last!
        if lastChar.isLowercase,
            lastChar == "i",
            secondLast.isLowercase,
            secondLast == "h" {
            let allButLast2 = String(allButLast.dropLast())
            if allButLast2.last != "x" {
                return 1 + countHi2(str: allButLast)
            } else {
                return countHi2(str: allButLast)
            }
        } else {
            return countHi2(str: allButLast)
        }
    }
}

let rp = RecursiveProblems()
rp.test(limit: 5)

let f = rp.factorial(n: 5)
let be = rp.factorial(n: 3)
let fib = rp.fibonacci(n: 8)
let bn2 = rp.bunnyEars2(n: 4)
let tb = rp.triangle(n: 3)
let sd = rp.sumDigits(n: 59)
let c7 = rp.count7(n: 123 )
let c8 = rp.count8(n: 8818)
let pn = rp.powerN(base: 3, n: 3)
let xCount = rp.countX(str: "xhixX")
let countHi = rp.countHi(str: "xhixhix")
let countXY = rp.changeXY(str: "xxhixx")
let piReplace = rp.changePi(str: "pbPIypi")
let noX = rp.noX(str: "xx")
let arr6 = rp.array6(num: [1,23,4], index: 0)
let arr11 = rp.array11(nums: [1,2,11,4,4,11,11], index: 1)
let arr220 = rp.array220(nums: [1], index: 1)
let allStar = rp.allStar(str: "hello")
let pairStar = rp.pairStar(str: "aaa")
let endX = rp.endX(str: "xa")
let pairCount = rp.countPairs(str: "axada")
let countAbc = rp.countAbc(str: "abba")
let allButLast = rp.count11(str: "11abc1143")
let cleanedStr = rp.stringClean(str: "")
let countHi2 = rp.countHi2(str: "xhixhi")

Conclusion

Solving problems recursively during a break-up, is very effective at getting my mind off stuff. Next up, I plan to finishing solving all those problems in Swift. After that if I can find the time, then maybe solve them all in Typescript or ES6 Javascript.
As usual, if you find any of my posts useful support us by  buying or even trying one of our products and leave us a review on the app store.
‎My Day To-Do - Smart Task List
‎My Day To-Do Lite - Task list
‎Snap! I was there
Developer: Bhuman Soni
Price: $3.99
‎Numbers Game: Calculate Faster
Numbers Game: Calculation Master
‎Simple 'N' Easy Task List
Developer: Bhuman Soni
Price: Free
‎Captain's Personal Log
Developer: Bhuman Soni
Price: $4.99
My Simple Notes
Developer: Bhuman Soni
Price: Free
‎My Simple Notes - Dictate
Developer: Bhuman Soni
Price: Free

Comments

Popular posts from this blog

Upload to AWS S3 from Java API

In this post, you will see code samples for how to upload a file to AWS S3 bucket from a Java Spring Boot app. The code you will see here is from one of my open-source repositories on Github, called document-sharing. Problem Let’s say you are building a document sharing app where you allow your users to upload the file to a public cloud solution. Now, let’s say you are building the API for your app with Spring Boot and you are using AWS S3 as your public cloud solution. How would you do that? This blog post contains the code that can help you achieve that. Read more below,  Upload to AWS S3 bucket from Java Spring Boot app - My Day To-Do (mydaytodo.com)

Addressing app review rejections for auto-renewing subscription in-app purchase (iOS)

The ability to know what the weather is like while planning your day is a feature of  My Day To-Do  Pro and as of the last update it’s also a part of the  Lite version . Unlike the Pro version it’s an auto-renewing subscription based  in-app purchase (IAP)  in the Lite version. What means is that when a user purchases it, the user only pays for the subscription duration after which the user will be automatically charged for the next period. Adding an  auto-renewing  subscription based IAP proved to be somewhat challenging in terms of the app store review i.e. the app update was rejected by the App Review team thrice because of missing information about the IAP. Therefore in this post I will share my experiences and knowledge of adding auto-renewing IAP in hopes to save someone else the time that I had to spend on this problem. In-App purchase This year I started adding IAPs to My Day To-Do Lite which lead to learning about different types of IAP...

Getting started with iOS programming using Swift (Part 1)

I have not been too fond of Objective-C, which was the primary reason for me to stay away from making iOS apps till now. So what changed? Well Apple has done something very interesting recently and that is the introduction of a new programming language i.e. Swift. Swift is awesome, it almost feels like Python, C++ and Objective-C had a baby with some of their good parts in them. So I have been getting to know Swift and it is an awesome language to program in. What I am going to share with this and a series of blog posts are solutions to some problems that i have encounter while i am trying to finish my first iOS app. The one hurdle that I have encountered while getting started on developing an iOS app is that a majority of the solutions for iOS specific problems provide solutions to them using Objective-C. Which is fair, because Swift has not been around for that long. Anyway let us get started with a few basics, A few basics I would highly recommend having a read of this book...