Tous les défis
Facile 8 résolus 44.4 % de réussite

Dot Product of Two Vectors

Multiply two vectors element by element and add the results: the core operation of every neuron.

Ce que vous allez apprendre

  • Multiply vectors element by element and sum
  • Understand the core operation of a neuron
  • Work with number arrays

Problème

Afficher la traduction

Every neuron starts with the same calculation: multiply each input by its weight and add everything up. That is the dot product of two vectors:

a · b = a₁×b₁ + a₂×b₂ + … + aₙ×bₙ

Implement dot_product(a, b).

Input

a, b: Two lists of numbers with the same length.

Output

Return the dot product as a decimal number.

Contraintes

1 ≤ length of a = length of b ≤ 1,000

-1,000 ≤ each value ≤ 1,000

Exemples

Exemple 1
Entrée :
a = [1.0, 2.0, 3.0]
b = [4.0, 5.0, 6.0]

Sortie attendue :
expected = 32.0
Entrée :
std::vector<double> a = {1.0, 2.0, 3.0};
std::vector<double> b = {4.0, 5.0, 6.0};

Sortie attendue :
double expected = 32.0;
Entrée :
double[] a = new double[] {1.0, 2.0, 3.0};
double[] b = new double[] {4.0, 5.0, 6.0};

Sortie attendue :
double expected = 32.0;

Explication

1×4 + 2×5 + 3×6 = 4 + 10 + 18 = 32.

Exemple 2
Entrée :
a = [0.5, -1.5]
b = [2.0, 4.0]

Sortie attendue :
expected = -5.0
Entrée :
std::vector<double> a = {0.5, -1.5};
std::vector<double> b = {2.0, 4.0};

Sortie attendue :
double expected = -5.0;
Entrée :
double[] a = new double[] {0.5, -1.5};
double[] b = new double[] {2.0, 4.0};

Sortie attendue :
double expected = -5.0;

Explication

0.5×2 + (-1.5)×4 = 1 - 6 = -5.

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Multiply matching positions from the two lists and combine the results into one total.

Indice 2

A single pass is enough because each pair contributes independently to the final value.

Indice 3

Make sure the lists have the same length, and keep the accumulator in a numeric type that preserves decimals if needed.

Complexité attendue

O(n) time, O(1) extra space

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

# Codeur Langage Temps Mémoire Note de l'IA Solution
1 ННикита Python 792 ms - 5/5 Voir le code
2 ゼゼロダニ Python 812 ms - 4/5 Voir le code
3 HJhettory Jurash Python 840 ms - 5/5 Voir le code
4 KKKaneki Ken Python 862 ms - 4/5 Voir le code
5 RARichard Adjei Python 1,252 ms - 4/5 Voir le code
6 FSFilippo Sanguineti C++ 3,126 ms - 4/5 Voir le code
7 PTphúc thịnh phạm C++ 3,240 ms - 3/5 Voir le code
8 ·𝓕·.★·.·⍣·.·★ 𝓕𝓪𝓫𝓲𝓸𝓵𝓪 𝓔𝓵𝓮𝓷𝓪 ★·.·⍣·.·★.· C++ 7,069 ms - 4/5 Voir le code

Apprenez des autres

Solutions de la communauté

Attention spoiler : 8 solutions complètes ci-dessous Essayez d'abord de le résoudre vous-même. Vous pouvez toujours jeter un œil au code des autres développeurs et aux avis IA. Connectez-vous et résolvez le défi pour rejoindre la discussion.