# 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)))

1/22/2021 7:53:28 PM

Victor Ortiz20 Answers

New Answernp.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.

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))

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

Yeah it works, I mean just 2 coloumns doesn't mean the numpy array is 2d.The nested arrays define the dimension.

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!

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

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))

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)

#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))

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

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))

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)))

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)

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.

r = int(input()) lst = [float(x) for x in input().split()] import numpy as np arr = np.array(lst) arr = arr.reshape(r,int(len(lst)/r)) print(arr.round(2))

np.reshape (arr, (r.2)) будет переформатироваться в массив с r гребцами и 2 столбцами. Если вывод на самом деле не имел 2 столбцов, тест не провалится.

arr = np.round(arr, 2) (I also tried around) print (np.reshape(arr, (r,2))) ----------------------------------------------------- change to (arr.round(2)) To fit it to any size of the array (r,int(arr.size/r))