排序矩形矩阵


15

给定一个矩阵A,我们可以说,如果矩阵的每一行和每一列都A被排序(对行从左到右,对列从上到下,则是“已排序” )。因此,该矩阵被排序:

[ 1 2 3 4 5 ]
[ 2 2 3 4 5 ]
[ 3 3 3 4 5 ]
[ 4 4 4 4 5 ]
[ 5 5 5 5 5 ]

但是,此矩阵未排序:

[ 1 2 3 4 5 ]
[ 2 1 3 4 5 ]
[ 3 3 3 4 5 ]
[ 4 5 4 5 5 ]
[ 5 5 5 5 5 ]

第二列(的换位[2, 1, 3, 5, 5])和第四列([4, 5, 4, 5, 5])没有按升序排序。

给定矩阵(作为2D矩形嵌套列表或在行之间和值之间具有一致的,不同的定界符的字符串)以及可选的矩阵的宽度和/或高度,请输出具有相同格式的排序矩阵(以相同格式)尺寸并包含与输入矩阵相同的值。

矩阵中的值将始终在包含范围内[-128, 127](8位带符号的二进制补码整数)。输入矩阵的尺寸没有限制。

例子

因为每个输入都有多个有效输出,所以这些都是可能的输出。满足条件的任何输出都是有效的。这些示例以Python列表符号给出。

