0% found this document useful (0 votes)
15 views9 pages

22053686-Ad Lab-3-0.5

The document outlines a series of programming assignments for a course on Applications Development Laboratory, focusing on the use of NumPy in Python. Each assignment includes a specific task, such as creating vectors, manipulating arrays, and verifying properties of matrices, along with the corresponding source code. The document serves as a guide for individual work in the lab, detailing various programming challenges to enhance skills in numerical computing.

Uploaded by

madhavgouniyal04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views9 pages

22053686-Ad Lab-3-0.5

The document outlines a series of programming assignments for a course on Applications Development Laboratory, focusing on the use of NumPy in Python. Each assignment includes a specific task, such as creating vectors, manipulating arrays, and verifying properties of matrices, along with the corresponding source code. The document serves as a guide for individual work in the lab, detailing various programming challenges to enhance skills in numerical computing.

Uploaded by

madhavgouniyal04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Applications Development Laboratory (CS33002), Spring 202

Applications Development Laboratory (ADL)


Individual Work
Lab. No: 3 , Assignment No.: 0.5, Date:16.12.2024, Day: Monday
Topic: NumPy

Roll Number: 22053686 Branch/Section: CSE-5


Name in Capital: DIBYANSHU ROY

Program No: 05.1

Program Title: Write a program to create a null vector of size 25 but the values from
fifth to tenth elements are all 1.

Input/Output Screenshots:

Source code
import numpy as np

vector = np.zeros(25)
vector[4:10] = 1
print(vector)

Program No: 05.2

Program Title: Write a program to create a vector with values ranging from 5 to 20
& reverse it

Input/Output Screenshots:

Page 1
Applications Development Laboratory (CS33002), Spring 202
Source code
import numpy as np

vector = np.arange(5, 21)


reversed_vector = vector[::-1]

print(reversed_vector)

Program No: 05.3

Program Title: Write a program to create a vector with 10 random values &amp sort
first half ascending &amp second half descending.

Input/Output Screenshots:

Source code
import numpy as np

vector = np.random.random(10)
first_half_sorted = np.sort(vector[:5])
second_half_sorted = np.sort(vector[5:])[::-1]

sorted_vector = np.concatenate((first_half_sorted, second_half_sorted))

print(sorted_vector)

Program No: 05.4

Program Title: Write a program to create a vector of size 10 with values ranging from
0 to 1, both excluded.

Input/Output Screenshots:

Page 2
Applications Development Laboratory (CS33002), Spring 202

Source code
import numpy as np

vector = np.linspace(0, 1, 12)[1:-1]

print(vector)

Program No: 05.5

Program Title: Write a program to concatenate element-wise two arrays of string.

Input/Output Screenshots:

Source code
import numpy as np

array1 = np.array(["Hello", "Good", "Nice", "Python", "Is"])


array2 = np.array(["World", "Morning", "To", "Programming", "Great"])

concatenated_array = np.core.defchararray.add(array1, array2)

print(concatenated_array)

Program No: 05.6

Program Title: Write a program to create a 4x3 array with random values & find
out the minimum & maximum values.

Page 3
Applications Development Laboratory (CS33002), Spring 202
Input/Output Screenshots:

Source code
import numpy as np

array = np.random.random((4, 3))

min_value = np.min(array)
max_value = np.max(array)

print("Array:")
print(array)
print("\nMinimum value:", min_value)
print("Maximum value:", max_value)

Program No: 05.7

Program Title: Write a program to create a 2D array with 1 on the border and 0 inside.

Input/Output Screenshots:

Page 4
Applications Development Laboratory (CS33002), Spring 202

Source code
import numpy as np

def create_border_array(rows, cols):


array = np.zeros((rows, cols), dtype=int)

array[0, :] = 1
array[-1, :] = 1
array[:, 0] = 1
array[:, -1] = 1

return array

rows, cols = 5, 5

array = create_border_array(rows, cols)


print(array)

Program No: 05.8

Program Title: Write a program to create a 4x4 matrix with values 1,2,3,4 just below
& above the diagonal, rest zeros.

Input/Output Screenshots:

Page 5
Applications Development Laboratory (CS33002), Spring 202

Source code
import numpy as np

matrix = np.zeros((4, 4), dtype=int)

np.fill_diagonal(matrix[1:], [1, 2, 3])


np.fill_diagonal(matrix[:, 1:], [1, 2, 3])

print(matrix)

Program No: 05.9

Program Title: Write a program to extract all the contiguous 3x3 blocks from a random
10x10 matrix.

Input/Output Screenshots:

Page 6
Applications Development Laboratory (CS33002), Spring 202

Source code
import numpy as np

matrix = np.random.random((10, 10))

blocks = []
for i in range(matrix.shape[0] - 2):
for j in range(matrix.shape[1] - 2):
block = matrix[i:i+3, j:j+3]
blocks.append(block)

print("Total number of 3x3 blocks:", len(blocks))


for i, block in enumerate(blocks):
print(f"\nBlock {i+1}:")
print(block)

Program No: 05.10

Program Title: A magic square is a matrix all of whose row sums, column sums and the
sums of the two diagonals are the same. (One diagonal of a matrix goes from the top
left to the bottom right, the other diagonal goes from top right to bottom left.)
Show by direct computation that if the matrix A is given by
A=np.array([[17, 24, 1, 8, 15],
[23, 5, 7, 14, 16],
[ 4, 6, 13, 20, 22],
[10, 12, 19, 21, 3],
[11, 18, 25, 2, 9]])
Page 7
Applications Development Laboratory (CS33002), Spring 202

The matrix A has 5 row sums (one for each row), 5 column sums (one for each column)
and two diagonal sums. These 12 sums should all be exactly the same, and you could
verify that they are the same by printing them and “seeing” that they are the
same. It is easy to miss small differences among so many numbers, though. Instead,
verify that A is a magic square by constructing the 5 column sums and computing the
maximum and minimum values of the column sums. Do the same for the 5 row sums,and
compute the two diagonal sums. Check that these six values are the same. If the
maximum and
minimum values are the same, the flyswatter principle says that all values are the
same.
Hints: The function np.diag extracts the diagonal of a matrix, and the function
np.fliplr extracts the other
diagonal.

Input/Output Screenshots:

Page 8
Applications Development Laboratory (CS33002), Spring 202

Source code
import numpy as np

A = np.array([[17, 24, 1, 8, 15],


[23, 5, 7, 14, 16],
[4, 6, 13, 20, 22],
[10, 12, 19, 21, 3],
[11, 18, 25, 2, 9]])

row_sums = np.sum(A, axis=1)


col_sums = np.sum(A, axis=0)

main_diagonal_sum = np.sum(np.diag(A))
anti_diagonal_sum = np.sum(np.diag(np.fliplr(A)))

all_sums = np.concatenate([row_sums, col_sums, [main_diagonal_sum, anti_diagonal_sum]])

max_sum = np.max(all_sums)
min_sum = np.min(all_sums)

print("Row sums:", row_sums)


print("Column sums:", col_sums)
print("Main diagonal sum:", main_diagonal_sum)
print("Anti diagonal sum:", anti_diagonal_sum)

print("\nMax sum:", max_sum)


print("Min sum:", min_sum)

if max_sum == min_sum:
print("\nThe matrix is a magic square.")
else:
print("\nThe matrix is not a magic square.")

Page 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy