The result of a google search for ‘itertools’ one day late last week resulted in my Chrome browser caving in to reveal a hidden message. It said ‘You’re speaking our language. Up for a challenge?’ But of course! I’ve since read a few articles about what this is — Business Insider article and Reddit thread — and spent several hours (days?) working through the challenges.
One that I thought was particularly fun (meaning mind-numbingly frustrating) was the string_cleaning challenge. The premise is that a pirate intercepts a message that is sent to you and injects a word repeatedly into the text using an insanely methodical approach. Luckily, the boozed up pirate goes on to tell you his approach so you can write a program to decode the message. The thing that baffled me was there that there is a distinct difference if you removed all occurrences of word at the same time rather than removing them one by one in different orders. Ultimately, I passes all test cases only by removing one by one in various sequences. Here is how I did it
# Test case data chunk = "lololololo" word = 'lol' def answer(chunk, word): # Declare empty list for results outputs =  # Find indices in string where word occurs instance = [i for i in range(len(chunk)) if chunk.startswith(word, i)] # Loop through those instance for i in instance: # Take out occurrence of word c = chunk[0:i] + chunk[i+len(word):len(chunk)] # One by one, take out remaining occurrences of word while c.find(word) != -1: c = c.replace(word, '', 1) # Append watered down string to result list outputs.append(c) # Sort the list by lexicographically earliest string outputs.sort(key=lambda item: (len(item), item)) # Return 1st element in result list return outputs
Zombit_infection was a challenge I found rather difficult. Here, one of the rabbits gets infected with the mad scientist’s disease and it is your job to simulate how the infection will spread through a population given the resistance level of all the rabbits in the population. It was an interesting problem to think and read through since it applies not only to the spread of disease, but any type of contagion situation (financial contagion, viral marketing, etc.). After several attempts of not using a recursive function, I arrived with this solution…
# Test case information population = [[6, 7, 2, 7, 6], [6, 3, 1, 4, 7], [0, 2, 4, 1, 10], [8, 1, 1, 4, 9], [8, 7, 4, 9, 9]] x = 2 y = 1 strength = 5 def answer(population, x, y, strength): if population[y][x] <= strength: # If patient zero is susceptible to the disease, represent with -1 population[y][x] = -1 # Call spread function to initiate spread of infection spread(population, x, y, strength) return population def spread(population, x, y, strength): ''' Spread will will recursively search through matrix and continue or cease depending upon rabbit resistance level and infection strength ''' # Look at the rabbit to left if x != 0 and population[y][x - 1] <= strength and population[y][x - 1] != -1: population[y][x - 1] = -1 spread(population, x - 1, y, strength) # Look at the rabbit to the right if len(population) > x + 1 and population[y][x + 1] <= strength and population[y][x + 1] != -1: population[y][x + 1] = -1 spread(population, x + 1, y, strength) # Look at rabbit above if y != 0 and population[y - 1][x] <= strength and population[y - 1][x] != -1: population[y - 1][x] = -1 spread(population, x, y - 1, strength) # Look at the rabbit below if len(population) > y + 1 and population[y + 1][x] <= strength and population[y + 1][x] != -1: population[y + 1][x] = -1 spread(population, x, y + 1, strength)
The experience was great for two reasons. One, it made me better at thinking through and writing code. Two, it feels good to be part of a small community who even had the chance to see that screen collapse and get the initial invitation to play, doesn’t it?