[[0, -29]] -> [[-29, 0]]
[[-36, -18, 0], [8, 99, 112], [14, 6, -12]] -> [[-36, -18, 0], [-12, 6, 14], [8, 99, 112]]
[[-20, -102, -41, -57, -73, 58], [49, 82, 60, -53, 58, 60], [-32, -127, -23, -70, -46, -108], [-56, -15, 98, -90, 73, -67], [97, 74, 59, 126, -7, 46], [-101, 42, -39, -52, 89, 29]] -> [[-127, -108, -101, -70, -53, -32], [-102, -90, -67, -52, -23, 42], [-73, -57, -46, -20, 46, 59], [-56, -41, -15, 49, 60, 74], [-39, -7, 58, 60, 82, 97], [29, 58, 73, 89, 98, 126]]
[[23, 82, 94, 7, -39, 70, -31, -120], [-13, 78, -70, 28, -10, 30, 101, 48], [60, -111, 32, 93, 91, 77, -27, 7], [-37, -41, -8, 80, 102, 18, -16, -48], [97, -14, -102, -53, 108, -92, -83, 108], [-67, -121, -15, -9, 91, -89, -127, -109], [-127, -103, -48, -2, -106, 3, -114, 97], [28, -78, -40, 52, 39, 115, -88, 10]] -> [[-127, -127, -120, -109, -92, -70, -40, -14], [-121, -114, -106, -89, -67, -39, -13, 7], [-111, -103, -88, -53, -37, -10, 10, 32], [-102, -83, -48, -31, -9, 18, 39, 77], [-78, -48, -27, -8, 23, 48, 78, 91], [-41, -16, -2, 28, 52, 80, 93, 97], [-15, 3, 28, 60, 82, 94, 101, 108], [7, 30, 70, 91, 97, 102, 108, 115]]
[[85, 90, -65, -38, -58, -71, 123, -83, 44], [55, -34, 21, 103, -10, 59, -109, 30, -41], [108, -106, -90, 74, 117, 15, -63, 94, -37], [43, -118, -126, -45, 77, -62, 22, 76, 9], [56, 31, 58, 51, -64, 125, -48, -123, -108], [77, -53, -61, 99, -16, -21, -98, -50, 60], [127, -113, -9, 33, -6, -102, -47, -122, 31], [-59, -23, 0, 21, 14, 61, 83, 47, -6], [97, -120, -111, 113, -68, -128, -81, 68, 88]] -> [[-128, -126, -122, -113, -106, -81, -62, -47, -16], [-123, -120, -111, -102, -71, -61, -45, -10, 21], [-118, -109, -98, -68, -59, -41, -9, 21, 44], [-108, -90, -65, -58, -38, -6, 22, 47, 60], [-83, -64, -53, -37, -6, 30, 51, 61, 77], [-63, -50, -34, 0, 31, 55, 68, 83, 94], [-48, -23, 9, 31, 56, 74, 85, 97, 108], [-21, 14, 33, 58, 76, 88, 99, 113, 123], [15, 43, 59, 77, 90, 103, 117, 125, 127]]
[[-80, -42, 1, 126, -42, -34, 81, 73, 6, 9, 72], [-98, -98, -82, 63, -16, -116, 29, 61, 119, 20, 19], [-99, -70, 41, 44, 117, 61, 89, -117, 92, -3, -49], [18, 122, 126, 84, 79, 114, -61, 45, 80, 109, -107], [-77, 73, -62, -58, -25, -24, 126, -14, -13, -90, -60], [84, 1, 95, 21, -13, 26, 75, 78, -36, -76, -91], [54, -58, 79, -128, 63, 119, 79, 106, 103, 125, 98], [126, 37, -77, -15, -57, 63, 22, -98, 93, 67, 41], [93, -45, -91, 34, 29, -38, -103, 109, -92, 6, -115], [32, -112, -29, -65, 61, 20, 80, -41, -68, 28, 25], [56, -112, 47, -88, 56, -35, -26, 13, 122, 27, -70], [108, -69, -42, 24, 94, -20, -46, 90, -98, 112, 32]] -> [[-128, -117, -115, -107, -98, -91, -77, -62, -45, -29, -3], [-116, -112, -103, -98, -90, -76, -61, -42, -26, 1, 21], [-112, -99, -98, -88, -70, -60, -42, -25, 1, 22, 34], [-98, -92, -82, -70, -58, -42, -24, 6, 24, 37, 61], [-91, -80, -69, -58, -41, -20, 6, 25, 41, 61, 73], [-77, -68, -57, -38, -16, 9, 26, 41, 61, 75, 81], [-65, -49, -36, -15, 13, 27, 44, 63, 78, 84, 93], [-46, -35, -14, 18, 28, 45, 63, 79, 84, 94, 108], [-34, -13, 19, 29, 47, 63, 79, 89, 95, 109, 117], [-13, 20, 29, 54, 67, 79, 90, 98, 109, 119, 122], [20, 32, 56, 72, 80, 92, 103, 112, 119, 125, 126], [32, 56, 73, 80, 93, 106, 114, 122, 126, 126, 126]]
[[53, 109, -41, 66, 63, -108, -24, 85, 28, 57, -11, -94, -16], [-28, -113, 58, 115, -28, -124, 71, -109, -65, 45, 75, 97, 107], [124, 23, 101, 112, -64, 19, 21, 34, 6, -2, -70, -92, 122], [19, 94, 80, -105, -3, -125, -2, 44, -24, 41, -30, 64, 32], [36, -44, 59, -28, -36, -11, 111, -64, 78, 120, 1, 102, 49], [-128, 67, 17, -9, -64, -86, 117, 7, 118, 7, -11, -82, 124], [5, -36, 22, 98, -78, -33, 100, 92, -55, 125, -28, 24, -6], [97, 31, -106, -15, 8, 80, -86, -107, -105, -5, -71, 76, 124], [-83, 24, -116, 66, 82, -32, -19, 111, -84, -77, -14, 67, -70], [77, -111, -101, -91, -23, 36, 24, -33, 13, -90, -9, 32, -54], [51, -31, 125, -25, -61, 5, 71, -81, -3, -39, 109, -17, -97], [61, -46, -122, 76, 13, -101, 24, 97, 39, -29, -22, -3, -116], [56, 0, -4, 71, -116, 115, 79, -83, 74, 44, -77, 42, -30], [-72, 45, -109, -82, 43, 38, -117, 1, 69, -66, -18, 108, 8]] -> [[-128, -125, -122, -116, -109, -105, -92, -83, -71, -55, -32, -24, -9], [-124, -117, -116, -109, -105, -91, -82, -70, -54, -31, -23, -9, 5], [-116, -113, -108, -101, -90, -82, -70, -46, -30, -22, -6, 5, 22], [-111, -107, -101, -86, -81, -66, -44, -30, -19, -5, 6, 23, 36], [-106, -97, -86, -78, -65, -41, -29, -18, -4, 7, 24, 38, 51], [-94, -84, -77, -64, -39, -28, -17, -3, 7, 24, 39, 53, 66], [-83, -77, -64, -36, -28, -16, -3, 8, 24, 41, 56, 67, 76], [-72, -64, -36, -28, -15, -3, 8, 24, 42, 57, 67, 76, 85], [-61, -33, -28, -14, -2, 13, 28, 43, 58, 69, 77, 92, 100], [-33, -25, -11, -2, 13, 31, 44, 59, 71, 78, 94, 101, 109], [-24, -11, 0, 17, 32, 44, 61, 71, 79, 97, 102, 111, 115], [-11, 1, 19, 32, 45, 63, 71, 80, 97, 107, 111, 117, 122], [1, 19, 34, 45, 64, 74, 80, 97, 108, 112, 118, 124, 124], [21, 36, 49, 66, 75, 82, 98, 109, 115, 120, 124, 125, 125]]
[[47, -58, 7, -88, 126, -87, 103, 125, 83, 32, 116, 107, -92, -96, 110, -102], [-75, -81, 53, -93, 91, -5, -4, 104, 88, -73, -114, -113, 126, 78, -114, -3], [125, -68, -88, -17, 1, 53, -124, -59, -19, 87, -60, 55, -30, -6, 39, 37], [-38, -123, 125, 119, -43, 11, -25, -89, -57, 112, 123, 9, -76, -125, 118, 68], [-119, -97, -42, 73, 80, 108, -96, -54, -110, 115, -58, -67, -9, 94, 71, -56], [-25, 109, -51, 71, 61, 12, 122, -99, -16, -87, -127, -76, 46, 102, 52, 114], [97, 26, -112, 49, -44, -26, -93, -119, 21, 101, 83, -112, 14, 41, 120, 37], [90, 95, 89, 73, 51, -33, 3, -125, -106, -83, -5, -26, 33, -119, -74, 41], [9, -81, 116, -124, -8, -15, 65, 104, 41, -92, 2, 51, 33, 115, -47, 30], [87, -127, 121, 42, 33, -22, 28, -74, 26, 55, 126, -70, 0, -63, -40, -51], [-117, 79, -113, -4, 78, -33, -54, -40, -1, -48, 60, 91, 119, 117, -75, 114], [7, 102, 6, 77, -112, -128, 34, 112, -82, -17, -120, -96, -101, -79, -27, -84], [-74, -77, 67, -78, -72, 80, 59, 115, -76, -7, 66, -28, 120, 117, 56, -46], [80, 42, -121, -5, 73, -82, -115, -72, 10, -120, -26, -82, -22, 110, -7, -119], [10, -88, 39, 92, -16, 58, -40, 79, 116, 75, 96, -102, 4, 93, 46, -95], [20, -61, 110, 18, -103, -87, -67, -26, -74, -22, 1, -106, -81, -20, 10, 87]] -> [[-128, -127, -125, -124, -120, -117, -112, -102, -95, -87, -81, -74, -59, -43, -26, -7], [-127, -125, -123, -119, -115, -112, -102, -93, -87, -79, -74, -58, -42, -25, -7, 6], [-124, -121, -119, -114, -112, -101, -93, -87, -78, -73, -58, -40, -25, -6, 7, 26], [-120, -119, -114, -110, -99, -92, -84, -77, -72, -57, -40, -22, -5, 7, 28, 41], [-119, -113, -106, -97, -92, -83, -76, -72, -56, -40, -22, -5, 9, 30, 42, 55], [-113, -106, -96, -89, -82, -76, -70, -54, -38, -22, -5, 9, 32, 42, 56, 73], [-103, -96, -88, -82, -76, -68, -54, -33, -20, -4, 10, 33, 46, 58, 73, 80], [-96, -88, -82, -75, -67, -51, -33, -19, -4, 10, 33, 46, 59, 73, 83, 91], [-88, -81, -75, -67, -51, -30, -17, -3, 10, 33, 47, 60, 75, 83, 92, 102], [-81, -74, -63, -48, -28, -17, -1, 11, 34, 49, 61, 77, 87, 93, 103, 110], [-74, -61, -47, -27, -16, 0, 12, 37, 51, 65, 78, 87, 94, 104, 110, 115], [-60, -46, -26, -16, 1, 14, 37, 51, 66, 78, 87, 95, 104, 112, 115, 117], [-44, -26, -15, 1, 18, 39, 52, 67, 79, 88, 96, 107, 112, 116, 118, 120], [-26, -9, 2, 20, 39, 53, 68, 79, 89, 97, 108, 114, 116, 119, 121, 125], [-8, 3, 21, 41, 53, 71, 80, 90, 101, 109, 114, 116, 119, 122, 125, 126], [4, 26, 41, 55, 71, 80, 91, 102, 110, 115, 117, 120, 123, 125, 126, 126]]
[[-88, -62, -59, -18, 118, -13, -93, 75, 44, 67, -122, -1, 117, -121, 118, 13, -33, 44], [-4, -75, 95, 25, 9, -104, 6, 79, -110, 3, -108, 117, 96, 113, 69, 55, 75, -95], [-69, 11, 87, -78, -18, -17, -52, 6, 88, 31, 39, 45, 61, -75, -83, 117, 85, -3], [-27, 83, -86, -69, -29, -15, 62, -90, -127, 53, -71, 77, -95, -86, -20, 69, 103, -111], [3, -6, -70, -121, -58, -72, 88, 105, 68, -31, 86, -28, 69, 78, 13, 88, 19, 75], [69, 73, 116, -2, -93, 15, 74, 58, 98, -100, -54, 95, 47, -126, -71, 63, 84, 113], [110, -42, -33, -87, 109, 86, -75, 25, 83, -25, -76, 84, -42, -57, -93, -9, -90, 3], [-100, 36, -83, 10, -85, 88, -15, 107, -76, -37, 109, 79, -120, 118, -60, 113, -124, -15], [123, 122, -94, 14, -16, 118, -57, -111, 80, 62, 56, 66, 27, -44, -53, -13, 94, -28], [116, -67, 8, -70, -54, -1, 53, 40, -78, 15, -121, -30, -125, -16, -74, 119, 97, 43], [-24, 109, -72, 16, 55, -51, -87, 46, -62, 69, -106, -49, -112, 71, -55, 104, -110, 62], [67, 13, -75, 106, -35, -54, 15, -104, 34, 93, 39, -126, -29, 61, 29, 4, 70, -28], [27, -89, -15, -32, -82, -72, 53, -22, -23, 49, -16, 76, -25, 31, 115, -88, -57, -97], [1, 29, 54, 88, 86, 77, -58, 100, -125, 117, 102, 41, 99, 115, 24, -16, -99, -116], [-85, -47, -108, 26, 18, -107, -88, 110, 27, -118, 88, -122, -85, -94, -33, 51, 40, 77], [-3, 52, -20, 12, 117, 101, 34, -8, -100, -23, 45, 83, -88, -90, -47, 70, 29, -111], [26, -68, 7, 38, -118, -53, -79, -48, 41, -88, 35, 86, 66, 24, 37, 72, -66, -77]] -> [[-127, -126, -125, -122, -121, -112, -108, -100, -93, -88, -83, -75, -68, -54, -37, -25, -15, 4], [-126, -125, -122, -120, -111, -108, -100, -93, -88, -83, -75, -67, -54, -35, -24, -15, 6, 18], [-124, -121, -118, -111, -107, -99, -93, -88, -82, -74, -66, -54, -33, -23, -13, 6, 19, 34], [-121, -118, -111, -106, -97, -90, -87, -79, -72, -62, -53, -33, -23, -13, 7, 24, 34, 45], [-116, -110, -104, -95, -90, -87, -78, -72, -62, -53, -33, -22, -9, 8, 24, 35, 45, 58], [-110, -104, -95, -90, -86, -78, -72, -60, -52, -32, -20, -8, 9, 25, 36, 46, 61, 69], [-100, -94, -89, -86, -77, -71, -59, -51, -31, -20, -6, 10, 25, 37, 47, 61, 69, 75], [-94, -88, -85, -76, -71, -58, -49, -30, -18, -4, 11, 26, 38, 49, 62, 69, 76, 83], [-88, -85, -76, -70, -58, -48, -29, -18, -3, 12, 26, 39, 51, 62, 69, 77, 84, 88], [-85, -75, -70, -57, -47, -29, -17, -3, 13, 27, 39, 52, 62, 70, 77, 84, 88, 96], [-75, -69, -57, -47, -28, -16, -2, 13, 27, 40, 53, 63, 70, 77, 85, 88, 97, 104], [-69, -57, -44, -28, -16, -1, 13, 27, 40, 53, 66, 71, 78, 86, 88, 98, 105, 110], [-55, -42, -28, -16, -1, 14, 29, 41, 53, 66, 72, 79, 86, 88, 99, 106, 110, 115], [-42, -27, -16, 1, 15, 29, 41, 54, 67, 73, 79, 86, 93, 100, 107, 113, 116, 117], [-25, -15, 3, 15, 29, 43, 55, 67, 74, 80, 86, 94, 101, 109, 113, 116, 117, 118], [-15, 3, 15, 31, 44, 55, 68, 75, 83, 87, 95, 102, 109, 113, 117, 117, 118, 119], [3, 16, 31, 44, 56, 69, 75, 83, 88, 95, 103, 109, 115, 117, 118, 118, 122, 123]]
[[84, 18, -122, 74, -47, 17, -69, 121, -79, 110, 10, 122, 84, 19, 77, -57, 25, 87, -42], [95, 89, 10, -1, -24, -93, -26, -39, 11, -15, 47, 23, 114, 36, 121, -87, 106, 120, -86], [48, 66, 65, 28, 74, -22, -67, 77, -77, 19, 88, -24, -88, 85, -34, 13, 103, -102, 86], [108, -17, -122, -13, 63, 61, -56, 24, -48, -3, -85, -57, 11, -52, -26, -24, 48, 100, 18], [-91, -126, 124, 5, -118, 93, 94, -100, -24, 15, 77, -43, 64, 51, 64, 7, -22, -47, 79], [98, 80, 117, -19, -55, -95, -35, -48, -56, -122, -120, 52, 54, 37, -101, -38, -35, 101, -6], [72, 68, 26, -79, -1, 25, -3, -40, 2, 56, 119, 17, -95, 83, -94, -79, -88, -110, 85], [55, 39, 75, 127, 110, 0, 56, -1, 39, 116, 44, 120, -113, 81, 113, 10, 78, 114, -79], [103, 121, 78, -121, -17, 33, 117, 110, -26, 2, -79, 27, -117, 62, -27, -17, -20, 104, 115], [-11, 67, 76, 62, -14, 78, -94, -8, -71, 15, 77, 98, 127, 109, 61, 33, -51, 65, -103], [-91, 97, 83, -61, 22, -31, 20, -119, 40, -48, -3, 34, -70, 23, -80, -73, 5, 23, -102], [109, 78, 124, 118, -39, -3, -114, -50, 0, 79, -68, -34, -96, 104, -120, 41, 2, 108, -17], [-90, -30, -25, -29, -52, -37, -49, 20, 91, -48, -91, 80, -117, -6, -88, -68, 69, 103, 118], [-79, -118, -122, -112, 71, -4, 28, 78, -77, -33, 10, 21, -125, 69, -88, 18, 99, 11, -127], [-124, -53, -4, 80, -94, -44, -124, 94, 97, -55, -89, -78, -37, -38, -40, -11, -116, 84, 18], [44, 32, -44, 76, -101, 85, -67, 69, -4, 20, -89, -103, 117, 18, -121, 84, 18, -91, -106], [107, 58, 6, -72, 112, 96, 39, 77, -4, 104, 60, 112, 39, -102, -4, -11, 80, 36, -117], [96, -79, 119, -65, 80, -35, -60, 4, -63, 92, 76, -46, -2, 59, -86, -105, -76, 106, -102], [-88, -89, 69, -5, 63, 75, -59, -93, 101, 33, 64, 114, -126, -106, 33, 113, 50, 71, 82], [-94, 9, -3, 11, 115, -121, 111, -34, -11, 96, -34, 51, -44, -56, -20, -14, 1, 49, 100]] -> [[-127, -126, -125, -122, -121, -119, -116, -105, -101, -93, -88, -79, -71, -57, -48, -38, -26, -14, -3], [-126, -124, -122, -121, -118, -114, -103, -100, -93, -88, -79, -70, -56, -48, -37, -26, -14, -3, 10], [-124, -122, -121, -118, -113, -103, -96, -91, -88, -79, -69, -56, -47, -37, -25, -13, -3, 10, 19], [-122, -120, -117, -112, -102, -95, -91, -88, -79, -68, -56, -47, -35, -24, -11, -2, 10, 19, 33], [-120, -117, -110, -102, -95, -91, -88, -79, -68, -55, -46, -35, -24, -11, -1, 10, 20, 33, 48], [-117, -106, -102, -94, -91, -87, -79, -67, -55, -44, -35, -24, -11, -1, 11, 20, 33, 48, 62], [-106, -102, -94, -90, -86, -78, -67, -53, -44, -34, -24, -11, -1, 11, 20, 33, 49, 62, 69], [-101, -94, -89, -86, -77, -65, -52, -44, -34, -22, -8, 0, 11, 21, 34, 50, 63, 71, 77], [-94, -89, -85, -77, -63, -52, -43, -34, -22, -6, 0, 11, 22, 36, 51, 63, 71, 77, 80], [-89, -80, -76, -61, -51, -42, -34, -20, -6, 1, 13, 23, 36, 51, 64, 72, 78, 81, 86], [-79, -73, -60, -50, -40, -33, -20, -5, 2, 15, 23, 37, 52, 64, 74, 78, 82, 87, 96], [-72, -59, -49, -40, -31, -19, -4, 2, 15, 23, 39, 54, 64, 74, 78, 83, 88, 96, 101], [-57, -48, -39, -30, -17, -4, 2, 17, 24, 39, 55, 65, 75, 78, 83, 89, 97, 103, 107], [-48, -39, -29, -17, -4, 4, 17, 25, 39, 56, 65, 75, 78, 84, 91, 97, 103, 108, 111], [-38, -27, -17, -4, 5, 18, 25, 39, 56, 66, 76, 79, 84, 92, 98, 103, 108, 112, 114], [-26, -17, -4, 5, 18, 26, 40, 58, 67, 76, 79, 84, 93, 98, 104, 109, 112, 115, 117], [-15, -3, 6, 18, 27, 41, 59, 68, 76, 80, 84, 94, 99, 104, 109, 113, 115, 118, 120], [-3, 7, 18, 28, 44, 60, 69, 77, 80, 85, 94, 100, 104, 110, 113, 116, 118, 120, 121], [9, 18, 28, 44, 61, 69, 77, 80, 85, 95, 100, 106, 110, 114, 117, 119, 121, 122, 124], [18, 32, 47, 61, 69, 77, 80, 85, 96, 101, 106, 110, 114, 117, 119, 121, 124, 127, 127]]

