Exemple #1
0
How is it still possible, then, that our implementation of scale changes the
actual parameter sent by the caller?
""")

import sys
sys.path.append("..")

from others.helpers import get_list_integers


def scale(data, factor):
    for j in range(len(data)):
        data[j] *= factor


data = get_list_integers()

print(data)
scale(data, 3)
print(data)

print("""
In fact, numeric types are immutable, but scale function handles
a mutable parameter, which is a List, and this is why this parameter changes.
""")


def test(just_a_int, factor):
    just_a_int *= factor

Exemple #2
0
print("""
The p-norm of a vector v = (v1,v2,...,vn) in n-dimensional space is defined
as [see book]

For the special case of p = 2, this results in the traditional Euclidean
norm, which represents the length of the vector. For example, the Euclidean norm of a two-dimensional vector with coordinates (4,3) has a
Euclidean norm of √42 + 32 = √16+9 = √25 = 5.

Give an implementation of a function named norm such that norm(v, p) returns the p-norm
value of v and norm(v) returns the Euclidean norm of v. You may assume
that v is a list of numbers.
""")

import sys
sys.path.append("..")

from others.helpers import get_list_integers


def norm(v, p=2):
    return (sum(i**p for i in v))**(1. / p)


int_list = get_list_integers()

print(norm(int_list, 1))
print(norm(int_list))
print(norm(int_list, 3))
Exemple #3
0
print("""
Write a short Python program that takes two arrays a and b of length n
storing int values, and returns the dot product of a and b. That is, it returns
an array c of length n such that c[i] = a[i] · b[i], for i = 0,...,n−1.
""")

import sys
sys.path.append("..")

from others.helpers import get_list_integers

print("Define the first array")
a_arr = get_list_integers()

print("Define the second array")
b_arr = get_list_integers()

prod = [a_arr[i] * b_arr[i] for i in range(len(a_arr))]

print("Dot Product (Vector) = {0}".format(prod))
print("Dot Product = {0}".format(sum(prod)))