PIXELBANKv9.1.0
Menu

Implement 1D convolution (valid mode, no padding).

Given a 1D signal and a kernel, slide the kernel across the signal and compute the dot product at each position:

output[i]=∑j=0k−1signal[i+j]⋅kernel[j]\text{output}[i] = \sum_{j=0}^{k-1} \text{signal}[i+j] \cdot \text{kernel}[j]

Return the convolution result, rounded to 4 decimal places.

Example:

Input:
signal = [1, 2, 3, 4, 5]
kernel = [1, 0, -1]
Output:
[-2, -2, -2]
Reasoning:
  • The kernel [1, 0, -1] is slid across the signal [1, 2, 3, 4, 5], and at each position, the dot product is computed.
  • At position i=0, the computation is: 1∗1+2∗0+3∗(−1)=1+0−3=−21*1 + 2*0 + 3*(-1) = 1 + 0 - 3 = -2.
  • This process is repeated for i=1 and i=2, yielding the same result of -2 due to the signal and kernel values: 2∗1+3∗0+4∗(−1)=2+0−4=−22*1 + 3*0 + 4*(-1) = 2 + 0 - 4 = -2 and 3∗1+4∗0+5∗(−1)=3+0−5=−23*1 + 4*0 + 5*(-1) = 3 + 0 - 5 = -2.
  • The final output is [-2, -2, -2], which are the results of the convolution operation at each valid position, rounded to 4 decimal places.

Constraints:

  • signal: 1D list of numbers
  • kernel: 1D list of numbers (length <= len(signal))
  • Return 1D list of length (len(signal) - len(kernel) + 1)
  • Round each value to 4 decimal places
solution.py

Test Results

0/0
Run code to see test results.