Unit-4 Lab Questions
Unit-4 Lab Questions
1 Unit-4
2 __________________________________________________________________________
3
4 1. Sort Words in a File and Write to Another File
5 __________________________________________________________________________
6
7 def sort_words(input_file, output_file):
8 with open(input_file, 'r') as infile:
9 words = infile.read().lower().split()
10 words.sort()
11 with open(output_file, 'w') as outfile:
12 outfile.write(' '.join(words))
13
14 # Example usage
15 sort_words('source.txt', 'sorted.txt')
16 __________________________________________________________________________
17 2. Print Each Line of a File in Reverse Order
18 __________________________________________________________________________
19
20 def reverse_lines(input_file):
21 with open(input_file, 'r') as file:
22 lines = file.readlines()
23 for line in reversed(lines):
24 print(line.strip()[::-1])
25
26 # Example usage
27 reverse_lines('source.txt')
28 __________________________________________________________________________
29 3. Compute the Number of Characters, Words, and Lines in a File
30 __________________________________________________________________________
31
32 def count_file_contents(file_name):
33 num_chars = num_words = num_lines = 0
34 with open(file_name, 'r') as file:
35 for line in file:
36 num_lines += 1
37 num_chars += len(line)
38 num_words += len(line.split())
39 print(f"Characters: {num_chars}")
40 print(f"Words: {num_words}")
41 print(f"Lines: {num_lines}")
42
43 # Example usage
44 count_file_contents('source.txt')
45 __________________________________________________________________________
46 4. Create, Display, Append, Insert, and Reverse the Order of Items in an
Array
47 __________________________________________________________________________
48
49 def array_operations():
50 arr = [1, 2, 3, 4, 5]
51 print("Original array:", arr)
52
https://tarikjaber.github.io/Code-to-PDF/ 1/3
9/22/24, 3:43 PM Unit-4 lab Questions
53 # Append
54 arr.append(6)
55 print("After appending 6:", arr)
56
57 # Insert
58 arr.insert(2, 10)
59 print("After inserting 10 at index 2:", arr)
60
61 # Reverse
62 arr.reverse()
63 print("After reversing:", arr)
64
65 # Example usage
66 array_operations()
67 __________________________________________________________________________
68 5. Add, Transpose, and Multiply Two Matrices
69 __________________________________________________________________________
70
71 import numpy as np
72
73 def matrix_operations():
74 A = np.array([[1, 2], [3, 4]])
75 B = np.array([[5, 6], [7, 8]])
76
77 # Addition
78 C = A + B
79 print("Addition of A and B:\n", C)
80
81 # Transpose
82 print("Transpose of A:\n", A.T)
83
84 # Multiplication
85 D = np.dot(A, B)
86 print("Multiplication of A and B:\n", D)
87
88 # Example usage
89 matrix_operations()
90 __________________________________________________________________________
91 6. Create a Class that Represents a Shape
92 __________________________________________________________________________
93
94 class Shape:
95 def area(self):
96 pass
97
98 def perimeter(self):
99 pass
100
101 class Circle(Shape):
102 def __init__(self, radius):
103 self.radius = radius
104
105 def area(self):
106 return 3.14 * self.radius ** 2
107
https://tarikjaber.github.io/Code-to-PDF/ 2/3
9/22/24, 3:43 PM Unit-4 lab Questions
https://tarikjaber.github.io/Code-to-PDF/ 3/3