0

Find largest and smallest element in given array

plz check following code.Getting largest element correctly, but something is wrong with smallest element. https://code.sololearn.com/c4YZs0BSItxP/?ref=app Plz help to solve this.

12th Mar 2018, 2:26 PM
Manorama
Manorama - avatar
6 Respuestas
+ 2
def largestSmallest(A): max=A[0] min=A[0] for i in A: if i >max: max=i if i < min: min=i print ("largest element is",max) print ("smallest element is",min) A=[45,12,3,5,100] largestSmallest(A)
12th Mar 2018, 2:35 PM
Raziul Islam
Raziul Islam - avatar
0
Why don't use max() and min() functions ?
12th Mar 2018, 2:29 PM
Raziul Islam
Raziul Islam - avatar
0
don't want to use library functions.
12th Mar 2018, 2:31 PM
Manorama
Manorama - avatar
0
If you set min=0 it will be hard to change it 😉 If you don't use lists then set min as 100 and change elif i>min: to elif i<min:
12th Mar 2018, 2:38 PM
U-Boot
U-Boot - avatar
0
@Raziul Islam thank you ... it works...
12th Mar 2018, 2:43 PM
Manorama
Manorama - avatar
0
welcome
12th Mar 2018, 2:46 PM
Raziul Islam
Raziul Islam - avatar