Average of rows attempt failing | Sololearn: Learn to code for FREE!

0

Average of rows attempt failing

I would like to know why numpy arrays are behaving differently when I attempt to np.append in comparison to the normal append https://code.sololearn.com/cTV88JWJ1KGY/?ref=app

8/9/2022 6:06:09 AM

YuGiMob

8 Answers

New Answer

+3

Unlike list.append() method, np.append() is not 'inplace', which means that the original value of the numpy array won't change and you need to assign the new returned value of np.append(arr, e) to a variable. For example: List 🔹 lst = [1, 2, 3] 🔹 lst.append(4) >> [1, 2, 3, 4] Array (numpy) 🔹 arr = np.array([1, 2, 3]) 🔹 np.append(arr, 4) >> [1, 2, 3] You can solve this by assigning the new array to the same variable. 🔹 arr = np.append(arr, 4)

+2

YuGiMob I tried my previous solution and it also didn't work. I guess it is the numpy module that has changed where now it requires each elements to have the same length, and that the sololearn exercise is not updated. Anyway, your code seems to be correct. The input on test 4 has 4 lines (excluding the first line for determining the number of lines), and your code computes for 4 elements of means but the expected output only has three. Yeah, the last element in expected output 6.4 should have been 6 4. You can report this issue on [email protected]

+1

noteve Thanks for the quick answer and thorough explanation

+1

after in my opinion finishing the code, I realized the exercise is bugged...

+1

YuGiMob I also tried it earlier on the average row test itself, the output seems to have a space after the first element of array. For example,: Output 🔸 [1.5 2.5 4] Desired Output 🔸 [1.5 2.5 4] Have you solved it?

+1

YuGiMob I think it is caused by the np.append() method (not a sololearn bug). It makes all elements 'occupy' the same characters. For example if the array has 1.5 and 2.75, since there are 4 characters in 2.75, all elements including 1.5 will also occupy 4 characters (1.5 then a space) [1.5 2.5] What about using the list.append() method first when calculating the values then convert the final list to array before printing. This way the array will behave normally. [1.5 2.5]

+1

I ended up with this: https://code.sololearn.com/cSz4aD5rQjuI/?ref=app It fails on the 4th check but this looks like an error of the check

+1

noteve just wanted to let you know it has been fixed now and formatting is not required anymore