0
Why my code doesn't past all test case? It only pases 2 from 5 test Cases. I have verify that my code is exactly same with other
Why they can passes all sololearn 5 test case, but my code doesn't.? Thank you class:) https://code.sololearn.com/cqQ10YTt9zBU/?ref=app
19 Answers
+ 3
I think the problem is with the arguments in the reshape() function.
Most probably , tests 1 and 2 have outputs with 2 rows and 2 columns.
When the code is tested for different values for rows(r), an error occurs.
Change reshape(r,2) to reshape(r,int(len(lst)/r)) so that the number of columns changes according to the value of r.
+ 1
Oliver Pasaribu
Listen to Kalana Kalana Ekanayake 's suggestion.
Don't simply use(r,2) in the reshape. That is only for 2,2 matrix.
The second value should be computed as
r2 = int(len(lst)/r))
+ 1
Oliver Pasaribu
From the example you have given,
lst = [1,2,3,4,...,15]
If it is to be divided into 5 rows, there should be 3 columns (15รท5).
In the reshape function of your code, the number of columns is always 2. So when r = 5 for the list above, it is trying to divide it into 5 rows and 2 columns, which is impossible (because there are 15 elements).
What me and Bob_Li trying to say is to change the code so that the number of columns changes according to the number of rows given.
+ 1
Use if clause to check if it is possible to arrange into N rows.
N = int(input())
list.append(element)
if len(list)%N == 0, we can do. else, list.appemd(0) insert at back so that the last element is 0.
+ 1
Oliver Pasaribu
you do realize you've just made a simple solution needlessly complicated?๐
+ 1
There is no need for the append.
There is no even need for the if-else.
just do this.
import numpy as np
r = int(input())
lst = [float(x) for x in input().split()]
print(np.array(lst).reshape(r,len(lst)//r))
0
What is the question your trying to answer?
0
This:
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
0
I got exactly same result and almost identical steps to solve this problem. Moreover, yg I got same expected output. But why my code only pass test case number#1 and number#2 only?
0
I don't really understand what you say. Let me think about this problem.
1st reading:
r = int(input)
r represents number of row for new reshape array 2D.
2nd reading:
Read 4 float values as a list. For example let 4 float values to read are : 1.0, 2.0, 3.0 and 4.0. we get 1D ordinary list lst
lst = [1.0, 2.0, 3.0, 4.0]
Then we reshape 1D list or array after np.array(lst) to new shape dimension. In this case can be reshape to (2,2) only. as in example, 1D array 90 elements can be reshape d as (2,45), (3,30), (6,15), (15,6), (30, 3), (45,2) and (90, 1). Am I right?
0
Oliver Pasaribu
I think I understand your confusion. It comes from the interpretation of
"divides the list EVENLY"
it is not the same as
"divides the list IN TWO"
but it means
"divides the list into a number EVEN LENGTH segments"
example 12 can be evenly split into 2,3,4 6 and 12 parts
It is still a 2D array.
0
Oliver Pasaribu
#example lst
lst = [1,2,3,4,5,6,7,8,9,10,11,12]
arr0 = np.array(lst)
print(arr0)
arr0_len = len(arr0)
# r is a number that divides the length of the list EVENLY (not INTO TWO)
r = 2
arr2 = arr0.reshape(r, arr0_len//r)
print(arr2)
r = 3
arr2 = arr0.reshape(r, arr0_len//r)
print(arr2)
r = 4
arr2 = arr0.reshape(r, arr0_len//r)
print(arr2)
r = 6
arr2 = arr0.reshape(r, arr0_len//r)
print(arr2)
r = 12
arr2 = arr0.reshape(r, arr0_len//r)
print(arr2)
0
Bob_Li :
2d np array comes from reshaping an 1D array(think it like a row-vector/matrices, or column vector matrices which N elements arranged horisontally or vertically. If N%2=0, we can reshape it to 2D array m*n. If N = 4, then each rows contain 2 elements and constructs new matrices m*n = 2*2. 2D means it has row and column.if N= 6, each row of 3 rows has equal number of element n=3, and so on.
0
Test case #1 and #2 both divide into 2 rows which element evenly or equally distributed between row contains of N/n rows. If N%2!=0, we still can make it evenly distributed between rows by adding 0 trailer to fill the empty space of column in specific row. Recall that int [] arr = new int[5] canbe written as arr[5] = [1,2,3] which has same meaning to arr[5] = [1,2,3,0,0]. What do you think Bob Li?
0
Oliver Pasaribu
you are stuck in 2.
The hidden cases might not have evenly divisible list length.
"reshape evenly" does not mean "divide into into two".
as long as the sub-lists are equal in length, it is reshaped evenly.
(r,2) cannot always work.
The code coach test cases does not limit itself with only r=2. Although the first few cases may be 2, it can be actually be any integer. The number of elements in the list just have to be a multiple of that r.
consider the following input case
r = 5
lst = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
or
r =3
lst = [1,2,3,4,5,6,7,8,9]
you can't reshape that to
(5,2)
or
(3,2)
you have to do
(5,3) (3,5)
or
(3,3)
to reshape it EVENLY.
0
Let us try.
lst = [1,2,3,4,5,6,7,8,9,10,11,...15]
if r = 5 it means we intends to reshape it from 1 row to 5 row. Then we need to test if it is possible to do by check if len(lst) % r = = 0; 15%5 ==0? The answer yes. Then we can rearrange 1D list lst to 2D list lst2 = np.arrange(lst(5,3)), that result:
lst2 = [[1,2,3], [4,5,6],[7,8,9],[10,11,12], [13,14,15]]
For r = 2, then 15 is not divisible by 2. It must be 14, 16, 18, etc. We could not eliminate defined element, but we can change vector column 15 element to vector column or row consist of 16 elements by adding 1 trailer zero as the 16th element or Li or arr. This is often use in solving array, or matrices problem mathematically I think as is linear algebra, or linear-program or eventually operations research.
0
Oliver Pasaribu
your analysis is only considering even numbers.
some length is not evenly divisible, that is why the second parameter in your reshape cannot be fixed to 2.
Sure we can pad the list so that it is evenly divisible.
But in the test cases, we cannot do that.
we can only change the reshape numbers.
0
Bob_Li :
if len(arr)%N==0:
arr_array = np.reshape(arr(N, len(arr)/N))
else:
arr.append(0)
arr_array = np.reshape(arr(N, len(arr)/N)).
0
I don't understand. It is not neddlessly. This is basic how the logic of problem solving goes. It also show the blackbox of python numerical library like a c++ stl etc.