Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write two versions of essentially the same function, call them sum_pairs() and sum_pairs1(). It returns a list containing, for each adjacent pair (n0, n1) in

Write two versions of essentially the same function, call them sum_pairs() and sum_pairs1(). It returns a list containing, for each adjacent pair (n0, n1) in the list passed in, the value of n0 + 2n1. For the first version (sum_pairs()), define within the scope of sum_pairs() a function add_double_2nd() that, passed a 2-tuple t of integers, returns t[0] + 2*t[1]. sum_pairs() itself returns the result of mapping this function over the list of adjacent pairs in the list passed in. The second version, sum_pairs1(), does the same but uses a lambda expression instead of a local function. To get the zipped list of adjacent pairs of the list passed in (which you can map or iterate over), use the pairwise() function defined below (and available on the assignment page), where the parameter is any iterable (such as a sequence). It uses function tee() from the itertools library. (Later in the semester, we shall cover iterators, Python functionals that, e.g., provide elements one at a time (see next() in the listing below) in a map application or a for loop.) import itertools def pairwise(iterable): a, b = itertools.tee(iterable) next(b, None) return zip(a, b) COMP 651

# -*- coding: utf-8 -*-

import itertools def pairwise(iterable): """ Return the zipped list of adjacent pairs of iterable This returns a zipped list with, for each pair of items i1, i2 at adjacent positions in iterable,the tuple (i1, i2). Note that one may iterate over the zipped list. Args: iterable: An iterable whose adjacency pairs are included Returns: A zipped list of adjacent pairs of iterable """ # pairwise('ABCDEFG') --> AB BC CD DE EF FG a, b = itertools.tee(iterable) next(b, None) return zip(a, b)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions