Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python 3.x You are to write an iterator that will output tuples of two elements from the given lists. Given the class Pair you will

Python 3.x

You are to write an iterator that will output tuples of two elements from the given lists.

Given the class Pair you will need to implement the following methods:

__iter__ this will return an iterator

__next__ returns the next element

__init__ initialises the iterator class or

You may create a separate iterator class after __iter__ is called.

When next() is called with the iterator should return the next pair of elements. If the number of elements are uneven, the position the smaller list's elements are place would be replaced with None.

Once the iterator has finished returning elements, your iterator should raise StopIteration

image text in transcribed

SCAFFOLD:

image text in transcribed

Example 1 k -[0, 9, 8, 7, 6] pair - iter(Pair(1, k)) #Checking if print (next(pair)) #(1, e) print(next(pair)) #(2, 9) Example 2 k [0, 9, 8] pair -iter(Pair(1, k)) print (next(pair)) #(1, 0) print (next(pair)) #(2, 9) print(next(pair)) #(3, 8) print(next(pair)) #(4, None)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Sham Navathe,Ramez Elmasri

5th Edition

B01FGJTE0Q, 978-0805317558

More Books

Students also viewed these Databases questions

Question

LO2 Explain the major laws governing employee compensation.

Answered: 1 week ago