{ "cells": [ { "cell_type": "markdown", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "### ```Q1.``` ### \n", "### ```a) Write a Python program to print all the Disarium numbers between 1 and 100.``` ### " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## -----------------------------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A ```Disarium``` number is a number defined by the following process: \n", "\n", "A number is be called ```Disarium``` if the sum of its digits powered with their respective position is equal with the number itself. \n", "\n", "For example, 175 is a Disarium number: \n", "As 11 + 72 + 53 = 175 \n", "\n", "Some other DISARIUM are 89, 135, 175, 518 etc. \n", " \n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## -----------------------------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this program, we need to print all the disarium numbers between 1 and 100 by following the algorithm as given below: \n", " \n", "### ALGORITHM: ### \n", "\n", "```STEP 1:``` \n", "* **calculate_length()** counts the digits present in a number. \n", "\n", "* Use a _while_ loop to check whether the _number_ variable is equal to 0 or not. \n", "\n", "* Divide the _number_ variable by 10 and increment the _length_ variable by 1. \n", "\n", "* Return length. \n", "\n", "```STEP 2:``` \n", "\n", "* **sum_of_digits()** calculates the sum of digits raised to their respective positions.\n", "\n", "* Make a call to **calculate_length()** to get the number of digits present in a given number and store the value in _length_ variable. \n", "\n", "* Using the _while_ loop calculate the _remainder_ variable repeatedly by dividing the _number_ with 10. \n", "\n", "* Calculate the value of _result_ variable by adding the _result_ variable to the _remainder_ variable raised to power its _length_ position.\n", "\n", "```STEP 3:``` \n", "\n", "* To display all the Disarium numbers between 1 and 100. \n", "\n", "* Start a loop from 1 to 100, then make a call to **sum_of_digits()** method for each value from 1 to 100 and store the return value into the _result_ variable.\n", "`\n", "* If the value of the _result_ is equal to the _number_, it implies that the given number is a Disarium number. Hence, display it. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## -----------------------------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### ```Q1.``` ###\n", "### ```b) Write a Python program to encrypt the text using Caesar Cipher technique. Display the encrypted text. Prompt the user for input and the shift pattern.``` ### " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## -----------------------------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In cryptography, Caesar cipher is one of the simplest and most widely known encryption techniques. It is also known with other names like Caesar's cipher, the shift cipher, Caesar's code or Caesar shift. This encryption technique is used to encrypt plain text, so only the person you want can read it. The method is named after Julius Caesar, who used it in his private correspondence. \n", "\n", "In this encryption technique, to encrypt our data, we have to replace each letter in the text by some other letter at a fixed difference. Let's say, there is a letter ```'T'``` then with a right shift of ```1``` it will be ```'U'``` and with a left shift of ```1``` it will become ```'S'```. So here, the difference is ```1``` and the direction will also be same for a text. Either we can use left shift or right, not both in same text. \n", "\n", "Let's understand it with an easy example. \n", "\n", "For example, suppose we have text ```\"abcdef\"``` to be encrypted. Then what we can do is replace each of letter present in the text by another letter having fixed difference. Lets say we want right shift by ```3``` then each letter of the above text have to replaced by the letter, positioned ```3 steps``` from the letter. \n", "\n", "```Plaintext: abcdef``` \n", "\n", "```Ciphertext: defghi``` \n", "\n", " \n", "\n", "Now user can't read this text until he/she have the decryption key. Decryption key is nothing just the knowledge about how we have shifted those letters while encrypting it. To decrypt this we have to left shift all the letters by ```3```.\n", "\n", "That was the basic concept of Caesar cipher. If we see this encryption technique in mathematical way then the formula to get encrypted letter will be:\n", "\n", "**```c = (x + n) mod 26```**\n", "\n", "where, ```c``` is the place value of encrypted letter, ```x``` is the place value of actual letter and ```n``` is the number that shows us how many positions of letters we have to replace.\n", "\n", "On other hand, to decrypt each letter we'll use the formula given below:\n", "\n", "**```c = (x – n) mod 26```**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## -----------------------------------------------------------------------------------------------------------------------------" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "pycharm": { "is_executing": true, "metadata": false, "name": "#%%\n" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter 1 to Print all the Disarium number between 1 and 100 \n", "\n", "Enter 2 to Encrypt the text using Caesar Cipher technique \n", "\n", "Enter 3 to Exit the program \n", "\n", "1\n", "\n", "Disarium numbers between 1 and 100 are \n", "\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n", "89\n", "Enter 1 to Print all the Disarium number between 1 and 100 \n", "\n", "Enter 2 to Encrypt the text using Caesar Cipher technique \n", "\n", "Enter 3 to Exit the program \n", "\n", "2\n", "\n", "Enter a text: \n", "abcdef\n", "Enter Shift Pattern for encryption: \n", "3\n", "The encrypted text is defghi \n", "\n", "Enter 1 to Print all the Disarium number between 1 and 100 \n", "\n", "Enter 2 to Encrypt the text using Caesar Cipher technique \n", "\n", "Enter 3 to Exit the program \n", "\n", "3\n", "\n" ] } ], "source": [ "# Code to generate Disarium number starts\n", "def calculate_length(number):\n", " length = 0\n", " while number != 0:\n", " length = length + 1\n", " number = number // 10\n", " return length\n", "\n", "\n", "# sum_of_digits() will calculates the sum of digits powered with their respective position\n", "def sum_of_digits(number):\n", " remainder = result = 0\n", " length = calculate_length(number)\n", " while number > 0:\n", " remainder = number % 10\n", " result = result + (remainder ** length)\n", " number = number // 10\n", " length = length - 1\n", " return result\n", "\n", "\n", "def print_disarium():\n", " result = 0\n", " # Displays all Disarium numbers between 1 and 100\n", " print(\"Disarium numbers between 1 and 100 are \\n\")\n", " for each_number in range(1, 101):\n", " result = sum_of_digits(each_number)\n", " if result == each_number:\n", " print(each_number)\n", "\n", "\n", "# Code to generate Disarium number ends\n", "\n", "\n", "# # Code for Caesar Cipher technique starts\n", "def encrypt_text(input_text, shift_pattern):\n", " cipher_result = \"\"\n", " # Traverse the plain text\n", " for each_char in input_text:\n", " # Check for empty spaces\n", " if each_char == \" \":\n", " cipher_result = cipher_result + each_char\n", " elif each_char.isupper():\n", " # Encrypt the uppercase characters in plain text\n", " cipher_result = cipher_result + chr(\n", " (ord(each_char) + shift_pattern - 65) % 26 + 65\n", " )\n", " else:\n", " # Encrypt the lowercase characters in plain text\n", " cipher_result = cipher_result + chr(\n", " (ord(each_char) + shift_pattern - 97) % 26 + 97\n", " )\n", "\n", " return cipher_result\n", "\n", "\n", "# # Code to apply Caesar Cipher technique end\n", "\n", "\n", "def user_input():\n", " while True:\n", " print(\"Enter 1 to Print all the Disarium number between 1 and 100 \\n\")\n", " print(\"Enter 2 to Encrypt the text using Caesar Cipher technique \\n\")\n", " print(\"Enter 3 to Exit the program \\n\")\n", " choice = int(input())\n", " print(\"\")\n", " if choice == 1:\n", " print_disarium()\n", " elif choice == 2:\n", " input_text = input(\"Enter a text: \\n\")\n", " shift_pattern = int(input(\"Enter Shift Pattern for encryption: \\n\"))\n", " encrypted_text = encrypt_text(input_text, shift_pattern)\n", " print(f\"The encrypted text is {encrypted_text} \\n\")\n", " else:\n", " break\n", "\n", "\n", "if __name__ == \"__main__\":\n", " user_input()" ] } ], "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" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } } }, "nbformat": 4, "nbformat_minor": 4 }