Problem 1 Min Max Write a program minmaxpy that reads in flo

Problem 1. (Min Max) Write a program min_max.py that reads in floats (as many as the user enters) from standard input and writes out the minimum and maximum values along with their ranks, ie, their positions (starting at 1) in the input.

min_max.py =

# min_max.py: reads in floats (as many as the user enters) from standard
# input and writes out the minimum and maximum values along with their ranks,
# ie, their positions (starting at 1) in the input.

import stdio

# Smallest and largest floats.
NEG_INFINITY = float(\'-inf\')
POS_INFINITY = float(\'inf\')

# Read floats from standard input into a list a.
a = ...

# Define variables to keep track of the mininum value and its rank and
# the maximum value and its rank.
min_val = ...
min_rank = ...
max_val = ...
max_rank = ...

# Iterate the list a to identify the minimum value and its rank and the
# maximum value and its rank.
for i, v in enumerate(a):
...

# Write the results (min_val, min_rank, max_val, max_rank).
...

Solution

# Smallest and largest floats.
NEG_INFINITY = float(\'-inf\')
POS_INFINITY = float(\'inf\')
a = []
print \"Enter input floats and s to stop\"
while(True):
   # Read floats from standard input into a list a.
   n = raw_input()
   if(n==\'s\'):
       break
   n = float(n)
   a.append(n)
# Define variables to keep track of the mininum value and its rank and
# the maximum value and its rank.
min_val = POS_INFINITY
min_rank = 0
max_val = NEG_INFINITY
max_rank = 0
for i in range(len(a)):
   if(a[i]>max_val):       # check min_val and replace if ii is not min_val
       max_val = a[i]
       max_rank = i
   if(a[i]<min_val):       # check max_val and replace if it is not max_val
       min_val = a[i]
       min_rank = i

print a
print \"MinValue:\",min_val,\"\ MinRank:\",min_rank,\"\ MaxValue:\",max_val,\"\ MaxRank:\",max_rank

#sample output

Problem 1. (Min Max) Write a program min_max.py that reads in floats (as many as the user enters) from standard input and writes out the minimum and maximum val
Problem 1. (Min Max) Write a program min_max.py that reads in floats (as many as the user enters) from standard input and writes out the minimum and maximum val

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site