From the course: Level Up: Advanced Python

Create a pairwise offset - Python Tutorial

From the course: Level Up: Advanced Python

Create a pairwise offset

- [Instructor] In this challenge we're going to create a pair-wise offset function that takes in as input a sequence amongst other things. Now a sequence is anything that you can index, slice and determine its length. So examples of sequences are strings, topples, and lists. And I want you to accept any sequence as an argument into the pair-wise offset function. Now this visualization will help you understand how the pair-wise offset function should work. So imagine you made a copy of this sequence, so that one is below the other. You should return an iterable that includes a topple of each corresponding item. Now you should also accept an offset, and the offset will always be greater than or equal to zero. So let's look at an example where the offset is one. Then the second sequence is moved across by one. This also means we need some sort of a fill value, and we use asterisks or stars here as an example. Now because the first topple will have the first value from the sequence and because we have an offset of one. There is no component from the second sequence, but just the fill value. And as we have an offset of one we want to use the same fill value when we have run out of items for the first sequence. Let me show you the same example in code. So we enter the list A, B, C with an offset of one to the pair-wise offset function. And we get the expected list of topples. And just to recap, update the file pairwise_offset.py so that it accepts a sequence. It accepts an offset using zero if nothing is provided. It accepts a fill value using a star or asterisk if nothing is provided. It returns an iterable that includes a topple of each corresponding item. And you can test your solution using pytest. So go ahead and use the pairwise_offset.py as your starting point. So go ahead and use the pairwise_offset.py as your starting point. Pause the video here and I'll show you the solution I came up with. Let's give ourselves a little bit of space. So I'm just going to exit out of here. We can use T from the edit tools module to return a topple of two independent iterators. This is like making the copy of the sequence that we saw in the visualization. We can then use the chain class from the itertools module to combine iterables. In this case we want an offset number of fill values at the front of IT2. We then use zip_longest to combine IT1 and this newly formed iterable. This means that when we have run out of iterables from IT1, we can have fill values substituted in their place. So let's check this passes all the tests. So I'm just going to clear my screen and pytest. Tests and test pairwise_offset.py And from the output in green you can see that this has passed all the tests. Now my solution is just one way of solving this problem. Go ahead and share your solution in the Q&A section. I'm really keen to see how you solved this code challenge.

Contents