digitalmars.D.learn - array help.
- NA (33/33) Jul 08 2014 Hi,
- Tobias Pankrath (1/3) Jul 08 2014 Use std.algorithm.sort with a custom comparison function.
- bearophile (7/8) Jul 08 2014 It's a very nice example of learning algorithms and data fitting.
- Tobias Pankrath (4/11) Jul 08 2014 I had your solution in mind, too, as a hint that the problem
- bearophile (36/37) Jul 08 2014 Is this good enough?
- NA (20/57) Jul 08 2014 Thanks but
- bearophile (43/44) Jul 08 2014 Why no sort?
- NA (6/9) Jul 08 2014 Thanks,
- Tobias Pankrath (2/4) Jul 08 2014 What are you trying to do? To reorder the elements of a sequence
Hi, I have the following array. [ [ [], [], [[0, 1] [0, 2]], [[0, 0] [0, 3]] ] [ [], [], [[1, 1] [1, 2]], [[1, 0] [1, 3]] ] ] and would like it to be arranged like [ [ [[0, 1] [0, 2]], [[0, 0] [0, 3]], [], [] ] [ [[1, 1] [1, 2]], [[1, 0] [1, 3]], [], [] ] ] Any ideas welcome. NA
Jul 08 2014
Any ideas welcome. NAUse std.algorithm.sort with a custom comparison function.
Jul 08 2014
Tobias Pankrath:Use std.algorithm.sort with a custom comparison function.It's a very nice example of learning algorithms and data fitting. I have shown an answer very fitted (perhaps over-fitted) on the given example. With just the given data there is no way to know how much general the problem is :-) Bye, bearophile
Jul 08 2014
On Tuesday, 8 July 2014 at 18:26:20 UTC, bearophile wrote:Tobias Pankrath:I had your solution in mind, too, as a hint that the problem might be underspecified.Use std.algorithm.sort with a custom comparison function.It's a very nice example of learning algorithms and data fitting. I have shown an answer very fitted (perhaps over-fitted) on the given example.With just the given data there is no way to know how much general the problem is :-)Yeah, but I hope at least he knows it.
Jul 08 2014
NA:Any ideas welcome.Is this good enough? void main() { import std.algorithm: swap; int[][][][] a = [[[], [], [[0, 1], [0, 2]], [[0, 0], [0, 3]] ], [[], [], [[1, 1], [1, 2]], [[1, 0], [1, 3]] ] ]; int[][][][] b = [[[[0, 1], [0, 2]], [[0, 0], [0, 3]], [], [], ], [[[1, 1], [1, 2]], [[1, 0], [1, 3]], [], [] ] ]; foreach (s1; a) { swap(s1[0], s1[2]); swap(s1[1], s1[3]); } assert(a == b); } Bye, bearophile
Jul 08 2014
On Tuesday, 8 July 2014 at 18:16:55 UTC, bearophile wrote:NA:Thanks but int[][][][] a = [[[], [], [], [], [[0, 1], [0, 2]], [[0, 0], [0, 3]] ], [[], [], [], [], [[1, 1], [1, 2]], [[1, 0], [1, 3]] ] ]; will not work and no I can't sort it. Cheers.Any ideas welcome.Is this good enough? void main() { import std.algorithm: swap; int[][][][] a = [[[], [], [[0, 1], [0, 2]], [[0, 0], [0, 3]] ], [[], [], [[1, 1], [1, 2]], [[1, 0], [1, 3]] ] ]; int[][][][] b = [[[[0, 1], [0, 2]], [[0, 0], [0, 3]], [], [], ], [[[1, 1], [1, 2]], [[1, 0], [1, 3]], [], [] ] ]; foreach (s1; a) { swap(s1[0], s1[2]); swap(s1[1], s1[3]); } assert(a == b); } Bye, bearophile
Jul 08 2014
NA:will not work and no I can't sort it.Why no sort? void main() { import std.algorithm: sort, SwapStrategy; import std.array: empty; int[][][][] a = [[[], [], [], [], [[0, 1], [0, 2]], [[0, 0], [0, 3]] ], [[], [], [], [], [[1, 1], [1, 2]], [[1, 0], [1, 3]] ] ]; int[][][][] b = [[[[0, 1], [0, 2]], [[0, 0], [0, 3]], [], [], [], [] ], [[[1, 1], [1, 2]], [[1, 0], [1, 3]], [], [], [], [] ] ]; foreach (s1; a) s1.sort!(q{ a.empty < b.empty }, SwapStrategy.stable); assert(a == b); } Bye, bearophile
Jul 08 2014
On Tuesday, 8 July 2014 at 18:40:37 UTC, bearophile wrote:NA:Thanks, It works as expected. As for the sort - wasn't thinking straight. Cheers, NAwill not work and no I can't sort it.Why no sort?
Jul 08 2014
will not work and no I can't sort it. Cheers.What are you trying to do? To reorder the elements of a sequence in a specified manner is called sorting, where I live.
Jul 08 2014