+ 2

Reshape question

I'm working on the module quiz for data science, and although I pass the first two test cases, I'm not passing the hidden test, and am not sure why. Any hints as to what could be the problem? Task Given a list of numbers and the number of rows (r), reshape the list into a 2-dimensional array. Note that r divides the length of the list evenly. Input Format First line: an integer (r) indicating the number of rows of the 2-dimensional array Next line: numbers separated by the space Output Format An numpy 2d array of values rounded to the second decimal. import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) arr = np.round(arr, 2) (I also tried around) print (np.reshape(arr, (r,2)))

22nd Jan 2021, 7:53 PM
Victor Ortiz
Victor Ortiz  - avatar
30 Answers
0
np.reshape(arr, (r,2)) will reshape into array with r rowns and 2 columns. Unless the output actually had 2 columns, the test will fail.
22nd Jan 2021, 8:48 PM
Mateusz Malenta
Mateusz Malenta - avatar
+ 22
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr_ABD = np.array(lst) arr_ABD = arr_ABD.reshape(r,int(len(lst)/r)) print(arr_ABD.round(2))
5th Mar 2021, 5:32 PM
Abdullah Abdelhakeem
Abdullah Abdelhakeem - avatar
+ 10
i think that will work import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) c=len(arr)/r #c for suitable column number new_arr=arr.reshape(int(r),int(c)) print(new_arr.round(2)) please give me feedback
13th Mar 2021, 12:18 AM
Mohamed Ayman
Mohamed Ayman - avatar
+ 7
The question defines r to be the number of rows of a 2d array, which is a helpful start. When using arr.reshape(r, -1), the value -1 acts to fill in the balance of what is left from the size of elements divided by r. Suppose we had 46 elements and r were 23, the code would change -1 into 2. Hence, for whatever value r is, -1 would change in accordance to it. As for the rounding of float, consider round(x, n) where x is the float to be rounded float(x) and n is the number of decimal places to round it to. Hope it helps to shorten the code needed for the solution. All the best! import numpy as np r = int(input()) lst = [round(float(x), 2) for x in input().split()] arr = np.array(lst) print(arr.reshape(r, -1))
28th Apr 2021, 11:58 AM
Derek Lim Jin Xiang
+ 4
Yeah it works, I mean just 2 coloumns doesn't mean the numpy array is 2d.The nested arrays define the dimension.
28th Jan 2021, 5:09 AM
Skanda
Skanda - avatar
+ 3
Did you get the solution that satisfies all the cases? Please do share the solution
23rd Jan 2021, 7:07 AM
Pavan S V
Pavan S V - avatar
+ 2
i dont think i can share the solution, but what i have posted is close! instead of (r,2), change the 2 so it fits with any size array. remmeber that r splits the array evenly!
24th Jan 2021, 3:02 AM
Victor Ortiz
Victor Ortiz  - avatar
+ 2
r divides the list equally is an major hint. If u place a array block(1block) in 3 square braces enclosed, then it is 3d , coloumn never defines the dimension
28th Jan 2021, 5:14 AM
Skanda
Skanda - avatar
+ 1
Tnks to mateuz malenta for the hint
28th Jan 2021, 5:09 AM
Skanda
Skanda - avatar
+ 1
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) newarr = arr.reshape(r,-1).round(2) print(newarr)
8th Jan 2022, 1:55 PM
Mohammad Kakuei Nezhad
Mohammad Kakuei Nezhad - avatar
+ 1
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) arr_ABD = np.array(lst) arr_ABD = arr_ABD.reshape(r,int(len(lst)/r)) print(arr_ABD.round(2))
24th Aug 2022, 4:26 PM
AKU JOJO
AKU JOJO - avatar
0
Oh i thought 2 dimensional meant that it would be 2 columns
22nd Jan 2021, 9:16 PM
Victor Ortiz
Victor Ortiz  - avatar
0
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst).reshape(r, int(len(lst)/r)) print(arr)
6th Apr 2021, 1:34 AM
Glauco da Rocha
Glauco da Rocha - avatar
0
#try this code import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) c=len(arr)/r new_arr=arr.reshape(int(r),int(c)) print(new_arr.round(2))
25th Apr 2021, 7:48 AM
Mardin D. Maloloy-on
Mardin D. Maloloy-on - avatar
0
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) arr=arr.reshape(r,len(lst)//r) print(arr) Consider the eg. Row = 3 User input= 6 Its specified evenly matched so len(lst)//row will return the evenly quotient, so it takes just 6 lines to solve this
30th May 2021, 10:43 AM
Tarun R Jain
Tarun R Jain - avatar
0
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) print(arr.reshape(r,len(lst)//r))
16th Jun 2021, 2:02 PM
Pramod Ugale
Pramod Ugale - avatar
0
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) print(arr.reshape(r,int(len(lst)/r)))
29th Jun 2021, 3:31 PM
Shanmuga Priya
Shanmuga Priya - avatar
0
This is my code it also worked import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) x=arr.reshape(r,int(int(len(lst))/r)) print(x)
25th Jul 2021, 9:08 AM
Abdulrhman Abduallah Elshiekh Idres
Abdulrhman Abduallah Elshiekh Idres - avatar
0
A 2d array in python is different than the language in mathematics I realized. In python a 2d array, is define as nested arrays within an array. An example would be: [ [a,b] , [c,d] ] which is seen in test 1. To access 'a', you would need the to locate the index of the first bracket and then within that first bracket, the index of 'a' with respect to it. Since you only need two indices to locate any particular item, this makes the array 2d. Another example : [[a,b,c], [d,e,f],[g,h,i]] : to access 'h', would index the third bracket, and within the third bracket, the index of 'h', [2][1] . Thus the number 'of columns' is in fact the columns of the nested bracket.
28th Jul 2021, 11:09 PM
MEI HUI TEH
MEI HUI TEH - avatar
0
import numpy as np r = int(input()) lst = [float(x) for x in input().split()] arr = np.array(lst) print(arr.reshape((r,-1)))
31st Oct 2021, 3:28 PM
youssef
youssef - avatar