All problems
Voxel Grid Sparsity
Problem
Compute the sparsity of a voxel grid.
Sparsity is 1 - occupied / total, where occupied is the number of unique voxels containing points and total is dims[0] * dims[1] * dims[2]. Return the value rounded to 4 decimal places, or 0.0 when total is 0.
Hint
A dense grid has sparsity 0; an empty grid has sparsity 1.
Test cases
case 1
in: [[[0.1,0.1,0.1],[1.5,1.5,1.5]],1,[2,2,2]]
expected: 0.75
case 2
in: [[],1,[4,4,4]]
expected: 1
case 3
in: [[[0,0,0]],1,[1,1,1]]
expected: 0
case 4
in: [[[0.5,0.5,0.5],[1.5,0.5,0.5],[0.5,1.5,0.5],[1.5,1.5,0.5],[0.5,0.5,1.5],[1.5,0.5,1.5],[0.5,1.5,1.5],[1.5,1.5,1.5]],1,[2,2,2]]
expected: 0
Starter codePython
def voxel_sparsity(points, size, dims):
# Your code here
pass4 test cases · run your code against them in the editor