Saturday, March 26, 2022


Step 1st:

#
#     ********* Futctions library in file LibraryList.py *************
#
def sum_two_list(x,y, nr):  #  Add two list items
    sum = [0]*nr
    i = 1
    while i < nr:
        sum[i] =  x[i] + y[i]
        i = i +1
    return sum
def sum_elem(x, nr):    # Add items to a list
    suma = 0
    i = 0
    while i < nr:
        suma = suma + x[i]
        i = i +1
    return suma
def prod_scalar(x, y, nr):   #  The scalar product of two vectors
    suma = 0
    i = 0
    while i < nr:
        suma = suma + x[i] * y[i]
        i = i +1
    return suma
def swap(x,i, j):  # Swap two elements
    y = x
    temp = y[i]
    y[i] = y[j]
    y[j] = temp
    return y

Step 2nd.
#
#   Call function from library 
#
import LibraryList
NR = 10
X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Z = LibraryList.sum_two_list(X,Y, NR)
SUMA_X = LibraryList.sum_elem(X, NR)
SUMA_Y = LibraryList.sum_elem(Y, NR)
PROD_XY = LibraryList.prod_scalar(X, Y, NR)
SWAPxij = LibraryList.swap(X,3, 7)
SWAPyij = LibraryList.swap(Y,0, 9)
print('ADD LIST ELEMENTS X:', SUMA_X)
print('ADD LIST ELEMENTS X:', SUMA_Y)
print('PRODUS LISTS ELEMENTS X, Y:', PROD_XY)
print('SWAP LIST ELEMENTS i and j from X :', SWAPxij)
print('SWAP LIST ELEMENTS i and j from Y :', SWAPyij)

Results are:

=============== RESTART: /Users/ionivan/Documents/CALLfunction.py ==============
55
55
385
[1, 2, 3, 8, 5, 6, 7, 4, 9, 10]
[10, 2, 3, 4, 5, 6, 7, 8, 9, 1]
ADD LIST ELEMENTS X: 55
ADD LIST ELEMENTS X: 55
PRODUS LISTS ELEMENTS X, Y: 385
SWAP LIST ELEMENTS i and j from X : [1, 2, 3, 8, 5, 6, 7, 4, 9, 10]
SWAP LIST ELEMENTS i and j from Y : [10, 2, 3, 4, 5, 6, 7, 8, 9, 1]

(March 26, 2022) 

No comments:

Post a Comment