#P9891. List Reshape
List Reshape
List Reshape
Problem Description
ProtectEMmm is learning to use NumPy.
She is curious about reshape
function, and wants to implement it by hand. For simplicity, hers reshape
function reads a non-nested list (which means, all elements in the list are numbers, not a list), and the size of two dimensions of the output list, and then output a 2-dimension list with the data, the order and the number of elements unchanged, but with the specified shape.
A well-formatted Python list can be written as a list of comma-separated values (items) between square brackets []
. See the sample input and output for more information.
The size of each dimensions describes the shape of the list. A list consists of lists, each of which consists of numbers. For example, a list like [[1, 2, 3], [4, 5, 6]]
, consists of two lists, each of which consists of three numbers.
ProtectEMmm finished implementing it quickly. Could you implement it as fast as she can?
Input
The first line contains a integer (), indicating the number of test cases.
In each test case:
The first line contains a string , indicating the list in Python. It is guaranteed that is not an empty list. That is, is not equal to []
.
The second line contains two numbers , the size of each dimension of the output list. Your output list need to consist of lists, each of which consists of numbers.
It is guaranteed that the number of elements in equals to , all elements in the list have no leading zeros, and are in the range 0 to 1000, and the sum of the number of elements in all test cases does not exceed .
(请不要使用 scanf 进行读入)
Output
For each test case, you should print a line of string, the result of your reshape
operation.
Note that you need to print exactly one space between every comma and next item in your output.
Sample Input
4
[3, 1, 4, 1, 5, 9, 2, 6]
2 4
[998, 244, 3, 5, 3]
5 1
[1, 1, 2, 3, 5, 8, 13, 21, 34]
1 9
[2, 3, 5, 7, 11, 13, 17, 19, 23]
3 3
Sample Output
[[3, 1, 4, 1], [5, 9, 2, 6]]
[[998], [244], [3], [5], [3]]
[[1, 1, 2, 3, 5, 8, 13, 21, 34]]
[[2, 3, 5], [7, 11, 13], [17, 19, 23]]
Source
2023“钉耙编程”中国大学生算法设计超级联赛(9)