非常感谢Sp3000在聊天中帮助我解决了这一难题


1
您的示例似乎与ouptut始终具有相同的输入,也许您可​​能想在输入不等于输出的地方添加一些内容?=)
瑕疵的

@flawr糟糕,我用来生成测试用例的脚本中有一个错误。我已经修复了。
Mego

Answers:


5

果冻,3个字节

FṢs

将矩阵和宽度作为左右参数。在线尝试!

怎么运行的

FṢs  Main link. Arguments: M (matrix), w (width)

F    Flatten M.
 Ṣ   Sort.
  s  Split into rows of width w.

13

视网膜8 6 5字节

感谢randomra节省了3个字节。

O#`.+

输入格式有点尴尬。行内和行之间的定界符都必须由行终止字符组成(即,与不匹配的字符.)。例如,您可以在行内使用单换行,在行之间使用双换行。输出以相同格式打印。

在线尝试!

说明

炫耀Retina最新产品的完美挑战:排序阶段!

排序阶段的工作方式如下:正则表达式与整个字符串匹配。然后,匹配项就是要排序的事物,而围绕它们的所有事物都被视为“列表定界符”,并且保持不变。如果我们将输入视为一个平面列表,对其进行排序并将其重塑为原始矩阵形式,则可以解决此难题。除没有明确的展平和重塑步骤外,这就是排序阶段的工作方式-Retina完全忽略了2D结构。

