Answer to Question #196791 in Algorithms for Adal

Question #196791


  1. Allow the user to input integers representing test scores until they enter -1. Store the values as an ArrayList of Integer values. Do not store the -1. Test score values may range between 0 and 100, inclusive. Your program should ignore any value that is outside of the range, and not add it to the ArrayList.
  2. Output the list of values entered.
  3. Sort the values, lowest to highest and output the sorted list.
  4. Find and output the following:
  5. average score
  6. median score (For an ArrayList with an even number of cells, your program should output the average of the middle two elements. For an ArrayList with an odd number of cells, your program should output the element in the middle.)
  7. range of scores (Find the minimum and maximum values in the list.  The range is calculated as: maximum value minus minimum value.)
  8. Remove the minimum and maximum values. Output the resulting list.
  9. Find and output the following (again):
  10. average score
  11. median score
  12. range of scores
1
Expert's answer
2021-05-23T12:18:59-0400
# 1
L = []
while True:
    x = int(input('Enter a score (-1 to exit): '))
    if x == -1:
        break
    L.append(x)

# 2
print('You have entered:')
for x in L:
    print(x, end=' ')

# 3
L.sort()
print('You have entered:')
for x in L:
    print(x, end=' ')

# 5
avr = sum(L) / len(L)
print(f'Average score is {avr:.2f}')

# 6
n = len(L)
if n == 0:
    median = None
elif n%2 == 1:
    median = L[n//2+1]
else:
    median = (L[n//2-1] + L[n//2])/2
print(f'Median score if {median}')

# 7
range = L[-1] - L[0]
print(f'The range of scores is {range}')

# 8
L = L[1:-1]
print('Minimum and maximum values are removed')

# 10
avr = sum(L) / len(L)
print(f'Average score is {avr:.2f}')

# 11
n = len(L)
if n == 0:
    median = None
elif n%2 == 1:
    median = L[n//2+1]
else:
    median = (L[n//2-1] + L[n//2])/2
print(f'Median score if {median}')

# 12
range = L[-1] - L[0]
print(f'The range of scores is {range}')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS