From 27ec86a094e35bdd439f2620a2be8611a5b9bc47 Mon Sep 17 00:00:00 2001 From: Hizenberg469 Date: Mon, 10 Mar 2025 11:40:48 +0530 Subject: [PATCH] OOP finished --- Basic/hello-world.py | 66 ++++++--------------------------- Functional-programming/wiki.txt | 0 OOP/MRO.py | 32 ++++++++++++++++ OOP/SuperList.py | 18 +++++++++ OOP/game.py | 37 ++++++++++++++++++ OOP/wiki.txt | 47 ++++++++++++++++++++++- 6 files changed, 144 insertions(+), 56 deletions(-) create mode 100644 Functional-programming/wiki.txt create mode 100644 OOP/MRO.py create mode 100644 OOP/SuperList.py create mode 100644 OOP/game.py diff --git a/Basic/hello-world.py b/Basic/hello-world.py index 46547f9..10d85f4 100644 --- a/Basic/hello-world.py +++ b/Basic/hello-world.py @@ -1,57 +1,13 @@ -import unittest -import random +def j(n): + if n == 1: + return 1 + + if n % 2 == 0 : + return 2 * j(n/2) - 1 + else: + return 2 * j((n-1)/2) + 1 + -globals = None -report = unittest.Report() -class ODD_EVEN_TEST_001(unittest.SystemTestCase): +for i in range(1,33): + print(f'{i} : {j(i)}') - @classmethod - def setUpClass(inst): - """ Add the initial configuration if any """ - - @classmethod - def tearDownClass(inst): - """ Add the teardown configuration if any """ - - # """Add the definitions for tests defined in suite here""" - # def dummy_assert(self): - # report.normal("This is normal print") - # report.error("This is errored print") - # report.notification("This is notify print") - # assrt=unittest.Assert(self) - # assrt.isEquals("1", "2", "1 Equals 2") - # assrt.isEquals("1", "1", "1 Equals 2") - - def generate_number(self,seed_value): - random.seed(seed_value) - - gen_value = random.randrange(1,7) - - return gen_value - - - def check_testcase(self): - - for x in range(1,26): - generated_value = self.generate_number(x) - - report.normal(f'For test case {x}:') - assrt = unittest.Assert(self) - - if( generated_value == 3): - assrt.isEquals("1",str(generated_value), "You win!!!") - elif( generated_value == 6): - assrt.isEquals("6",str(generated_value), "You win!!!") - else: - assrt.isEquals("6",str(generated_value), "You lose!!!") - - -def suite(): - suite = unittest.TestSuite() - """Add your tests into the suite here""" - suite.addTest(ODD_EVEN_TEST_001("check_testcase")) - return suite - -def main(): - runner = unittest.TextTestRunner() - return runner.run(suite()) diff --git a/Functional-programming/wiki.txt b/Functional-programming/wiki.txt new file mode 100644 index 0000000..e69de29 diff --git a/OOP/MRO.py b/OOP/MRO.py new file mode 100644 index 0000000..fcc2acb --- /dev/null +++ b/OOP/MRO.py @@ -0,0 +1,32 @@ +#MRO - Method Resolution order + +class A: + num = 10 + +class B(A): + pass + +class C(A): + num = 1 + +class D(B,C): + pass + +print(D.mro()) + +# [, +# , +# , +# , +# ] + +# The order of this determined using DFS. + +# A +# / \ +# / \ +# B C +# \ / +# \ / +# D + diff --git a/OOP/SuperList.py b/OOP/SuperList.py new file mode 100644 index 0000000..4f7cfc8 --- /dev/null +++ b/OOP/SuperList.py @@ -0,0 +1,18 @@ +#Implementing List: + +class SuperList(list): + def __len__(self): + return 1000 + + +super_list1 = SuperList() + +print(len(super_list1)) + +super_list1.append(5) + +print(super_list1[0]) + +print(issubclass(SuperList, list)) + +print(issubclass(list, object)) \ No newline at end of file diff --git a/OOP/game.py b/OOP/game.py new file mode 100644 index 0000000..98f3d10 --- /dev/null +++ b/OOP/game.py @@ -0,0 +1,37 @@ +class User(object): + def __init__(self, email): + self.email = email + print('init complete') + + def sign_in(self): + print('logged in') + +class Wizard(User): + def __init__(self, name, power): + self.name = name + self.power = power + + def attack(self): + print(f'attacking with power of {self.power}') + +class Archer(User): + def __init__(self, name, arrows): + self.name = name + self.arrows = arrows + + def check_arrows(self): + print(f'{self.arrows} remaining') + + def run(self): + print('ran really fast') + +class HybridBorg(Wizard, Archer): + def __init__(self, name, power, arrows): + Archer.__init__(self, name, arrows) + Wizard.__init__(self, name, power) + +hb1 = HybridBorg('borgie', 50, 100) + +print(hb1.check_arrows()) +print(hb1.attack()) +print(hb1.sign_in()) \ No newline at end of file diff --git a/OOP/wiki.txt b/OOP/wiki.txt index 571098a..59c3f34 100644 --- a/OOP/wiki.txt +++ b/OOP/wiki.txt @@ -109,4 +109,49 @@ It specify what are the functions and variable the instance of a class(object) has access to. --> Dunder methods: \ No newline at end of file +-> Dunder methods: + + Dunder methods allow us to modify and implement built-in function + of objects. This allow us to implement our own custom logic for + built-in function which work for objects. The modification only + works for that same object only and if we apply it on different + object then it would behave as usual(or as implemented for other + object) + + + for ex: + + class Toy(): + def __init__(self, color, age): + self.color = color + self.age = age + self.my_dict = { + 'name' : 'Yoyo', + 'has_pets' : False + } + + def __str__(self): + return f'{self.color}' + + def __len__(self): + return 5 + + def __call__(self): + return ('yes??') + + def __getitem__(self, i): + return self.my_dict[i] + + + action_figure = Toy('red',0) + print(action_figure.__str__()) + print(str(action_figure)) + print(len(action_figure)) + print(action_figure()) + print(action_figure['name']) + +-> MRO (Method Resolution order): + + MRO allow us to determine the order in which + the Inheritance need to be processed. This + order is determined using Depth first search. \ No newline at end of file