Chapter 33 More with Data Structures

33.1 Simple Exercises with Lists

Exercise 33.1-1 Creating a List that Contains the Average Values of its Neighboring Elements

Write a Python program that lets the user enter 100 positive numerical values into a list. Then, the program must create a new list of 98 elements. This new list must contain, in each position the average value of the three elements that exist in the current and the next two positions of the user-provided list.

Solution

Let's try to understand this exercise through an example using 10 elements.

Image

List newArr is the new list that is created. In list newArr, the element at position 0 is ...

Get Python and Algorithmic Thinking for the Complete Beginner now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.