We are given a matrix with R rows and C columns has cells with integer coordinates (rCenter, cCenter), where 0 <= r < R and 0 <= c < C.
You are given four integers row, cols, rCenter, and cCenter. There is a rows x cols matrix and you are on the cell with the coordinates (rCenter, cCenter).
Return the coordinates of all cells in the matrix, sorted by their distance from (rCenter, cCenter) from the smallest distance to the largest distance. You may return the answer in any order that satisfies this condition.
The distance between two cells (r1, c1) and (r2, c2) is |r1 – r2| + |c1 – c2|.
Input:
rows = 1, cols = 2, rCenter = 0, cCenter = 0
Output: [[0,0],[0,1]]
Explanation: The distances from (0, 0) to other cells are: [0,1]
def distOrder(rows,cols,rC,cC): d,L={},[] for i in range(rows): for j in range(cols): d[i,j]=abs(i-rC)+abs(j-cC) d=sorted(d.items(),key=lambda x:x[1]) for i in d: L.append(list(i[0])) return L rows=int(input('rows: ')) cols=int(input('cols: ')) rC=int(input('rC: ')) cC=int(input('cC: ')) print(distOrder(rows,cols,rC,cC))
INPUT_1:
rows: 1
cols: 2
rC: 0
cC: 0
OUTPUT:
[ [ 0, 0 ], [ 0, 1 ] ]
INPUT_2:
rows: 2
cols: 2
rC: 0
cC: 1
OUTPUT:
[ [ 0, 1 ], [ 0, 0 ], [ 1, 1 ], [ 1, 0 ] ]
INPUT_3:
rows: 2
cols: 3
rC: 1
cC: 2
OUTPUT:
[ [ 1, 2 ], [ 0, 2 ], [ 1, 1 ], [ 0, 1 ], [ 1, 0 ], [ 0, 0 ] ]
ILLUSTRATION:
Morae! Q
- Find a triplet such that triplet is minimum of all the triplets.
- Rearrange characters in a string such that no two adjacent are same.
- Return the coordinates of all cells in matrix sorted by their distance.
- Function to verify the ISBN number from the book.
- Find out the absolute difference of values of two array integers.
- Rotate the array in the right direction by K steps.
- Find the sequence of moves that wins the game, collect exactly G coins!.
- Calculate the sum of boundary elements of a matrix.
- Find the total number of ways to reach the tree house.
- Compute profit and loss of a product.
- Find the total expense of purchased items.
- Determine the maximum sequence weight.
- Find the person’s birth year is a leap year or not.
- Find the inner rating with the displayed rating.
- Find if angles are valid to form a triangle.
- Find out the winner of the game from the given statistics.
- Verify the tag to determine if one needs to arrest or allow the vehicle.
- Find the largest hexadecimal number.
- Group up all anagrams together from given words.
- Return all anagrams from the given words.