Got abs()?
Another Python problem
9 min read
Read TimeMay 15, 2024
Published OnGiven that this isn’t a Fitness & Health blog, the abs in the title does not refer to a six pack, but it refers to Python’s built-in function abs(). It’s just an “attention grabbing” strategy, alias clickbait. I know, it’s lame, but what’s not lame is one of the problems from the Python course, which is:
Given an integer n, return True if n is within 10 of either 100 or 200.
Alright, let's break it down.
If we only have to check if n is within 10 of only 100, then I would write it as follows:
However, since we need to check for 2 numbers, we can not use this logic.
Btw, the problem already mentions 100 or 200, and more often than not, parts of the solution are already in the problem. So in this case, we know we'll probably have to use the Python or operator, but this also means that we could probably reuse the same statement for both numbers, which makes sense.
Sometimes it's clear which Python function(s) we can use, but other times it may be less clear. That's why it's important to really understand the problem, but also to make associations. It's somewhat similar to when you're learning a new language, and you recognize two different words that have the same or similar meaning. For example, to eat and to consume.
In the problem above, we can see the word within, which unlike or, is not valid Python syntax or vocabulary, but range() is, and range and within do have some similarities. So I already know that I can probably use range() to solve this problem.
With this in mind, we can write the following:
There is a new keyword in the statement above, which is in, but this is also a keyword in Python, which as the name applies will check if x, or in this case, if n is in something else, and in the problem above that something else is Python's built-in function range().
When you think about it, Python is actually not that difficult to learn. Coming back to the languages analogy, when you learn a new language, and while you may not learn all the words there are in a language, and I would make the case that the majority of people don't know all the words in their native language, but all languages have at least more than 1,000 words in their dictionary, with the sole exception of Toki Pona, which according to Wikipedia only has 120 words.
My native language is Dutch i.e. the language I grew up with, and according to that same Wikipedia article, we have 400,000 words in our dictionary. If your native language is Dutch, I'm willing to bet that you don't know that many words. Heck, you may not even know that amount of words if you combine all the languages you know, unless you're a polyglot.
In fact, according to a 2013 article from the Economist most adult native-test takers have a vocabulary range of about 20,000 - 35,000 words, and this is based on the English language, which is in the top 5 languages with the most total words in their dictionaries. So in retrospect, I don’t even think most polyglots know 400,000 words.
Speaking of which, before I hit a 400,000 word article, coming back to Python, Python only has 35 keywords, also known as reserved words, like or, and, and in, and 68 built-in functions like range(), which brings the total to a little over just 100 words. To put that in perspective, even Toki Pona has more words with a total of 120.
Ok, I got a little sidetracked there, but coming back to the problem, we don't even have to explicitly return True or False, since the output of "n in" is a boolean, so we could simply return that. Meaning we could rewrite or refactor our code as follows:
All we now need to do is change the values to check if n is within 200, and include the or operator, so we can all write it as a single statement like so:
Now at this point, if you've made it this far, you may think, but what does this have to do with abs()? Well, the code above is how I wrote the solution, but the solution to this problem from the solution uses the abs() function to solve the problem.
Before I show the solution with abs(), let's first look at what this function does.
Abs() is short for absolute, and it returns the absolute value of whatever value you give inside the parentheses. Assuming it's an integer, float, or even a complex number. Now truth be told, I didn't know what an absolute number was before I googled it, but I now know that an absolute number is always positive, unless we're talking about an absolute number of 25 in your bank account. So the output of abs(-25) is 25.
Alright, so how does this translate to the solution to the above problem. Well, we know that we need to check if n is within 10 of x, above or below, but the range is within 10, or in other words, the difference must be less than 10. Or less or equal than 10, depending on how you would define within 10, but the solution uses less or equal than.
But with this, we could simply subtract n from 100, and check if the result is less or equal than 10, right? Now of course if n is more than 100, then the output will be a negative number, but absolutely speaking (did you see what I did there 😏) we can check if n is within 10 of 100. We just have to put the output of 100 - n or x - n inside of the abs() function like so:
And this will yield the same output of either True or False as using range().
And the same principle applies here too to check if n is within 10 of 200, which is that you just use the or operator and change 100 to 200.
There you have it, the abs() function in my own words and a use-case.
Can you think of “another” real-world use case for abs()? Don’t let me know in the comments below, since I have yet to build the comments section.
Bye for now!