Are you a Python developer brushing up on your skills before an interview? If so, then this tutorial will usher you through a series of Python practice problems meant to simulate common coding test scenarios. After you develop your own solutions, you’ll walk through the Real Python team’s answers so you can optimize your code, impress your interviewer, and land your dream job!
In this tutorial, you’ll learn how to:
- Write code for interview-style problems
- Discuss your solutions during the interview
- Work through frequently overlooked details
- Talk about design decisions and trade-offs
This tutorial is aimed at intermediate Python developers. It assumes a basic knowledge of Python and an ability to solve problems in Python. You can get skeleton code with failing unit tests for each of the problems you’ll see in this tutorial by clicking on the link below:
Download the sample code: Click here to get the code you'll use to work through the Python practice problems in this tutorial.
Each of the problems below shows the file header from this skeleton code describing the problem requirements. So download the code, fire up your favorite editor, and let’s dive into some Python practice problems!
Python Practice Problem 1: Sum of a Range of Integers#
Let’s start with a warm-up question. In the first practice problem, you’ll write code to sum a list of integers. Each practice problem includes a problem description. This description is pulled directly from the skeleton files in the repo to make it easier to remember while you’re working on your solution.
You’ll see a solution section for each problem as well. Most of the discussion will be in a collapsed section below that. Clone that repo if you haven’t already, work out a solution to the following problem, then expand the solution box to review your work.
Problem Description#
Here’s your first problem:
# integersums.py
""" Sum of Integers Up To n
Write a function, add_it_up(), that takes a single integer as input
and returns the sum of the integers from zero to the input parameter.
The function should return 0 if a non-integer is passed in.
"""
Remember to run the unit tests until you get them passing!
Problem Solution#
Here’s some discussion of a couple of possible solutions.
Note: Remember, don’t open the collapsed section below until you’re ready to look at the answer for this Python practice problem!
How did writing the solution go? Ready to look at the answer?
For this problem, you’ll look at a few different solutions. The first of these is not so good:
# integersums.py
def first(n):
num = 1
sum = 0
while num < n + 1:
sum = sum + num
num = num + 1
return sum
In this solution, you manually build a while
loop to run through the numbers 1
through n
. You keep a running sum
and then return it when you’ve finished the loop.
This solution works, but it has two problems:
-
It doesn’t display your knowledge of Python and how the language simplifies tasks like this.
-
It doesn’t meet the error conditions in the problem description. Passing in a string will result in the function throwing an exception when it should just return
0
.
You’ll deal with the error conditions in the final answer below, but first let’s refine the core solution to be a bit more Pythonic.
The first thing to think about is that while
loop. Python has powerful mechanisms for iterating over lists and ranges. Creating your own is usually unnecessary, and that’s certainly the case here. You can replace the while
loop with a loop that iterates over a range()
:
# integersums.py
def better(n):
sum = 0
for num in range(n + 1):
sum += num
return sum
You can see that the for...range()
construct has replaced your while
loop and shortened the code. One thing to note is that range()
goes up to but does not include the number given, so you need to use n + 1
here.
This was a nice step! It removes some of the boilerplate code of looping over a range and makes your intention clearer. But there’s still more you can do here.
Summing a list of integers is another thing Python is good at:
# integersums.py
def even_better(n):
return sum(range(n + 1))
Wow! By using the built-in sum()
, you got this down to one line of code! While code golf generally doesn’t produce the most readable code, in this case you have a win-win: shorter and more readable code.
There’s one problem remaining, however. This code still doesn’t handle the error conditions correctly. To fix that, you can wrap your previous code in a try...except
block:
# integersums.py
def add_it_up(n):
try:
result = sum(range(n + 1))
except TypeError:
result = 0
return result
This solves the problem and handles the error conditions correctly. Way to go!
Occasionally, interviewers will ask this question with a fixed limit, something like “Print the sum of the first nine integers.” When the problem is phrased that way, one correct solution would be print(45)
.
If you give this answer, however, then you should follow up with code that solves the problem step by step. The trick answer is a good place to start your answer, but it’s not a great place to end.
If you’d like to extend this problem, try adding an optional lower limit to add_it_up()
to give it more flexibility!
Python Practice Problem 2: Caesar Cipher#
Read the full article at https://realpython.com/python-practice-problems/ »
[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
from Planet Python
via read more
No comments:
Post a Comment