• 1
    Input and Output Data
    • Tasks
  • 2
    Conditions
    • Tasks
  • 3
    For Loop
    • Tasks
  • 4
    Strings
    • Tasks
  • 5
    While Loop
    • Tasks
  • 6
    Lists
    • Tasks
  • 7
    Two-Dimensional Arrays
    • Tasks
  • 8
    Dictionaries
    • Tasks
  • 9
    Sets
    • Tasks
  • 10
    Functions and Recursion
    • Tasks
  • к

Занятие 6. Lists

Difficulty level:

Task«Sliding medium»

Imagine that you are working in a research center studying demographic data. You have a list with people's age, and you need to analyze the trends of life expectancy using the sliding average method.

Input format

list of people divided by spaces (integers). Justify; "> the size of the sliding window (integer)

Output format

a list of average ages calculated for each sliding window. Each average value should be rounded to two signs after decimal

Example

Input

65 70 75 80 85 90 95
3

Output

70.00 75.00 80.00 85.00 90.00

Hint

There will be no clue here, decide for yourself!

main.py
Test 1
Test 2
Test 3
Test 4
Test 5
Test 6
Test 7
Test 8
Test 9
Test 10
Developer’s solution
# Получаем список возрастов на вход и преобразуем его в список чисел
ages = list(map(int, input().split()))
# Получаем размер окна на вход
window_size = int(input())

# Вычисляем средние значения в скользящем окне
moving_averages = []
for i in range(len(ages) - window_size + 1):
    window = ages[i:i+window_size] # Выделяем текущее окно
    average = sum(window) / window_size # Вычисляем среднее значение в окне
    moving_averages.append(round(average, 1)) # Добавляем среднее в список

# Выводим средние значения, разделенные пробелами
print(*moving_averages)

🎉 Congratulations! 🎉

You did an excellent job with the task! It was a challenging problem, but you found the correct solution. You are one step closer to mastering programming! Keep up the good work, because every stage you pass makes you even stronger.

AD

Advertisement

red-snake blue-snake green-snake

Running your code...

Помощник ИИ

Привет! Я твой помощник по программированию. Задавай любые вопросы по Python, я могу рассказать о функциях, методах, обьяснить то, что тебе не понятно, а так же о текущей задаче!