fave77 / Mathball

A JavaScript library for Competitive Programming
https://fave77.github.io/Mathball-Docs/
MIT License
99 stars 49 forks source link

Given a matrix, sort the rows (either in ascending or descending order) based on the values of column (column index given as an input to the function). #159

Open chichra opened 5 years ago

chichra commented 5 years ago

Do the checklist before filing the issue:

NOTE: Provide a clear and concise description of the feature that needs to be added! Or if its a bug, then provide the necessary steps to reproduce it along with screenshots.

chichra commented 5 years ago

@pbiswas101 Similarly, this can be extended to sorting the columns of a matrix based on a row index.

For example , given matrix M = [[3 2 1] [4 6 5] [3 4 2]]

Sorting the rows by last column gives.

M =

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

fave77 commented 5 years ago

@chichra can you be more specific on this:

sort the rows based on the values of column

I mean are you talking about just swapping two rows? and how do the sorting works? In the example you mentioned, what if the last row was [ 3 4 7 ] and how this would affect the outcome!

viditkumar commented 5 years ago

@pbiswas101 I suppose, she is talking about swapping the two rows. If the last row was [ 3 4 7 ], please look at the below example.

M = [[3 2 1] [4 6 5] [3 4 7]]

Sorting the rows by last column gives. M = [[3 2 1] [4 6 5] [3 4 7]]

There is no swapping required in the above scenario. If this issue is fine and gets accepted, I would like to work on it.

sakshichahal53 commented 5 years ago

For sorting rows by a column number let say 2, we have to look upon sorting the numbers in the given column. And accordingly the rows are swapped. Given : M= [[1 2 10], [4 6 5], [2 4 7]]

Sorting rows by 1 column gives: Since numbers in first column are 3 4 3 sorted = (3 3 4), we have A= [[1 2 10], [2 4 7], [4 6 5]]

Sorting rows by 2 column gives: Since numbers in second column are 2 6 4 sorted = (2 4 6), we have B= [[1 2 10], [2 4 7], [4 6 5]]

Sorting rows by 3 column gives: Since numbers in third column are 10 7 5 sorted = (5 7 10), we have C= [[4 6 5], [2 4 7], [1 2 10]]

@chichra I believe this is what you suppose to mean?

chichra commented 5 years ago

@sakshichahal53 yes absolutely, that is what I wanted.

chichra commented 5 years ago

@pbiswas101 as I mentioned earlier that similarly, this can be extended to sorting the columns of a matrix based on a row index.

For example , given matrix the same matrix, If i wish to sort (rather rearrange) the columns of matrix based on the values of row 1

Original matrix is M = [[3 2 1] [4 6 5] [3 4 2]]

Sorting the columns by row indices gives.

sorted matrix M(based on row 1)= [[1 2 3] [5 6 4] [2 4 3]]

sorted matrix M (based on row 2)= [[3 1 2] [4 5 6] [3 2 4]]

sorted matrix M (based on row 3)= [[1 3 2] [5 4 6] [2 3 4]]

Shubhayu-Das commented 5 years ago

I have a doubt: How will this help in competitive programming?

fave77 commented 5 years ago

@chichra will you be working on this issue?

chichra commented 5 years ago

@pbiswas101 yes

fave77 commented 5 years ago

@chichra okay, how are you planning to implement this? please provide code snippets so as to show the usage of this function in terms of a matrix a,

const a = new M.Matrix([ [3, 2, 1], [4, 6, 5], [3, 4, 2] ])
chichra commented 5 years ago

a.sort('row', 0) output will be [[1 2 3] [5 6 4] [2 4 3]]

a.sort('column', 0) [[3 2 1] [3 4 2] [4 6 5]]

The first parameter in function signature represents if we want to sort based on row/column and the second parameter denotes the index.

fave77 commented 5 years ago

@chichra this issue is more or less similar to issue #152 and the sorting also need to include both ascending / descending order as mentioned in the PR #169

So, the overall implementation would be:

a.sort('row', true, 0)

where 2nd and 3rd arguments are optional! If the 3rd argument is to be omitted, then the behavior of sorting would go along as mentioned in issue #152

Hence, once that issue is resolved you may start working on this one, building on top of that implementation.

chichra commented 5 years ago

Yes, sure.