From aee554cdc6646aaf5d174046fd5329b87741ed85 Mon Sep 17 00:00:00 2001 From: Saksham Manocha Date: Mon, 14 Oct 2019 20:59:22 +0530 Subject: [PATCH 1/2] Add files via upload --- Programmes!!!.ipynb | 224 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 Programmes!!!.ipynb diff --git a/Programmes!!!.ipynb b/Programmes!!!.ipynb new file mode 100644 index 0000000..1b61e50 --- /dev/null +++ b/Programmes!!!.ipynb @@ -0,0 +1,224 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Rules" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "1. No use of In Built Methods\n", + "2. Try to Decrease Time Complexity\n", + "3. Try to Decrease Space Complexity\n", + "4. Try to make the code as simple as possible" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# String Methods " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "olleh\n", + "world\n" + ] + } + ], + "source": [ + "# String Reversal\n", + "\n", + "def string_rev(string):\n", + " rev = ''\n", + " le = len(string)-1\n", + " \n", + " while(le>=0):\n", + " rev += string[le]\n", + " le-=1\n", + " return rev\n", + "\n", + "print(string_rev('hello'))\n", + "print(string_rev('dlrow'))" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "True\n", + "False\n" + ] + } + ], + "source": [ + "# Palindrome Check\n", + "\n", + "def palindrome(string):\n", + " start = 0\n", + " end = len(string)-1\n", + " palindrome = False\n", + " \n", + " while(start!=end):\n", + " if string[start]==string[end]:\n", + " palindrome = True\n", + " start+=1\n", + " end-=1\n", + " else:\n", + " palindrome = False\n", + " break\n", + " \n", + " return palindrome\n", + "\n", + "print(palindrome('civic'))\n", + "print(palindrome('radar'))\n", + "print(palindrome('hello'))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "# Checking if sentence/word is a pangram(sentence/word with every alphabet)\n", + "import re\n", + "\n", + "def pangram_checker(word):\n", + " letter = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']\n", + " # Remove spaces \n", + " no_space = re.sub(re.compile(r'\\s+'), '', word)\n", + " for i in no_space:\n", + " if i in letter:\n", + " letter.remove(i)\n", + " if len(letter)==0:\n", + " return True\n", + " return False\n", + "\n", + "print(pangram_checker('The quick brown fox jumps over the dog'))\n", + "print(pangram_checker('The quick brown fox jumps over the lazy dog'))" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "abcfa\n", + "abcac\n", + "helo wrdlo\n", + "acbdlabl\n" + ] + } + ], + "source": [ + "# Rearrange characters in a string such that no two adjacent letter are same\n", + "\n", + "from collections import Counter\n", + "import numpy as np\n", + "\n", + "def rearrage_string(string):\n", + " letter_dict = Counter(string)\n", + " rearr_str = ''\n", + " loop_run = np.ceil(np.array(list(letter_dict.values())).sum()/len(list(letter_dict.keys())))\n", + " \n", + " while(loop_run>0):\n", + " for key,value in letter_dict.items():\n", + " if value>0:\n", + " rearr_str+=str(key)\n", + " letter_dict[key]-=1\n", + " else:\n", + " continue\n", + " loop_run-=1\n", + " return rearr_str \n", + "\n", + "\n", + " \n", + "print(rearrage_string('aabcf')) \n", + "print(rearrage_string('aabcc'))\n", + "print(rearrage_string('hello world'))\n", + "print(rearrage_string('aacbbdll'))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.3" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 1e8efa29ee304b9293604b7bf4f33af2cd61356b Mon Sep 17 00:00:00 2001 From: Saksham Manocha Date: Mon, 14 Oct 2019 21:16:06 +0530 Subject: [PATCH 2/2] Add files via upload --- OOP.ipynb | 388 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 388 insertions(+) create mode 100644 OOP.ipynb diff --git a/OOP.ipynb b/OOP.ipynb new file mode 100644 index 0000000..c9fc59d --- /dev/null +++ b/OOP.ipynb @@ -0,0 +1,388 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "labra\n", + "5\n", + "7\n", + "saksham\n", + "saksham is 20 years old and is Student\n" + ] + } + ], + "source": [ + "#Classes \n", + "#syntax \n", + "# class ClassName()\n", + "# def __init__(self,argument1,......)\n", + "# self.argument1 = argument1,\n", + "# .....\n", + "# def myfunc():\n", + "# .......\n", + "\n", + "#Classes are used to use common statements for different arguments...\n", + "\n", + "#Note : ClassName must be capitalize.......\n", + "\n", + "#Example 1:\n", + "\n", + "#Lets make class for dog \n", + "\n", + "class Dog():\n", + " #Attributes the class takes in\n", + " #is assigned to class using self method\n", + " #Not : It is very similar to javascript method 'this' with the differnce that in javascript, constructor func. is used and 'this' is not an argument.\n", + " def __init__(self,breed,age):\n", + " self.breed = breed\n", + " self.age = age\n", + " \n", + "my_dog = Dog('labra',5)\n", + "print(my_dog.breed)\n", + "print(my_dog.age)\n", + "\n", + "my_dog2 = Dog('huksy',7)\n", + "print(my_dog2.age)\n", + "\n", + "\n", + "\n", + "#Methods in Classes\n", + "#methods are the functions defined in classes.\n", + "\n", + "#Example 1:\n", + "\n", + "class Person():\n", + " def __init__(self,name,age,profession):\n", + " self.name = name\n", + " self.age = age\n", + " self.profession = profession\n", + " \n", + " def bio(self):\n", + " print('{} is {} years old and is {}'.format(self.name,self.age,self.profession))\n", + " \n", + "me = Person('saksham',20,'Student')\n", + "print(me.name)\n", + "me.bio()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Circle has the perimeter of 12.56\n", + "314.0\n", + "Hello World\n" + ] + } + ], + "source": [ + "#Example 2:\n", + "\n", + "class Circle():\n", + " \n", + " #Class Argument\n", + " pi = 3.14\n", + " \n", + " #instance attribute\n", + " def __init__(self,radius):\n", + " self.radius = radius\n", + " self.area = radius*radius*self.pi\n", + " \n", + " def perimeter(self):\n", + " print('Circle has the perimeter of {}'.format(self.radius*2*self.pi))\n", + "\n", + "# instantiate the Circle class \n", + "circle1 = Circle(2) \n", + "# access the class attributes\n", + "circle.perimeter()\n", + "\n", + "circle2 = Circle(10)\n", + "print(circle2.area)\n", + "\n", + "\n", + "\n", + "# important: when we initiate class,then __init__ function runs automatically..\n", + "#For Ex:\n", + "class Example():\n", + " def __init__(self):\n", + " print('Hello World')\n", + " \n", + "exam1 = Example() " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Inheritance" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello from my side\n", + "I am an animal\n", + "Hello from my side\n", + "Hola\n", + "I love eating\n", + "I love barking\n" + ] + } + ], + "source": [ + "#Inheritance is using one class inside another class and inherating one's class property to another class........\n", + "\n", + "\n", + "#Example 1:\n", + "\n", + "class Animal():\n", + " def __init__(self):\n", + " print('Hello from my side')\n", + " \n", + " def who_me(self):\n", + " print('I am an animal')\n", + " \n", + " def i_love(self):\n", + " print('I love eating')\n", + " \n", + "animal1 = Animal()\n", + "animal1.who_me()\n", + " \n", + "#Now creating another class and inherating Animal class init...\n", + "\n", + "class Dog(Animal):\n", + " def __init__(self):\n", + " Animal.__init__(self)#It inheritis Animal into Dog....\n", + " print('Hola')\n", + " \n", + " def i_love(self):\n", + " print('I love barking')#Overwriting the function...\n", + "\n", + "dog1 = Dog()\n", + "animal1.i_love()\n", + "dog1.i_love()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Polymorphism\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Parrot's can fly\n", + "Penguin's can't fly\n" + ] + } + ], + "source": [ + "#Polymorphism is an ability (in OOP) to use common interface for multiple form (data types).\n", + "\n", + "#Suppose, we need to color a shape, there are multiple shape option (rectangle, square, circle). \n", + "#However we could use same method to color any shape. This concept is called Polymorphism.\n", + "\n", + "class Parrot():\n", + " def fly(self):\n", + " print('Parrot\\'s can fly')\n", + " \n", + " def swim(self):\n", + " print('Parrot\\'s can\\'t fly')\n", + " \n", + "class Penguin():\n", + " def fly(self):\n", + " print('Penguin\\'s can\\'t fly')\n", + " \n", + " def swim(self):\n", + " print('Penguin can swim')\n", + " \n", + " \n", + "myParrot = Parrot()\n", + "myPenguin = Penguin()\n", + "\n", + "def flying_test(animal):\n", + " animal.fly()\n", + " \n", + " \n", + "flying_test(myParrot)\n", + "flying_test(myPenguin)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Gone Girl by Gillian Flynn\n", + "278\n", + "This class is deleted\n" + ] + } + ], + "source": [ + "#Some built-in methods :\n", + "\n", + "\n", + "class Book():\n", + " def __init__(self,title,author,pages):\n", + " self.title = title\n", + " self.author = author\n", + " self.pages = pages\n", + " \n", + " def __str__(self):\n", + " return f'{self.title} by {self.author}'\n", + " \n", + " def __len__(self):\n", + " return self.pages\n", + " \n", + " def __del__(self):\n", + " print('This class is deleted')\n", + " \n", + "b = Book('Gone Girl','Gillian Flynn',278)\n", + "print(str(b))\n", + "\n", + "print(len(b))\n", + "\n", + "del b" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# OOP Challenge:\n" + ] + }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "For this challenge, create a bank account class that has two attributes:\n", + "\n", + "owner\n", + "balance\n", + "and two methods:\n", + "\n", + "deposit\n", + "withdraw\n", + "As an added requirement, withdrawals may not exceed the available balance.\n", + "\n", + "Instantiate your class, make several deposits and withdrawals, and test to make sure the account can't be overdrawn." + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Amount 20 is added onto the account. Available balance is 40\n", + "Amount 100 is added onto the account. Available balance is 140\n", + "Amount 25 is withdrawn from the account. Available balance is 115\n", + "Amount 100 is withdrawn from the account. Available balance is 15\n" + ] + }, + { + "data": { + "text/plain": [ + "15" + ] + }, + "execution_count": 58, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "class Bank():\n", + " \n", + " total=0\n", + " \n", + " def __init__(self,owner,balance):\n", + " self.owner = owner\n", + " self.balance = balance \n", + " self.total+=balance\n", + " \n", + " def deposit(self,amount):\n", + " self.total+=amount\n", + " print(f'Amount {amount} is added onto the account. Available balance is {self.total}')\n", + " \n", + " def withdraw(self,amount):\n", + " if self.total-amount<0:\n", + " print('Available balance is not sufficient')\n", + " else:\n", + " self.total-=amount\n", + " print(f'Amount {amount} is withdrawn from the account. Available balance is {self.total}')\n", + " \n", + " \n", + "myAccount = Bank('saksham',20) \n", + "myAccount.balance\n", + "myAccount.deposit(20)\n", + "myAccount.deposit(100)\n", + "myAccount.withdraw(25)\n", + "myAccount.withdraw(100)\n", + "myAccount.total" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}