Mistake in Python for Beginners course | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Mistake in Python for Beginners course

I believe there is a mistake in the Python for Beginners course, in the Functions chapter, in 38.2 Practice. We had to remove the smallest and largest elements from the list and write out the sum of the remaining numbers. The largest element in the list is 1024, but it occurs twice. When I remove both occurrences, the result is considered invalid (sum is to small). The code shown in the solution removes only one maximum. data.remove(max(data)) data.remove(min(data)) I think the remove function for maximum should be called twice, or what I think is a better solution, it should be called using while. maximum = max(data) minimum = min(data) while maximum in data: data.remove(maximum) while minimum in data: data.remove(minimum) Do you agree with me?

22nd Jun 2022, 9:28 AM
MSdev
4 Answers
+ 6
๐Ÿ may surprise you: 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] print(sum(data)-min(data)-max(data)) # ๐Ÿ™‚ print(sum(sorted(data)[1:-1])) # โ‰๏ธ๐Ÿ˜• print(sum(sorted(data)[1:-1][::-1])) # โ€ผ๏ธ๐Ÿ˜ƒ
22nd Jun 2022, 2:04 PM
Janusz Bujak ๐Ÿ‡ต๐Ÿ‡ฑ ๐Ÿ‡บ๐Ÿ‡ฆ
Janusz Bujak ๐Ÿ‡ต๐Ÿ‡ฑ ๐Ÿ‡บ๐Ÿ‡ฆ - avatar
+ 6
to pass the test case, we only have to remove 1 x the max number in the list and also the min number. Vitaly Sokol => be careful with using set(). there is not only 1024 duplicated, also 8 and 11.
22nd Jun 2022, 5:27 PM
Lothar
Lothar - avatar
+ 3
I would recommend you not to change a list's size while iterating over it โ€“ unless you are 100% sure that you know what you are doing.
22nd Jun 2022, 10:39 AM
Lisa
Lisa - avatar
+ 3
Mariusz Stasiak I read through the code description which states, "You need to remove the outliers". If you were to sort the list, then you would need to remove the first & last items in the list. This is the reason why the other high number remains according to the challenge description
22nd Jun 2022, 12:55 PM
Rik Wittkopp
Rik Wittkopp - avatar