# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# ## Week 0 Assignement
# _<NAME> - **L3C1**_

# Write a program to store the values of the matrix given below in a 2D list.
# Then calculate
# 1. The sum of the diagonal elements
# 2. The sum of all elements above the diagonal and the sum of all the elements below the diagonal
# 3. The sum of all the numbers in the rows of the matrix
# 4. The sum of all the columns of the matrix
# 5. Find out the max and min element of each column.
# <br/><br/>
# a = [[2 3 4]
#      [1 5 6]
#      [5 8 5]]

# **Creating the list/ array**

# importing the numpy module
import numpy as np

# storing the matrix in a numpy array
matrix = np.array([2,3,4,1,5,6,5,8,5])
# reshaping the array into a 3x3 array
matrix = matrix.reshape(3,3)

# The array must be of shape 3x3
print(matrix)

# **Question 1**

# _Finding the sum of all diagonal elements by iterating through the matrix_

# +
sum = 0 # this variable will store the sum

for row in range(len(matrix)):
    for column in range(len(matrix[row])):
        if row == column:
            sum += matrix[row][column]
# -

print(sum) # checking the result

# _Finding the sum of all diagonal elements by using ndenumerate and checking diaginal indices_

sum2 = 0 # this variable will store the sum
for index, element in np.ndenumerate(matrix):
    """
        the index variable will store a tuple which contains
        the 2D index of the element, which is stored in the 
        variable element
    """
    if (index[0] == index[1]):
        sum2 += element
        

print(sum2)

# **Question 2**

# using ndenumerate to iterate through the 2D array
sum_of_elements_above, sum_of_elements_below = 0, 0
for index, element in np.ndenumerate(matrix):
    # checking if the element lies above the diagonal [row < column]
    if index[0] < index[1]:
        sum_of_elements_above += element
    # checking if the element lies below the diagonal [row > column]
    elif index[0] > index[1]:
        sum_of_elements_below += element
        

print (f'The sum of elements above the diagonals is: {sum_of_elements_above}\nThe sum of elements below the diagonal is: {sum_of_elements_below}')

# **Question 3**

# Storing the sum of rows in a list will address the case when the number of rows is unknown
sum_of_rows = list()
# Iterating through each row in the matrix and stroing the value in var row
for row in matrix:
    """
        You can iterate through the row and add the values individually,
        then you can store the value in a variable or push it into the
        list.
        Alternatively, you can use the numpy.sum() function to calculate the 
        sum of all elements of the row, this way you save coding time and 
        don't have to reinvent the wheel.
    """
    sum_of_rows.append(np.sum(row))

# Printing out the sum of the rows
for index in range(len(sum_of_rows)):
    print(f'The sum of the row {index + 1} is {sum_of_rows[index]}.')

# **Question 4**

# Creating a numpy array of length equal to the number of column of the matrix
# This will store the sum of the columns
sum_of_columns = np.full(len(matrix[0]), 0)
# Iterating through each row of the matrix
for row in matrix:
    """
        iterating through the columns of the row and adding them to the values
        on the sum of columns at respective place
    """
    for column in range(len(row)):
        sum_of_columns[column] += row[column]

# Printing out the sum of the rows
for index in range(len(sum_of_columns)):
    print(f'The sum of the column {index + 1} is {sum_of_columns[index]}.')

# **Question 5**

# Creating a numpy array of length equal to the number of column of the matrix
# This will store the min and max value of each columns in a nx2 where the
# element of col 0 is the min value and col 1 is the max value
min_and_max_val = np.full((len(matrix[0]), 2), [100,0])
# Iterating through each row of the matrix
for row in matrix:
    """
        iterating through the columns of the row and checking if
        it is the max value and min value
    """
    for column in range(len(row)):
        # Checking for min value
        if row[column] < min_and_max_val[column][0]:
            min_and_max_val[column][0] = row[column]
        
        # checking for max value
        if row[column] > min_and_max_val[column][1]:
            min_and_max_val[column][1] = row[column]

# printing the min and max values
for index in range(len(min_and_max_val)):
    print(f'In column {index+1}:\n\tMinimum Value: {min_and_max_val[index][0]}\n\tMaximun Value: {min_and_max_val[index][1]}')



StarcoderdataJupyterScriptsDedupFiltered