0

Code to input the numbers.

my_array = []
count = int(input("How many numbers you want to add : "))
for i in range(1, count + 1):
    my_array.append(int(input("Enter number {} : ".format(i))))

print("Input Numbers : ")
print(my_array)

Code to get the minimum and maximum element.

Min = access.my_array[0]
Max = access.my_array[0]

for no in access.my_array:
    if no < Min:
        Min = no
    elif no > Max:
        Max = no
print("Minimum number : {}, Maximum number : {}".format(Min, Max))
MaJoR
  • 954
  • 7
  • 20
Andy Eyo
  • 3
  • 4

2 Answers2

1

I am assuming that you need to get the element which is right before the minimum of the list.

For example, [4,5,6,2,3] will get you '6', the element right before the smallest number.

To do that, you first need to find the index of the smallest element. First, you find out the smallest element, and then find it's index. In Python, you can use the min and the index methods. The min method finds out the smallest element in a list, and the index method returns the index of the number in the list.

a = [4, 5, 6, 2, 3]
smallestNum = min(a) #getting the smallest number in the list
indexOfSmallestNum = a.index(smallestNum) # Getting the index of the smallest number

# Now, if the indexOfSmallestNum is 0, there is no element before it. 
#Hence, we return a None. 
#Else, we return a[indexOfSmallestNum-1], the element before the smallest number.

if indexOfSmallestNum == 0:
    return None
else
    return a[indexOfSmallestNum-1]

OR If you just want to find out the second smallest element, then the question is a duplicate, and it can be found here.

MaJoR
  • 954
  • 7
  • 20
0

You can use min(myarray) max(myarray)

If you want the index:

myarray.index(min(myarray))

And if you want the value with index one before min

myarray(myarray.index(min(myarray))-1)

But you should check that min value is not on index 0 then because you could else end up with an exception

Tanja Bayer
  • 711
  • 5
  • 13