Skip to content
Advertisement

Python – Solve some mathematical problems in a list filled with random elements [closed]

Task: In a list filled with random numbers you need to count:

  1. A sum of negative numbers
  2. A sum of even numbers
  3. A sum of odd numbers
  4. A product of elements with indexes that multiple 3
  5. A product of elements between min and max element
  6. A sum of elements, which located between first and last element

I can’t figure out how to solve last two issues

The code I have so far:

JavaScript

Advertisement

Answer

A product of elements between min and max element

For this task, you can use .sort() method of the “list” object to sort it in-place then use list indexes to exclude the first and last item (min and max because it is sorted) and then use math.prod to calculate the product of rest

JavaScript

A sum of elements, which located between the first and last element

Use list indexes to get the remaining elements and sum() them

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement