Aaron Ritchey

Productivity Engineer

Blog Post: LeetCode Solution #2651 - Calculate Delayed Arrival Time

Just because you can write your code in O(n^4) doesn't mean you should...

Coding Challenges: Accomplishing vs. Learning

While using Easy-level coding challenges to practice new languages, I'll occasionally find problems better classified as Trivial-level. What I call trivial are problems which don't test your coding skill, such as one-line algebra problems or math trivia questions.

For Example:

You are given a positive integer arrivalTime denoting the arrival time of a train in hours, and another positive integer delayedTime denoting the amount of delay in hours. (The times in this problem are in 24-hours format.)

Return the time when the train will arrive at the station.

class Solution:
    def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int:
        # An algebra problem being called a coding challenge.
        return (arrivalTime + delayedTime) % 24

What Did I Learn?

In the above example, nothing. (X+Y) % 24 isn't very exciting.

So when I find Trivial-level problems, I challenge myself to solve the problem in an unusual way.

  • can I avoid using basic addition?
  • can I avoid using modulo arithmetic?

Don't Try This at Home

This is the solution I submitted to LeetCode. And it passed without giving a Time Limited Exceeded response.

class Solution:
    def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int:
        
        result = 2 * string.ascii_lowercase[1:25] + "SWEARWORD"

        for i in string.ascii_lowercase[1:25]:
            for j in string.ascii_lowercase[1:25]:
                if (i != chr(97+arrivalTime)):
                    continue
                if (j != chr(97+delayedTime)):
                    continue
                
                temp = list(result[arrivalTime:])
                temp[delayedTime] = temp[delayedTime].upper()
                result = "".join(temp) 

        return (reduce(lambda w,f: w+f, [ord(x) for x in result if x in string.ascii_uppercase])) % 2**2**(2*2-2//2)

What's Happening Here?

Assuming you didn't turn to face a wall and weep, (I'm sorry M.P.) this solution illustrates numerous concepts of how Python works.

  • strings can be multiplied by an integer: "a" * 5 = "aaaaa"
  • string.ascii_lowercase 🔗 offers a built-in array of lower-case letters
  • the ASCII value of the lower-case letter a is "97"
  • everyone adds swearwords in their code from time to time
  • the reduce function doesn't get enough love--it was removed from core 🔗 in Python3
  • the lambda variable names w and f were deliberately chosen
  • list comprehension 🔗 is ironically used to make the list incomprehensible
  • I needed the number 256, but some of the keys on my keyboard were broken
  • summing the ordinal value of each letter in SWEARWORD results in 190
  • the ASCII value of the upper-case letter a is "65"

In Conclusion

This LeetCode problem was trivial, yet gave me an opportunity to test my knowledge in various areas of Python.