Consecutive Reading Pairs

Medium ⏱ 12 min 47% acceptance ★★★★☆ 4.4
Sensor readings need to be compared pairwise. Write adjacent_pairs(readings) returning a list of (readings[i], readings[i+1]) tuples for every consecutive pair, using a comprehension over indices.

Examples

Example 1
Input
adjacent_pairs([3, 7, 1])
Output
[(3, 7), (7, 1)]
Explanation

Two overlapping windows of size 2.

Constraints

  • A list with fewer than 2 items yields [].
  • Use a comprehension.

Topics

List Comprehensionssliding window

Companies

UberCiscoIntel

Hints

Hint 1

Iterate i over range(len(readings) - 1).

Hint 2

Each tuple pairs index i with i+1.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.