特别是对于此代码,正则表达式.+简单地匹配所有非换行字符的运行,由于输入格式,所有字符都是数字。默认情况下,匹配项按字典顺序排序,但是该#选项告诉Retina按数字对它们进行排序(通过匹配项中出现的第一个有符号整数)。

对于另外三个字节,通过使用regex,它可以支持任何输入格式-?\d+


现在我可以确定视网膜是一种高尔夫语言。我不太确定。看起来完全是通用的。好吧,也许十进制->一元->十进制的东西有点舍弃了,但我现在知道了。
CalculatorFeline

太棒了。是否可以在不使用内置排序的情况下执行此操作?
Mama Fun

@CatsAreFluffy引用文档,“ Retina主要是为代码高尔夫开发的……”。那就是说我不明白a)“高尔夫语言”和“通用”语言是如何互斥的,b)完全基于正则表达式处理的Retina最初是如何通用的,c)如何添加内置的排序功能使其用途更少。
马丁·恩德

@MamaFunRoll当然,视网膜是图灵完备的。但是,这将是非常痛苦的。您必须将整数转换为一进制,并以一种或另一种方式处理符号(例如,使用一进制+1-1数字)。然后,您可以对列表进行反复冒泡排序,方法是重复地将配对x...yx > y并替换为y...x。然后将一元转换回十进制。
马丁·恩德

