This is the third post of the Software Engineer Interviews series. I have brought a challenge I did a few years ago, and actually got the position - other tech interviews were involved, such as a past experience screening.
If you've missed the previous posts on this series, you can find them here.
This challenge was also a take-home coding task, where I had to develop a CLI program that would query the OEIS (On-Line Encyclopedia of Integer Sequences) and return the total number of results, and the name of the first five sequences returned by the query.
Thankfully, the OEIS query system includes a JSON output format, so you can get the results by calling the url and passing the sequence as a query string.
Example input and output:
oeis 1 1 2 3 5 7
Found 1096 results. Showing first five: 1. The prime numbers. 2. a(n) is the number of partitions of n (the partition numbers). 3. Prime numbers at the beginning of the 20th century (today 1 is no longer regarded as a prime). 4. Palindromic primes: prime numbers whose decimal expansion is a palindrome. 5. a(n) = floor(3^n / 2^n).
Note: this result is outdated!
The plan to solve this challenge is the following:
Since this is a coding challenge, I will be using Poetry to help me create the structure of the project, and to facilitate anyone running it. You can check how to install and use Poetry on their website.
I’ll start by creating the package with:
poetry new oeis
This will create a folder called oeis, which will contain the Poetry’s configuration file, a test folder, and a folder also called oeis, which will be the root of our project.
I will also add an optional package, called Click, which helps building CLI tools. This is not required, and can be replaced by other native tools from Python, although less elegant.
Inside the project’s folder, run:
poetry add click
This will add click as a dependency to our project.
Now we can move to our entrypoint file. If you open the folder oeis/oeis, you will see there’s already an __init__.py file. Let’s update it to import Click, and a main function to be called with the command:
# oeis/oeis/__init__.py import click @click.command() def oeis(): pass if __name__ == "__main__": oeis()
This is the starting point to our CLI. See the @click.command? This is a wrapper from click, which will help us define oeis as a command.
Now, remember we need to receive the sequence of numbers, separated by a space? We need to add this as an argument. Click has an option for that:
oeis 1 1 2 3 5 7
This will add an argument called sequence, and the nargs=-1 option tells click it will be separated by spaces. I added a print so we can test the argument is being passed correctly.
To tell Poetry that we have a command, we need to open pyproject.toml and add the following lines:
Found 1096 results. Showing first five: 1. The prime numbers. 2. a(n) is the number of partitions of n (the partition numbers). 3. Prime numbers at the beginning of the 20th century (today 1 is no longer regarded as a prime). 4. Palindromic primes: prime numbers whose decimal expansion is a palindrome. 5. a(n) = floor(3^n / 2^n).
This is adding a script called oeis, which calls the oeis function on the oeis module. Now, we run:
poetry new oeis
which will let us call the script. Let’s try it:
poetry add click
Perfect, we have the command and the arguments being parsed as we expected! Let's move on to the client. Under the oeis/oeis folder, create a folder called clients, a file called __init__.py and a file called oeis_client.py.
If we expected to have other clients in this project, we could develop a base client class, but since we will only have this single one, this could be considered over-engineering. In the OEIS client class, we should have a base URL, which is the URL without the paths, and that we will use to query it:
# oeis/oeis/__init__.py import click @click.command() def oeis(): pass if __name__ == "__main__": oeis()
As you can see, we are importing the requests package. We need to add it to Poetry before we can use it:
# oeis/oeis/__init__.py import click @click.command() @click.argument("sequence", nargs=-1) def oeis(sequence: tuple[str]): print(sequence) if __name__ == "__main__": oeis()
Now, the client has a base url which does not change. Let's dive into the other methods:
We also need to update our main file, to call this method:
# oeis/pyproject.toml [tool.poetry.scripts] oeis = "oeis:oeis"
Here we are now building a client instance, outside the method, so it doesn’t create an instance every time the command is called, and calling it inside the command.
Running this results in a very, very long response, since the OEIS has thousands of entries. As we only need to know the total size and the top five entries, we can do the following:
poetry install
Running this is already way better than before. We now print the total size, and the top five (if they exist) entries.
But we also don’t need all of that. Let's build a formatter to correctly format our output. Create a folder called formatters, which will have a __init__.py file and a oeis_formatter.py file.
oeis 1 1 2 3 5 7
This file is basically formatting the top five results into what we want for the output. Let’s use it in our main file:
Found 1096 results. Showing first five: 1. The prime numbers. 2. a(n) is the number of partitions of n (the partition numbers). 3. Prime numbers at the beginning of the 20th century (today 1 is no longer regarded as a prime). 4. Palindromic primes: prime numbers whose decimal expansion is a palindrome. 5. a(n) = floor(3^n / 2^n).
If you run this code, you will get this now:
poetry new oeis
It is now returning with the format we expect, but notice that it says it found 10 results. This is wrong, if you search on the OEIS website you will see there are way more results. Unfortunately, there was an update to OEIS API and the result no longer returns a count with the number of results. This count still shows up on the text formatted output, though. We can use it to know how many results there are.
To do this, we can change the URL to use the fmt=text, and a regex to find the value we want. Let’s update the client code to fetch the text data, and the formatter to use this data so we can output it.
poetry add click
As you can see, we added two new methods:
# oeis/oeis/__init__.py import click @click.command() def oeis(): pass if __name__ == "__main__": oeis()
In this file, we only added a new param for the method, and used it instead of the length of the query result.
# oeis/oeis/__init__.py import click @click.command() @click.argument("sequence", nargs=-1) def oeis(sequence: tuple[str]): print(sequence) if __name__ == "__main__": oeis()
Here we are just calling the new method on the client, and passing the information to the formatter. Running it again results in the output we were expecting:
# oeis/pyproject.toml [tool.poetry.scripts] oeis = "oeis:oeis"
The code is basically ready. But for a real challenge, remember to use Git when possible, do small commits, and of course, add unit tests, code formatting libs, type checkers, and whatever else you feel you will need.
Good luck!
The above is the detailed content of Software Engineer Interviews - #EIS CLI. For more information, please follow other related articles on the PHP Chinese website!