Are you a Python developer who is preparing for an upcoming interview and want to brush up on your skills? In such case, the next lesson will walk you through a set of Python practise problems that are designed to imitate frequent coding exam situations. When you have developed your own solutions, you will next go over the answers that the Real Python team has provided so that you may optimise your code, wow your interviewer, and get the job of your dreams! You will learn how to do the following at the end of this guide:
for interview-style problems
Discuss your solutions
during the interview
frequently overlooked details
This lesson is intended for Python developers who have some experience. It presumes that you have some familiarity with Python and the ability to work through challenges using Python. By following the link that is provided below, you will be able to get skeleton code along with failed unit tests for each of the challenges that are going to be covered in this tutorial:
Each of the issues that are shown below includes a screenshot of the file header that is generated by the skeleton code and describes the requirements for the challenge. The code is available for download, so open up your preferred text editor and we’ll get started with some Python drills.
Python Practice Problem 1: Sum of a Range of Integers
Let’s get started with a question to get you in the mood. You will need to develop some code to calculate the sum of a list of numbers for the first practise issue. Each practise issue contains a problem description. This description has been taken straight from the skeleton files that are included inside the repository in order to make it simpler for you to keep in mind while you are working on your solution.
You will also see a section with the solutions for each challenge. The majority of the conversation will be included in a section that has been collapsed below that. If you haven’t already done so, clone that repository. After you’ve figured out how to solve the following issue, expand the solution box so that you may examine your work.
Your first challenge is the Summing of the Integers Up to the Number n. ( integersums.py )
Create a method called add it up() that accepts a single integer as its input argument and returns the sum of all the numbers starting from zero and going up to the input value.
If a value that is not an integer is provided into the function, the function should return 0.
Keep in mind that you need to keep running the unit tests until you receive them.
The following is a description of a few different approaches that may be taken. Note: Please keep in mind that you should not expand the portion that has been collapsed below until you are ready to look at the solution for this Python exercise.
Python Practice Problem 2: Caesar Cipher
The following inquiry consists of two parts. You will write some code to create a function that, given some text as input, will calculate a Caesar cypher. You are allowed to utilise any component of the Python standard library to carry out the transformation, as long as it is relevant to this situation. A useful method may be found in the str class that will greatly simplify the completion of this job.
Caesar Cipher is where the issue statement is located at the very beginning of the skeletal source file ( caesar.py )
A Caesar cypher is a kind of straightforward substitution cypher in which each letter of the plain text is changed to a different letter that can be obtained by travelling down the alphabet by n positions. Take, for instance, the assumption that the plain text input is the
In the event where the shift value, n, is equal to 4, the text that is encrypted would be the
You are tasked with writing a function that takes two arguments: a message in plain text and an integer indicating how many letters should be shifted in the cypher. A string that has been encrypted will be returned by the function, with the exception of punctuation and white space, all of which will retain their original forms.
Note: You should presume that the plain text uses only lowercase ASCII characters, with the exception of whitespace and punctuation.
Have in mind that this component of the test focuses mostly on determining how effectively you are able to navigate the standard library. If you discover that you are capable of doing the transformation without using the library, then jot down those thoughts! You’re going to need it.
The following is an answer to the Caesar cypher issue that was presented before. Note: Please keep in mind that you shouldn’t expand the area that has been collapsed below until you are ready to look at the solutions to this Python exercise issue.
Now that you’ve seen how to solve the issue by using the Python standard library, let’s try it out once again, but this time without using it.
Python Practice Problem 3: Caesar Cipher Redux
You’ll be asked to decipher the Caesar cypher once again for the third practise problem, but this time you won’t be allowed to use the.translate command ()
The description of this issue is identical to the explanation of the earlier problem. Before you go into the answer, you may be curious as to why you’re being asked to do the same exercise but this time without the assistance of the.translate() function.
It is a really interesting question. It is not a good idea to rewrite sections of a standard library if your objective is to produce a software that can be maintained and works well in everyday situations. The Python standard library is jam-packed with solutions that are not only functional but also well-tested and quick to implement for a wide variety of issues. A skilled programmer will see the value of this and use it to its maximum potential.
Having said that, the project you are working on is not a work project, nor is it a software that you are developing to fulfil a need. This is a learning exercise, and the question is of the kind that may be asked of a candidate during the interview process. The purpose of these exercises is to determine how the issue might be solved as well as what interesting design trade-offs can be made when attempting to solve the challenge.
Hence, in the interest of education, let’s make an effort to crack the Caesar cypher without using.translate ()
When you are ready to enlarge the area below, you will see that there are two distinct options for you to choose for this particular issue. Note: Please keep in mind that you shouldn’t expand the area that has been collapsed below until you are ready to look at the solutions to this Python exercise issue.
Let’s move on to another cypher now that you’ve shown all three of the possible methods to write the Caesar cypher.
Python Practice Problem 4: Log Parser
In the world of software development, the log parser issue is one that crops up rather regularly. The usual functioning of many systems results in the production of log files; on occasion, you may need to parse these files in order to locate abnormalities or get general information about the system’s performance.
In order to solve this issue, you will need to write a report after parsing a log file that has a certain format. Log Parser ( logparse.py )
Allows a filename to be specified through the command line. The file is a log file that is similar to Linux and comes from the system that you are troubleshooting. Messages describing the condition of the gadget are interspersed throughout the numerous statements that are being shown. They seem to be.
Jul 11 16:11:51:490  dut: Device State: ON
This programme is just concerned with three of the numerous potential values that the device status message might take, and those are ON, OFF, and ERR.
Your application will analyse the provided log file and provide a report that includes the timestamps of any error circumstances along with the amount of time that the device was turned on.
Take note that the unit tests have not been included in the given skeleton code. Due to the fact that the precise structure of the report is up to you, this was left out. Think about your own and write it down while the procedure is going on.
You will find an example in the test.log file, which is included with this package. The following is an effect produced by the solution you will investigate.
$ ./logparse.py test.log Device was on for 7 seconds Timestamps of error events: Jul 11 16:11:54:661 Jul 11 16:11:56:067
You are allowed to create your own format for the output, despite the fact that the Real Python solution will produce that format automatically. The output file should be equal to the example input file.
You will discover a potential answer to the log parser issue in the portion that has been compressed farther down. When you are ready, expand the box and compare what you found to what the other person has come up with! Note: Please keep in mind that you shouldn’t expand the area that has been collapsed below until you are ready to look at the solutions to this Python exercise issue.
The log-parsing solution has been completed in its entirety. Now we’ll move on to the last of our challenges:
Python Practice Problem 5: Sudoku Solver
Your last Python exercise is to complete a sudoku puzzle. Good luck!
Finding a solution to this issue that is both quick and low on memory requirements might prove to be rather difficult. The solution that you will look at was chosen more for its readability than its speed; nevertheless, you are free to make as many improvements to your own solution as you see fit.
The explanation for the solution for the sudoku puzzle is a little bit more detailed than the explanations for the earlier puzzles ( sudokusolve.py )
Provide a software that, given a string in the SDM format, which is specified further below, can identify the solution to the sudoku problem contained in the string and return it. It is expected that the solution will be provided in SDM format, the same as the input.
There will be certain riddles that cannot be solved. In such scenario, the string “Unsolvable” should be returned.
The standard SDM format will now be discussed.
For the sake of this exercise, each SDM string will consist of a series of 81 numbers, with one digit corresponding to each place on the sudoku problem. The places that are not known will have a value of zero, and the numbers that are known will be presented.
Take, for instance, the following series of words as an example.
This string is the representation of the beginning sudoku.
0 0 4 0 0 6 0 7 9 0 0 0 0 0 0 6 0 2 0 5 6 0 9 2 3 0 0 0 7 8 0 6 1 0 3 0 5 0 9 0 0 0 4 0 6 0 2 0 5 4 0 8 9 0 0 0 7 4 1 0 9 2 0 1 0 5 0 0 0 0 0 0 8 4 0 6 0 0 1 0 0
It may take some time to execute the unit tests that are supplied, so please be patient.
Please take note that a rundown of the sudoku puzzle may be found here.
on Wikipedia .
You can see that in addition to coming up with a solution, you’ll need to cope with reading and creating content that adheres to a certain structure.
In the next paragraph, you will find a box with a solution to the sudoku puzzle that contains an in-depth description of how to complete it. Inside the repository, you will find a skeleton file that contains unit tests. Note: Please keep in mind that you shouldn’t expand the area that has been collapsed below until you are ready to look at the solutions to this Python exercise issue.
Your journey through the Python practise problems has come to a successful conclusion. But, if you are interested in further information, you may see an experienced software developer go through an actual interview issue in the video course titled “Create and Test a Python Function: Interview Practice.”