#Quiz07

Quiz 7

In mathematics, the dot product, or scalar product (or sometimes inner product in the
context of Euclidean space), is an algebraic operation that takes two equal-length
sequences of numbers (usually coordinate vectors) and returns a single number.Algebraically, it is the sum of the products of the corresponding entries of the two
sequences of numbers

Create a function called dot_product that receives two lists of numbers (say list1 and
list2). The function returns what is the dot product of the two lists.

For full marks, if the lists are not the same size, then the function should return the
special value of NaN (which represents not a number).

  • For Python, you can create this value with the expression: float(‘NaN’)

Example. If the input is [2,4,5,6] and [1,2,3,4] the result will be 49 since (2*1)+(4*2)+(5*3)+(6*4) = 49

 

Gracias a este programa podemos hacer el producto de dos listas, es muy simple ya que solamente multiplicamos cada numero de la lista uno por uno, pero existía una excepción, que si el tamaño de las listas no eran iguales, entonces no se podría realizar el producto de las listas, para lo que hice un condicional que se encarga de comparar el tamaño de las listas y te dice si son iguales o no, para saber el tamaño de la lista use la función len de no serlo retorna NaN(Not a Number). En este programa utilice por primera vez un ciclo for doble, para realizar la multiplicación de las listas.

Aquí esta mi código:

import os
os.system(“clear”)
print(“scalar product”)
lista1=[1,2,3,4]
lista2=[2,4,5,6]

def dot_product(var1,var2):
acum=0
if(len(var1)==len(var2)):
for l1,l2 in zip(var1,var2):
suma=l1*l2
acum=acum+suma
print(acum)
else:
print(“NaN”)

dot_product(lista1,lista2)

Por aquí el enlace a GitHub

Por acá el programa funcionando:

ESTEYAESTETAMBIEN

 

CC BY-SA 4.0 #Quiz07 by carlosdanielmartinezblog is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.