Today was the third day of my schedule, with time split between Python fundamentals and data structures & algorithms.
Python Language Fundamentals
I continued working through the Python track on Exercism, completing the exercises on conditionals, collections, and loops. The exercises themselves were trivial, but I tripped over a few Python idiosyncrasies along the way.
Ranges Exclude Their End
In the Black Jack exercise, I used range(9, 11) to represent the hand values where a player should double down. I expected this to be the equivalent of [9, 10, 11], but a test kept failing when the hand value was 11.
It turns out that Python ranges exclude the end value, so range(9, 11) only covers 9 and 10. Switching to an explicit list fixed the test. This is in contrast to what I’m used to in Elixir, where 9..11 includes both ends.
insert Doesn’t Return the List
Chaitana’s Colossal Coaster involved inserting an element into an existing list at a specific index. An easy enough task, but I tried to do it all in one line with return queue.insert(...), and the function kept returning None.
insert modifies the list in place and doesn’t return a value, so the insert and the return had to be split into two lines:
queue.insert(index, person_name)
return queue
Coming from Elixir, where data is immutable and functions return a new value, this feels so strange. These basic exercises are helping to reinforce the fundamental differences I need to become accustomed to.
Data Structures & Algorithms
I continued through the Arrays section of the NeetCode 150.
Top K Frequent Elements
Top K Frequent Elements gives you a list of integers and a number k, and asks for the k most frequent values in the list.
I’ve done this exercise a couple of times in the past, and the logic behind the solution stuck with me, so it came together quickly apart from a couple of syntax issues. I used a bucket sort, where each bucket’s index represents a frequency, and had to look up the list comprehension syntax to create the buckets:
buckets = [[] for _ in range(len(nums) + 1)]
I also stumbled on iterating through the key/value pairs of a dict (or defaultdict). I assumed it would be enumerate(my_dict), but that yields an index and a key. The right approach is my_dict.items().
String Encode and Decode
String Encode and Decode asks you to design an algorithm that encodes a list of strings into a single string, and decodes that string back into the original list.
This was my first time solving the problem. I relied on the hints to get a sense of what kind of delimiter to use. The name of the problem and its medium difficulty suggested that simply joining the strings and splitting on a common delimiter wasn’t what was being tested, since any of the strings could contain that delimiter.
I solved it with relatively few problems. There were still some Python syntax issues around substrings, but they were resolved more quickly than the syntax issues I ran into on previous days.
Products of Array Except Self
Products of Array Except Self gives you a list of integers and asks for a new list where each element is the product of every number in the original list except the one at that position.
This was also my first time solving this one. I got to a solution by generating prefix and postfix lists, holding the products of all of the elements before and after each position, then multiplying them together. It passed, but my runtime needs improvement. I want to revisit the problem at a later date after watching some of the YouTube walkthroughs to see where I can make it more efficient.
Takeaways
The mistakes I made today weren’t about logic, but about assumptions carried over from Elixir. Ranges that include their end and functions that return a new value are second nature to me, and Python doesn’t always work that way. Catching those differences now, in low-stakes exercises, is far better than discovering them in the middle of an interview.
The syntax friction from the first couple of days is also starting to fade. I’m still reaching for Google, but less often, and the fixes come quicker when I do.
