Skip to content

Commit c441e49

Browse files
committed
Create main.py
1 parent 43e073d commit c441e49

File tree

1 file changed

+286
-0
lines changed

1 file changed

+286
-0
lines changed

Theory/main.py

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
# Sieve of Eratosthenes: generate prime numbers with the help of an algorithm given by the Greek Mathematician named Eratosthenes, whose algorithm is known as Sieve of Eratosthenes.
2+
# File I/O : File input and output operations in Python Programming
3+
# Exceptions and Assertions
4+
# Modules : Introduction , Importing Modules ,
5+
# Abstract Data Types : Abstract data types and ADT interface in Python Programming.
6+
# 08
7+
# V
8+
from curses import pair_number
9+
10+
11+
def sieve_of_eratosthenes(n):
12+
prime_list = []
13+
for i in range(2, n + 1):
14+
if i not in prime_list:
15+
prime_list.append(i)
16+
for j in range(i * 2, n + 1, i):
17+
if j not in prime_list:
18+
prime_list.append(j)
19+
return prime_list
20+
21+
print(sieve_of_eratosthenes(100))
22+
23+
class file_io:
24+
def __init__(self, file_name):
25+
self.file_name = file_name
26+
self.file_object = open(file_name, "r")
27+
self.file_content = self.file_object.read()
28+
self.file_object.close()
29+
30+
def read_file(self):
31+
return self.file_content
32+
33+
def write_file(self, content):
34+
self.file_object = open(self.file_name, "w")
35+
self.file_object.write(content)
36+
self.file_object.close()
37+
38+
def append_file(self, content):
39+
self.file_object = open(self.file_name, "a")
40+
self.file_object.write(content)
41+
self.file_object.close()
42+
43+
def __del__(self):
44+
self.file_object.close()
45+
46+
file_io_object = file_io("test.txt")
47+
file_io_object.write_file("Hello World")
48+
file_io_object.append_file("\nHello World")
49+
print(file_io_object.read_file())
50+
del file_io_object
51+
52+
53+
def exeptions_and_assertions():
54+
try:
55+
assert 1 == 2
56+
except AssertionError:
57+
print("Assertion Error")
58+
except Exception as e:
59+
print(e)
60+
61+
exeptions_and_assertions()
62+
class adt():
63+
def __init__(self, name):
64+
self.name = name
65+
66+
def __str__(self):
67+
return self.name
68+
69+
def __repr__(self):
70+
return self.name
71+
72+
def __eq__(self, other):
73+
return self.name == other.name
74+
75+
def __hash__(self):
76+
return hash(self.name)
77+
78+
79+
class adt_list(list):
80+
def __init__(self, name):
81+
self.name = name
82+
83+
def __str__(self):
84+
return self.name
85+
86+
def __repr__(self):
87+
return self.name
88+
89+
def __eq__(self, other):
90+
return self.name == other.name
91+
92+
def __hash__(self):
93+
return hash(self.name)
94+
95+
adt_list_object = adt_list("adt_list")
96+
print(adt_list_object)
97+
print(adt_list_object == adt_list_object)
98+
print(adt_list_object == adt_list("adt_list"))
99+
print(adt_list_object == adt_list("adt_list2"))
100+
101+
# Classes : Class definition and other operations in the classes , Special Methods ( such as _init_,
102+
# _str_, comparison methods and Arithmetic methods etc.) , Class Example , Inheritance , Inheritance
103+
# and OOP.
104+
105+
def classes():
106+
class Person:
107+
def __init__(self, name, age):
108+
self.name = name
109+
self.age = age
110+
111+
def __str__(self):
112+
return self.name
113+
114+
def __repr__(self):
115+
return self.name
116+
117+
def __eq__(self, other):
118+
return self.name == other.name
119+
120+
def __hash__(self):
121+
return hash(self.name)
122+
123+
class Student(Person):
124+
def __init__(self, name, age, grade):
125+
super().__init__(name, age)
126+
self.grade = grade
127+
128+
def __str__(self):
129+
return self.name + " " + str(self.grade)
130+
131+
def __repr__(self):
132+
return self.name + " " + str(self.grade)
133+
134+
class Teacher(Person):
135+
def __init__(self, name, age, salary):
136+
super().__init__(name, age)
137+
self.salary = salary
138+
139+
def __str__(self):
140+
return self.name + " " + str(self.salary)
141+
142+
def __repr__(self):
143+
return self.name + " " + str(self.salary)
144+
145+
class Course:
146+
def __init__(self, name, teacher, students):
147+
self.name = name
148+
self.teacher = teacher
149+
self.students = students
150+
151+
def __str__(self):
152+
return self.name + " " + str(self.teacher) + " " + str(self.students)
153+
154+
def __repr__(self):
155+
return self.name + " " + str(self.teacher) + " " + str(self.students)
156+
157+
class School:
158+
def __init__(self, name, courses):
159+
self.name = name
160+
self.courses = courses
161+
162+
def __str__(self):
163+
return self.name + " " + str(self.courses)
164+
165+
def __repr__(self):
166+
return self.name + " " + str(self.courses)
167+
168+
class StudentCourse(Course):
169+
def __init__(self, name, teacher, students, grade):
170+
super().__init__(name, teacher, students)
171+
self.grade = grade
172+
173+
def __str__(self):
174+
return self.name + " " + str(self.teacher) + " " + str(self.students) + " " + str(self.grade)
175+
176+
def __repr__(self):
177+
return self.name + " " + str(self.teacher) + " " + str(self.students) + " " + str(self.grade)
178+
179+
class SchoolCourse(Course):
180+
def __init__(self, name, teacher, students, grade):
181+
super().__init__(name, teacher, students)
182+
self.grade = grade
183+
184+
def __str__(self):
185+
return self.name + " " + str(self.teacher) + " " + str(self.students) + " " + str(self.grade)
186+
187+
def __repr__(self):
188+
return self.name + " " + str(self.teacher) + " " + str(self.students) + " " + str(self.grade)
189+
190+
191+
student_1 = Student("John", 20, "A")
192+
print(student_1)
193+
194+
teacher_1 = Teacher("John", 20, 1000)
195+
print(teacher_1)
196+
197+
198+
# Iterators & Recursion: Recursive Fibonacci , Tower Of Hanoi
199+
# Search : Simple Search and Estimating Search Time , Binary Search and Estimating Binary Search
200+
# Time
201+
# Sorting & Merging: Selection Sort , Merge List , Merge Sort , Higher Order Sort
202+
203+
def recursive_fibonacci(n):
204+
if n == 0:
205+
return 0
206+
elif n == 1:
207+
return 1
208+
else:
209+
return recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2)
210+
211+
def tower_of_hanoi(n, A, B, C):
212+
if n == 1:
213+
print(A, "->", C)
214+
else:
215+
tower_of_hanoi(n - 1, A, C, B)
216+
print(A, "->", C)
217+
tower_of_hanoi(n - 1, B, A, C)
218+
219+
def simple_search(list, element):
220+
for i in range(len(list)):
221+
if list[i] == element:
222+
return i
223+
return -1
224+
225+
def binary_search(list, element):
226+
low = 0
227+
high = len(list) - 1
228+
while low <= high:
229+
mid = (low + high) // 2
230+
if list[mid] == element:
231+
return mid
232+
elif list[mid] < element:
233+
low = mid + 1
234+
else:
235+
high = mid - 1
236+
return -1
237+
238+
239+
def selection_sort(list):
240+
for i in range(len(list)):
241+
min_index = i
242+
for j in range(i + 1, len(list)):
243+
if list[j] < list[min_index]:
244+
min_index = j
245+
list[i], list[min_index] = list[min_index], list[i]
246+
return list
247+
248+
249+
print(selection_sort([6, 5, 3, 1, 8, 7, 2, 4]))
250+
251+
def merge_list(list1, list2):
252+
merged_list =[]
253+
while len(list1) > 0 and len(list2) > 0:
254+
if list1[0] < list2[0]:
255+
merged_list.append(list1[0])
256+
list1.pop(0)
257+
else:
258+
merged_list.append(list2[0])
259+
list2.pop(0)
260+
if len(list1) > 0:
261+
merged_list += list1
262+
if len(list2) > 0:
263+
merged_list += list2
264+
return merged_list
265+
266+
267+
print(merge_list([1, 3, 5], [2, 4, 6]))
268+
269+
270+
def merge_sort(list):
271+
if len(list) <= 1:
272+
return list
273+
mid = len(list) // 2
274+
left = merge_sort(list[:mid])
275+
right = merge_sort(list[mid:])
276+
return merge_list(left, right)
277+
278+
print(merge_sort([6, 5, 3, 1, 8, 7, 2, 4]))
279+
280+
def higher_order_sort(list):
281+
if len(list) <= 1:
282+
return list
283+
mid = len(list) // 2
284+
left = higher_order_sort(list[:mid])
285+
right = higher_order_sort(list[mid:])
286+
return merge_list(left, right)

0 commit comments

Comments
 (0)