Sie sind auf Seite 1von 3

6/28/2019 PathAI Machine Learning Screen :: powered by HackerRank

 
  02m : 34s
PathAI Machine Learning Screen 13/13 Attempted     Swapnil Asawa
to test end


 Weigh Your Area First

Jaccard Index is a way to measure the similarity of two finite sets and is the most used evaluation metric for object detection in images. More popularly
- Objective known as Intersection over Union (IoU) is defined exactly as the name suggests,
Assessment -

You are given a matrix of size n rows and m columns with every cell (i, j) having a weight of Wi,j. For the matrix, you’ll be given q queries which consists
3
of two rectangles where every rectangle is denoted by the top-left (i1, j1) and bottom-right (i2, j2) cell location. Both the diagonally opposite cell
locations given are inclusive. For each query, output the IoU.
4
 
Size of a rectangle is measured as,
5

8 Function Description
Complete the function weighted_iou in the editor below.
9 weighted_iou has the following parameter(s):
mat[weights0[0...m-1],...weightsn-1[0...m-1]]:  2-D array of integers with n rows and m columns
10 queries[[(i1, j1, i2, j2), (i3, j3, i4, j4)]0, ... [(i1, j1, i2, j2), (i3, j3, i4, j4)]q-1]: array of q queries with every query containing two rectangles, R1 as a tuple of (i1, j1, i2,
j2) and R2 as (i3, j3, i4, j4)
11  
weighted_iou has to return the following:
12
IoUs[IoU0, ... IoUq-1]: array of q floats with each element an IoU to the respective query.
- Programming  
Challenge -
Note: Hidden cases test your code for efficiency; brute force time complexity for each query is O(nm). Can you reduce this to linear time, or
13 even better, constant time?
 
Constraints
Assume every rectangle given is a valid rectangle and at least contains one cell.
1 <= n, m <= 1100
0 <= Wi,j <= 2^16 - 1
 

Input Format For Custom Testing

Sample Case 0

Sample Input For Custom Testing

3 3
6 7 9
2 8 1
3 4 4
2
0 0 1 1 1 1 2 2
0 0 2 2 0 2 1 2

Sample Output

0.2500000000
0.2272727273

https://www.hackerrank.com/tests/gbatgmslns3/questions/6tr471k4pln 1/3
6/28/2019 PathAI Machine Learning Screen :: powered by HackerRank

Explanation  
  02m : 34s
PathAI Machine Learning Screen 13/13 Attempted     Swapnil Asawa
to test end
Query 1
R1 = 0, 0, 1, 1

|R1| = 6 + 7 + 2 + 8 = 23
 R2 = 1, 1, 2, 2
|R2| = 8 + 1 + 4 + 4 = 17
- Objective Intersection = 8
Assessment -
Union = 6 + 7 + 2 + 8 + 1 + 4 + 4 = 32
1 Answer = Intersection / Union = 8/32 = 0.25
 
2 Query 2
R1 = 0, 0, 2, 2
3 |R1| = 6 + 7 + 9 + 2 + 8 + 1 +3 + 4 + 4 = 44
R2 = 0, 2, 1, 2
4
|R2| = 9 + 1 = 10
Intersection = 9 + 1 = 10
5
Union = 6 + 7 + 9 + 2 + 8 + 1 +3 + 4 + 4 = 44
Answer = Intersection / Union = 10/44 = 0.2272727273
6

Sample Case 1
7

YOUR ANSWER
8

9 We recommend you take a quick tour of our editor before you proceed. The timer will pause up to 90 seconds for the tour.   Start tour ✖

10

 For help on how to read input and write output in Python 3, click here. ×
11

12
Draft saved 07:38 pm View Code Diff Python 3 
- Programming
Challenge - 1 #!/bin/python3 ⋯
10  
13
11 # Complete the weighted_iou function below.
12 def areaunder(tpl,sm):
13     fin=sm[tpl[2]][tpl[3]]
14     print(tpl, fin)
15     if (tpl[0]-1)>=0:
16         fin-=sm[tpl[0]-1][tpl[3]]
17     print(tpl, fin)
18     if((tpl[1]-1)>=0):
19         fin-=sm[tpl[2]][tpl[1]-1]
20         print(tpl, fin)
21         if((tpl[0]-1)>=0):
22             fin+=sm[tpl[0]-1][tpl[1]-1]
23             print(tpl, fin)
24     return fin
25     #TODO: edges
26 def weighted_iou(mat, queries):
27     # mat[i][j] is the value in mat at row (i+1) and column (j+1)
28     # q[k] gives the (k+1)th query in the form [(i1,j1,i2,j2), (i3,j3,i4,j4)]
29     # q[k][0] is a tuple with the coordinates of the first rectangle of the (k+1)th query
30     # q[k][1] is a tuple with the coordinates of the second rectangle of the (k+1)th query
31     sm=[[0 for j in range(len(mat[0])) ]for i in range(len(mat))]
32     sm[0][0]=mat[0][0]
33     for i in range(1,len(mat)):
34         sm[i][0]=mat[i][0]+sm[i-1][0]
35     for j in range(1,len(mat[0])):
36 sm[0][j]=mat[0][j]+sm[0][j 1]

https://www.hackerrank.com/tests/gbatgmslns3/questions/6tr471k4pln 2/3
6/28/2019 PathAI Machine Learning Screen :: powered by HackerRank

  Line: 41 Col: 11
  02m : 34s
PathAI Machine Learning Screen 13/13 Attempted     Swapnil Asawa
to test end
Run Code Submit code & Continue
Test against custom input

(You can submit any number of times)

 Download sample test cases The input/output files have Unix line endings. Do not use Notepad to edit them on windows.

- Objective
Assessment -
Status: Compiled successfully. 2/12 test cases passed.
1

Test case 2 
2 Compiler Message

Wrong Answer
3 Test case 3 

4 Your Output (stdout)


Test case 4 
Output hidden
5
Test case 5 
6

Test case 6 
7

8 Test case 7 

9
Test case 8 

10
Test case 9 
11

Test case 10 
12

- Programming
Challenge -
About Privacy Policy Terms of Service

13

https://www.hackerrank.com/tests/gbatgmslns3/questions/6tr471k4pln 3/3