@MartinBüttner是的,这是在开玩笑。
CalculatorFeline

11

Pyth,6个 5字节

1字节感谢DenkerAffe

cSsQE

示范

输入是矩阵,然后是宽度。

要对矩阵进行排序,我只需将其展平为一个列表,对列表进行排序,然后再次将其切成矩阵。由于已排序列表的每个子序列都已排序,因此会生成已排序矩阵。


6

Python,43个字节

lambda M:eval("map(sorted,zip(*"*2+"M))))")

有趣的问题是如何申请mapsort-transpose两次。我发现的最短eval字符串是包含两次的字符串。比较一些替代方案:

lambda M:eval("map(sorted,zip(*"*2+"M))))")
lambda M:map(sorted,zip(*map(sorted,zip(*M))))
s="map(sorted,zip(*%s))";lambda M:eval(s%s%M)
g=lambda M:map(sorted,zip(*M));lambda M:g(g(M))
f=lambda M,n=-1:n*M or f(map(sorted,zip(*M)),n+1)
def f(M):exec"M=map(sorted,zip(*M));"*2;return M

(通过别名可以将第二个和第四个缩短1个字节sorted,但这还不够。)

转换为1D,排序并重塑为2D的另一种方法非常接近,为44字节。宽度w作为输入。

lambda M,w:zip(*[iter(sorted(sum(M,[])))]*w)

2

JavaScript(ES6),67个字节

a=>a.map(b=>c.splice(0,b.length),c=[].concat(...a).sort((d,e)=>d-e))

@isaacg的Pyth答案端口。


2

Pyth,7个字节

CSMCSMQ

在这里尝试!

首先,我们对矩阵的所有行进行排序,对结果进行转置,再次对所有行进行排序,然后将其转回。


2

CJam,8个字节

{:$z:$z}

在线尝试!

一个未命名的块,期望在堆栈上有一个矩阵,并将排序后的矩阵留在堆栈上。

也只需对每行(:$)进行排序,对结果(z)进行转置,再次进行排序,然后将其转回。


2

八度,19字节

当两个程序满足要求时不好

@(a)sort(sort(a')') % or
@(a)sort(sort(a)')'

匿名函数,该函数两次对矩阵进行转置以获取原始尺寸。我要声明这将是非高尔夫语言中最短的条目。让我们看看是否有人证明我错了:)


2

Mathematica,26个字节

Nest[Sort@*Transpose,#,2]&

函数名称很长。糟透了


3
当您拥有用于识别颠倒山羊的内建物时,可以使用长名称。:P
Rɪᴋᴇʀ

2

返回 10个字节

[{␇␌}␅␆␎␉]

Try it here.

将匿名lambda留在堆栈上的结果。用法:

""{1 2 3 4}""{1 3 4 2}""{4 1 2 3}""{3 1 2 4}[{␇␌}␅␆␎␉]!

说明

[            ]  lambda
 {␇␌}          push matrix width to stack2
      ␅␆      flatten, sort
          ␎␉   chunk by matrix width (popped from stack2)

2

APL, 11 9个字节

⍴⍴⊂∘⍋∘,⌷,

三个功能组成的挂钩:)。

例:

    (⍴⍴⊂∘⍋∘,⌷,) 2 2⍴10 5 4 8
4  5
8 10

1

Python 3,52个字节

使用numpy的解决方案

from numpy import*
lambda s:sort(sort(array(s)).T).T

sort(s,None)更长,因为我们需要在排序后重塑形状)

