0% found this document useful (0 votes)
15 views

Unit-4 Lab Questions

Uploaded by

lt9731948
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 views

Unit-4 Lab Questions

Uploaded by

lt9731948
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/ 3

9/22/24, 3:43 PM 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

108 def perimeter(self):


109 return 2 * 3.14 * self.radius
110
111 class Triangle(Shape):
112 def __init__(self, a, b, c):
113 self.a = a
114 self.b = b
115 self.c = c
116
117 def area(self):
118 s = (self.a + self.b + self.c) / 2
119 return (s * (s - self.a) * (s - self.b) * (s - self.c)) ** 0.5
120
121 def perimeter(self):
122 return self.a + self.b + self.c
123
124 class Square(Shape):
125 def __init__(self, side):
126 self.side = side
127
128 def area(self):
129 return self.side ** 2
130
131 def perimeter(self):
132 return 4 * self.side
133
134 # Example usage
135 circle = Circle(5)
136 print("Circle area:", circle.area())
137 print("Circle perimeter:", circle.perimeter())
138
139 triangle = Triangle(3, 4, 5)
140 print("Triangle area:", triangle.area())
141 print("Triangle perimeter:", triangle.perimeter())
142
143 square = Square(4)
144 print("Square area:", square.area())
145 print("Square perimeter:", square.perimeter())
146
147 __________________________________________________________________________
148
149

https://tarikjaber.github.io/Code-to-PDF/ 3/3

You might also like