Problem 123295 · easy · Level 01 Prerequisites & Setup

Songs Within Reach

Euclidean distance · datasets · sorting · ranking

A music app describes every song by a vector of four whole numbers: [tempo, energy, acoustic, vocals]. When you like a song, it looks for songs that are within reach of it: songs whose Euclidean distance to the liked song is at most radius.

Write within_reach(liked, songs, radius) that returns the indices of all songs within reach, from the closest to the furthest. Songs at the same distance are listed by index, smallest first. The liked song may itself be in the list (at distance 0); it is included like any other song.

The tests build large song tables with song_table(n, seed), which is available in your code, so you can look at one with Run.

Examples

Input:  liked = [120, 80, 20, 40]
        songs = [[118, 75, 25, 45], [70, 20, 85, 60], [124, 83, 20, 40], [120, 80, 20, 50]]
        radius = 10
Output: [2, 0, 3]
Explanation: the distances are about 8.89, 103.56, 5.0 and 10.0. Song 3 is exactly at the
radius, so it counts; song 1 is a quiet acoustic song far away.

Input:  liked = [100, 50, 50, 50], songs = [[100, 50, 50, 50], [101, 50, 50, 50], [99, 50, 50, 50]], radius = 0
Output: [0]

Constraints

  • 0 <= len(songs) <= 10**4; every vector has 4 whole numbers between 0 and 300
  • radius is a whole number with 0 <= radius <= 500

Goals

  • Measure the distance from a query to every row of a dataset
  • Keep only the rows within a radius, comparing squared distances
  • Order the matches from the most to the least similar with a clear tie rule
Starting Python…