结果

>>> f([[-5, -4, -3, -2, -1, 0], [-60, -70, -80, -9, 100, 110], [5, 4, 3, 2, 1, 0], [6, 7, 8, 9, 10, 11]])
array([[-80, -70, -60,  -9,  -1,   0],
       [ -5,  -4,  -3,  -2,   4,   5],
       [  0,   1,   2,   3,  10,  11],
       [  6,   7,   8,   9, 100, 110]])

1

MATL,4个字节

S!S!

在线尝试!

这只是按S列排序()(分别对每一列的内容进行排序),然后转置(!)并再次对输入进行排序,非常简单。该程序需要用分号(;)代替逗号(,)作为行的定界符。


1

Ruby,42个字节

(m,w)->{m.flatten.sort.each_slice(w).to_a}

参数是矩阵及其宽度。它的工作方式与读取的内容完全相同:)


1

CJam,6个字节

{e_$/}

这是一个代码块(未命名的函数),它期望堆栈上的宽度和矩阵,将其弹出并推入排序后的矩阵作为回报。 在线尝试!

怎么运行的

     e# STACK: w (width), M (matrix)
     e#
e_   e# Flatten M.
  $  e# Sort the resulting, flat array.
   / e# Split the sorted array into rows of length w.

0

Perl 6中,28 25个字节

