The array A(10) is given. Replace all the elements standing between the largest and the smallest with the number 100

Question:

I have a problem with python task
for example:
array [1, 5, 2, 3, -1, 3]
new_array [1, 5, 100, 100, -1, 3]

i dont know how do this cuz i new in python

Asked By: POG MEE

||

Answers:

my solution works only for this case when maximal value before minimal but you could easily update this solution.

array = [1, 5, 2, 3, -1, 3] 
c = []
for i,j in enumerate(array):
if i<=(array.index(max(array))) or i>=array.index(min(array)):
    c.append(j)
else:
    c.append(100)

print(c)

Answered By: cryingrock

You could find the indexes of the minimum and maximum values and then assign a corresponding list of 100s to the subscript:

A = [1, 5, 2, 3, -1, 3]

start,end = sorted(map(A.index,(min(A),max(A))))
A[start+1:end] = [100]*len(A[start+1:end])

print(A) # [1, 5, 100, 100, -1, 3]

If you need it to be in a separate variable you can make copy beforehand (ex: new_array = array.copy()) and work on the copy

Answered By: Alain T.
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.