Breaking Down Hard Code
Summary
Everything in programming can be treated as an abstraction of what happens behind the scenes. Treating functions, libraries and programming languages as black boxes with inputs and outputs is the best way to slowly delve into programming and other topics.
Black Box
What is a black box?
Wikipedia indicates it as a device, system, or object which produces useful information without revealing any information about its internal workings. The explanations for its conclusions remain opaque or “black.”
For our purposes, this simply means that we consider functions that we use in terms of their inputs and outputs.
Intuitively, what this means is that we take a larger problem and treat a solution as a black box. We then iteratively write chunks of code which replace functionality within this black box in the process until we’re happy with our end-result or understanding.
Let’s see a simple example as seen below.
Unsure about what functions are? See this post for a quick primer to pure and impure functions.
Say we had a list of numbers and we wanted to find the largest number in this.
arr = [1,2,3,4,5,6]We have a few different methods but let’s solve this by writing an algorithm.
The first way is to just use the max function as defined in Python. This would allow us to get the answer
max(arr) #This returns 6In this specific example, we can then look at max as a black box as seen below
It effectively takes in a list of numbers and returns the one with the maximum value. Let’s go a bit further and try to think of an algorithm that can yield the same result. This algorithm should
Take in a list of unsorted numbers
Look through the list of numbers
Compare each number to another and find the largest number
Return this largest number
Let’s break it down step by step
def findLargestNumber(arr):
#Take a List of unsorted numbers : Done through the arr parameter
# Look through a list of numbers
for i in arr:
print(i)
return max(arr)This in turn gives the following output when applied to arr
1
2
3
4
5
6
>and because we still use max at the end, we still get the final answer of 6 in this array. So, now we’ve successfully achieved step 1 and 2. This leaves us with step 3 - comparing each number with one another and finding the largest number that exists in the list of numbers.
As it turns out, python already has a built in function to compare two numbers, it’s the max function.
max(4,5) # Returns 5
max(5,4) # Returns 4This can be done by the following code.
def findLargestNumber(arr):
#Take a List of unsorted numbers : Done through the arr parameter
# Look through a list of numbers
finalMax = arr[0]
for i in arr:
print("Comparing {} with {}...".format(i,finalMax))
finalMax = max(i, finalMax)
return finalMax
print(findLargestNumber([1, 2, 3, 4, 5, 6]))The intuition here is that we essentially consider our first element as the largest number that we’ve found before we look at the list of elements. We store this value in a variable which we can identify with a name.
Variables provide ways for us to keep track of bits of data that we care about. Think of them like bookmarks to these values.
As we look through our list of numbers, we compare this largest number that we’ve seen so far and then update this variable. This ensures that by the time we’ve reached the end of the array, we know that our finalMax variable stores the largest value we’ve seen thus far, giving us the following comparisons as we run through our array
Comparing 1 with 1...
Comparing 2 with 1...
Comparing 3 with 2...
Comparing 4 with 3...
Comparing 5 with 4...
Comparing 6 with 5...We then verify that our new function does indeed give us the value of 6 when applied to the list of
[1,2,3,4,5,6]and with that we’ve managed to figure out a way to implement our own version of max, which helps us in this case to get the largest value in an array.
Conclusion
I hope this was a useful example on how to understand more complex pieces of code. While this was arguably a simple example, we’ll go into deeper details in the following week about why abstractions are useful in programming when you’re on the job.
Intuition is built brick by brick after all.



