r/dailyprogrammer • • Aug 05 '12
[8/3/2012] Challenge #85 [easy] (Row/column sorting)
Write a program that reads a matrix of numbers separated by newlines and whitespace, like this:
10 5 4 20
9 33 27 16
11 6 55 3then calculates the sums for each row and column, optionally outputting them...
Rows: 39 85 75
Columns: 30 44 86 39then prints two new matrices:
- first, print the matrix with its rows sorted by their sums
- then, print the matrix with its columns sorted by their sums.
Like this:
10 5 4 20
11 6 55 3
9 33 27 16
10 20 5 4
9 16 33 27
11 3 6 55Here's a large input matrix to test your program on.
5 58 88 60 11 23 97 48 59 82 95 24 6 67 47
45 14 36 99 16 70 77 18 43 39 97 54 11 53 98
85 14 96 66 34 86 95 49 4 49 72 76 45 49 37
72 88 20 56 37 16 20 97 71 11 91 33 90 5 96
15 53 54 95 61 93 75 95 51 83 71 70 2 57 83
54 29 56 80 79 93 40 55 40 14 63 94 77 12 90
96 97 3 47 2 43 12 2 82 92 1 99 90 13 35
24 19 54 96 82 96 10 40 62 30 35 16 70 83 64
59 81 8 84 14 46 32 45 41 35 98 66 87 51 49
13 49 12 51 34 82 36 77 88 14 84 41 66 18 56
6 68 82 63 77 72 67 36 85 53 66 70 21 86 80
40 51 87 5 78 56 99 44 39 48 78 56 19 55 40
5 94 62 46 85 73 24 67 95 63 42 95 43 53 4
14 99 7 36 25 65 22 71 20 80 16 10 71 97 23
99 77 85 53 13 32 37 19 61 32 45 62 25 18 32
98 79 35 17 26 96 22 3 76 20 81 9 40 95 72
18 39 55 99 96 63 90 78 77 81 2 99 68 6 84
53 27 68 43 48 29 27 14 50 29 53 65 5 56 46
94 36 17 64 2 93 5 95 47 78 90 3 85 26 32
46 62 70 63 81 6 86 51 44 96 47 83 33 28 28For bonus points, format your output matrices nicely (align the columns, draw boxes with - and |...)
15
Upvotes
4
u/bh3 Aug 05 '12 edited Aug 05 '12
Python:
3
u/agph Aug 05 '12
Haskell
2
u/5outh 1 0 Aug 05 '12 edited Aug 05 '12
Haskell solution with no bonus
yet.Edit: Decided not to do bonus because formatting output makes the code kinda yucky. I'm just gonna leave this the way it is.
2
u/jlink005 Aug 08 '12
C#. This feels really bloated.
2
u/mathiasbynens 0 0 Aug 08 '12 edited Aug 09 '12
JavaScript (with Node.js):
Output with the small input:
1
u/[deleted] Aug 05 '12
Should the input be made through stdin, a file or be hardcoded?
1
u/leonardo_m Aug 05 '12 edited Aug 05 '12
D version:
Output with the small input:
Python2 version, a little too much compressed:
1
u/wocamai Aug 06 '12 edited Aug 06 '12
Done in Python 2.7 and I can't say I'm proud of the print statements. Though I am happy because this is my first time using zip or lambda functions, so you win some you lose some.
EDIT: also i should probably add that i more or less took the idea of the sysargs bit from bh3 since it seemed better than me forgetting to enter the file name and getting tracebacks over and over.
1
u/semicolondash Aug 06 '12 edited Aug 06 '12
In Scala, aligns the columns, but does not draw boxes.
1
u/ArriMurri Aug 06 '12
Scala:
1
u/paininthebass Aug 06 '12
Solution in C++11. The class Matrix<T> has 2 non-modifying functions sortInRowOrder and sortInColOrder. Supports output using operator<<
1
u/NJL97 Aug 07 '12
Will anyone be willing to talk me through this in C++ please.
1
u/bschlief 0 0 Aug 07 '12
Ruby. Rubyists, I'd love your comments, as I'm still very new to Ruby and coming from a C background. In particular, the print statements seem sort of ham-fisted.
Source code available under http://github.com/bschlief/daily.programmer
1
u/oskar_stephens Aug 08 '12
Ruby
One of my first attempts at ruby. I learned about the transpose function on arrays about halfway through, so the initial transpose is left as a warning sign to others.
1
u/Puzzel Aug 08 '12
Although this was 4 days ago, here's a solution in Python 3:
Not proud of the way I printed it, but I like the rest.
1
u/cdelahousse Aug 09 '12
Verbose javascript
1
u/larg3-p3nis Aug 11 '12
I've been trying to figure a way to do this in Java for days but I can't seem to. Apparently I'm not the only one.
1
u/02471 Aug 15 '12
Python
0
u/landscapez Aug 15 '12
C++ solution