Skip to main content
Code Review

Return to Question

edited tags
Link
Toby Speight
  • 87.7k
  • 14
  • 104
  • 325
Post Reopened by Latika Agarwal, mdfst13, t3chb0t, Billal BEGUERADJ, Peilonrayz
edited tags
Link
t3chb0t
  • 44.6k
  • 9
  • 84
  • 190
Post Closed as "Not suitable for this site" by Gareth Rees, Mast , Billal BEGUERADJ, Sᴀᴍ Onᴇᴌᴀ , Stephen Rauch
code edited
Source Link
Latika Agarwal
  • 1.9k
  • 2
  • 23
  • 47

I am solving interview questions from here.

Problem : Given a N cross M matrix in which each row is sorted, find the overall median of the matrix. Assume N*M is odd.

Note: No extra memory is allowed. For example:

 Matrix= [1, 3, 5]
 [2, 6, 9]
 [3, 6, 9]

A = [1, 2, 3, 3, 5, 6, 6, 9, 9] Median is 5. So, output is 5.

This is my approach:

def find_median( A):
 """Returns the median value from given list"""
 list_new = []
 for i in range(1,len(A:
 for j in i)):
 list_newA[0].appendextend(jA[i])
 return (sorted(list_newA[0])).pop(len(list_newA[0])/2)

Test Cases:

assert find_median([[1,3,5],[2,5,9],[3,6,11]]) == 5
assert find_median([[0,1,1],[2,6,10],[3,5,9]]) == 3
assert find_median([[1,3,4,12,14],[1,6,9,10,15],[0,1,3,3,4]]) == 4

I am able to solve the problem but I wanted to know is there a better approach to solve this problem?

I am solving interview questions from here.

Problem : Given a N cross M matrix in which each row is sorted, find the overall median of the matrix. Assume N*M is odd.

Note: No extra memory is allowed. For example:

 Matrix= [1, 3, 5]
 [2, 6, 9]
 [3, 6, 9]

A = [1, 2, 3, 3, 5, 6, 6, 9, 9] Median is 5. So, output is 5.

This is my approach:

def find_median( A):
 """Returns the median value from given list"""
 list_new = []
 for i in A:
 for j in i:
 list_new.append(j)
 return (sorted(list_new)).pop(len(list_new)/2)

Test Cases:

assert find_median([[1,3,5],[2,5,9],[3,6,11]]) == 5
assert find_median([[0,1,1],[2,6,10],[3,5,9]]) == 3
assert find_median([[1,3,4,12,14],[1,6,9,10,15],[0,1,3,3,4]]) == 4

I am able to solve the problem but I wanted to know is there a better approach to solve this problem?

I am solving interview questions from here.

Problem : Given a N cross M matrix in which each row is sorted, find the overall median of the matrix. Assume N*M is odd.

Note: No extra memory is allowed. For example:

 Matrix= [1, 3, 5]
 [2, 6, 9]
 [3, 6, 9]

A = [1, 2, 3, 3, 5, 6, 6, 9, 9] Median is 5. So, output is 5.

This is my approach:

def find_median( A):
 """Returns the median value from given list"""
 for i in range(1,len(A)):
 A[0].extend(A[i])
 return (sorted(A[0])).pop(len(A[0])/2)

Test Cases:

assert find_median([[1,3,5],[2,5,9],[3,6,11]]) == 5
assert find_median([[0,1,1],[2,6,10],[3,5,9]]) == 3
assert find_median([[1,3,4,12,14],[1,6,9,10,15],[0,1,3,3,4]]) == 4

I am able to solve the problem but I wanted to know is there a better approach to solve this problem?

Post Undeleted by Latika Agarwal
Post Deleted by Latika Agarwal
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 479
Loading
Source Link
Latika Agarwal
  • 1.9k
  • 2
  • 23
  • 47
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /