Problem 100693 · hard · Level 01 Prerequisites & Setup

What Is New in Each Feature

vectors · dot product · projection · orthogonality · normalising

A data team adds features to a model one at a time. Every feature is a vector with one value per example, and all features have the same length. A new feature is only useful as far as it contains something the earlier features do not: any part of it that can be made by adding up multiples of the earlier features (with any weights, positive, negative or fractional) is nothing new.

For every feature, in order, split it into two parts: the part that can be made from the earlier features, and the remainder that cannot. Write whats_new(features) that returns the list of the Euclidean lengths of the remainders.

The first feature has no earlier features, so its remainder is the whole feature. Treat a remainder shorter than 1e-9 as exactly zero: such a feature adds nothing new.

Examples

Input:  features = [[3, 0, 0], [1, 2, 0], [4, 4, 0], [1, 1, 5]]
Output: [3.0, 2.0, 0.0, 5.0]
Explanation: [1, 2, 0] is 1/3 of the first feature plus the new part [0, 2, 0].
[4, 4, 0] = 2/3 · [3, 0, 0] + 2 · [1, 2, 0] is nothing new. [1, 1, 5] brings the new part [0, 0, 5].

Input:  features = [[2, 2], [3, 1]]
Output: [2.8284271247461903, 1.4142135623730951]
Explanation: the part of [3, 1] along [2, 2] is [2, 2] itself; the remainder is [1, -1].

Constraints

  • 1 <= len(features) <= 30, all features have the same length m with 1 <= m <= 200
  • values are whole numbers between -100 and 100
  • answers are compared with a tolerance of 1e-6

Goals

  • Remove from a vector the part that points along another vector
  • Build perpendicular unit directions one at a time and reuse them
  • Decide when a remainder is zero within floating-point error
Starting Python…