{.flat.sort.rotor(+.[0])}

这需要一个列表列表(而不是数组)。


0

Clojure,39个字节

(fn[M w](partition w(sort(flatten M))))

M是一个嵌套列表(矩阵),并且w是其宽度(列数)。

(f [[1 3 2 3][5 1 3 7][4 3 6 5]] 4)

((1 1 2 3)
 (3 3 3 4)
 (5 5 6 7))

0

C,39字节

f(x,m,n){qsort(x,m*n,4,"YZZ+QQQÃ");}

如果超出范围的字符无法显示,请改用此字符,尽管更长

f(x,m,n){qsort(x,m*n,4,"YZ\x8B\2Z+\2QQQ\xC3");}

用法:

int m, n, i, j;
scanf ("%d%d", &m, &n);
int arr[m][n];
for (i=0; i<m; i++) {
    for (j=0; j<n; j++) {
        scanf("%d", &arr[i][j]);
    }
}
f(arr,m,n);
for (i=0; i<m; i++) {
    for (j=0; j<n; j++) {
        printf("%d ", arr[i][j]);
    }
    puts("");
}

文本的反汇编程序: 00000000: 59 POP ECX 00000001: 5A POP EDX 00000002: 8B02 MOV EAX,[EDX] 00000004: 5A POP EDX 00000005: 2B02 SUB EAX,[EDX] 00000007: 51 PUSH ECX 00000008: 51 PUSH ECX 00000009: 51 PUSH ECX 0000000A: C3 RETN

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.