Find a Nth highest or lowest number in a given series of numbers using Python

Rajnilari2015
Posted by Rajnilari2015 under Python category on | Points: 40 | Views : 838
from System import *
from System.Collections.Generic import *

class Program(object):
def Main(args):
lstNumbers = List[Double](1, 2.06, -36.4, -4, 5.90, 61.9, 79.8, 8, 9, 18.0, 23.7)
minNum = 0
maxNum = 0
minNum = lstNumbers[0]
maxNum = lstNumbers[0]
i = 1
while i < lstNumbers.Count:
#logic for obtaining the Minimum Number
if minNum > lstNumbers[i]:
minNum = lstNumbers[i]
#logic for obtaining the Maximum Number
if maxNum < lstNumbers[i]:
maxNum = lstNumbers[i]
i += 1
Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum)
Console.ReadKey(True)

Main = staticmethod(Main)


Explanation: First of all we have collected the data and put into a collection.Then we have assumed the first collection element as the Maximum and Minimum number and put in two variables minNum and maxNum. Then we have looped through from second element till the last element in the collection and compared each element with minNum and maxNum variables. If the values found to be greater/smaller, then those values are assigned.

Comments or Responses

Login to post response