- 2
Python for beginners: Analyze To Realize (List Functions)
Hi Guys, I'm trying to figure out the correct solution for this problem. The code seems to work fine in an IDE, but I can't get a pass on SoloLearn. :/ The task is to complete the code to remove the smallest and largest elements from the list and output the sum of the remaining numbers. # The given list: data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78] # My code: max = max(data) min = min(data) i_max = data.count(max) i_min = data.count(min) for i in range(i_max): data.remove(max) for i in range(i_min): data.remove(min) total = 0 for i in data: total += i print(total)
4 Respuestas
+ 1
however, it is bad practice to use built-in names for your variables: in your code, you cannot access more than once the min and max built-in functions as you are overiding their value with the result ;P
0
just remove min and max from data (without count, loop...)
then compute sum of remaining elements and output it ^^
max = max(data)
min = min(data)
#i_max = data.count(max)
#i_min = data.count(min)
#for i in range(i_max):
# data.remove(max)
#for i in range(i_min):
# data.remove(min)
data.remove(max)
data.remove(min)
total = 0
for i in data:
total += i
print(total)
0
data = [7, 5, 6.9, 1, 8, 42, 33, 128, 1024, 2, 8, 11, 0.4, 1024, 66, 809, 11, 8.9, 1.1, 3.42, 9, 100, 444, 78]
#your code goes here
data.remove(min(data))
data.remove(max(data))
sum = 0
for nums in range(0, len(data)):
sum = sum + data[nums]
print(sum)
- 3
Thank you @visph!
I tried that before, but I must have made some silly mistake. :)
Take care!