-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAss2.py
More file actions
152 lines (95 loc) · 3.99 KB
/
Ass2.py
File metadata and controls
152 lines (95 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Question 1
def create_and_read_file():
with open("example.txt", "w") as file:
file.write("This is a sample text.\nIt contains multiple lines for testing purposes.\n")
with open("example.txt", "r") as file:
content = file.read()
print("Content of 'example.txt':\n", content)
# Question 2
def read_specified_characters():
with open("sample.txt", "w") as file:
file.write("This is a test file for reading specific characters.\n")
num_chars = int(input("Enter the number of characters to read: "))
with open("sample.txt", "r") as file:
content = file.read(num_chars)
print("First {num_chars} characters of 'sample.txt':\n", content)
# Question 3
def append_and_read_file():
with open("sample.txt", "a") as file:
file.write("This is an appended line.\n")
with open("sample.txt", "r") as file:
content = file.read()
print("Updated content of 'sample.txt':\n", content)
# Question 4
def count_lines_not_starting_with_t():
count = 0
with open("text.txt", "r") as file:
for line in file:
if not line.lstrip().startswith("T"):
count += 1
print(f"Number of lines not starting with 'T': {count}")
# Question 5
def copy_file_content():
with open("source.txt", "r") as source_file:
content = source_file.read()
with open("destination.txt", "w") as dest_file:
dest_file.write(content)
print("Content copied to 'destination.txt' successfully.")
import numpy as np
# Question 6
def transpose_and_flatten_matrix():
rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))
print("Enter the matrix elements row by row (space-separated):")
matrix = []
for i in range(rows):
for j in range(cols):
matrix[i][j]=int(input("Enter the matrix element"))
matrix = np.array(matrix)
print("Transpose of the matrix:\n", matrix.T)
print("Flattened version of the matrix:\n", matrix.flatten())
# Question 7
def array_properties():
array = np.array([[10, 16, 71, 9], [71, 91, 31, 51]])
print("Dimension of the array:", array.ndim)
print("Shape of the array:", array.shape)
print("Size (total elements) of the array:", array.size)
# Question 8
def random_array_operations():
array1 = np.random.randint(1, 10, (3, 3))
array2 = np.random.randint(1, 10, (3, 3))
print("Array 1:\n", array1)
print("Array 2:\n", array2)
concatenated = np.concatenate((array1, array2), axis=0)
print("Concatenated array:\n", concatenated)
print("Sorted Array 1:\n", np.sort(array1))
print("Sorted Array 2:\n", np.sort(array2))
print("Element-wise addition:\n", array1 + array2)
print("Element-wise subtraction:\n", array1 - array2)
print("Element-wise multiplication:\n", array1 * array2)
print("Element-wise division:\n", np.divide(array1, array2, where=array2 != 0))
# Question 9
def matrix_min_max():
matrix = np.random.randint(1, 100, (8, 7))
print("Random matrix (8x7):\n", matrix)
max_values = matrix.max(axis=0)
min_values = matrix.min(axis=0)
print("Maximum values in each column:\n", max_values)
print("Minimum values in each column:\n", min_values)
# Question 10
def numpy_tasks():
matrix = np.arange(1, 21).reshape(4, 5)
print("4x5 matrix:\n", matrix)
zeros = np.zeros(10)
ones = np.ones(10,1)
fives = np.full(10, 5)
print("Array of 10 zeros:", zeros)
print("Array of 10 ones:", ones)
print("Array of 10 fives:", fives)
even_numbers = np.arange(10, 51, 2)
print("Even integers from 10 to 50:\n", even_numbers)
random_number = np.random.rand()
print("Random number between 0 and 1:", random_number)
np.savetxt("matrix.txt", matrix, fmt="%d")
loaded_matrix = np.loadtxt("matrix.txt", dtype=int)
print("Matrix loaded from 'matrix.txt':\n", loaded_matrix)