Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1. (Min Max) Write a program min_max.py that reads in floats (as many as

ID: 3924557 • Letter: P

Question

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).
...

Explanation / Answer

# 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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote