|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 2, |
| 6 | + "id": "de859aba", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "import random" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 3, |
| 16 | + "id": "a294cbd8", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [ |
| 19 | + { |
| 20 | + "name": "stdout", |
| 21 | + "output_type": "stream", |
| 22 | + "text": [ |
| 23 | + "XVp5zKkJ\n" |
| 24 | + ] |
| 25 | + } |
| 26 | + ], |
| 27 | + "source": [ |
| 28 | + "use = ''\n", |
| 29 | + "\n", |
| 30 | + "for i in range(97, 123):\n", |
| 31 | + " use += chr(i)\n", |
| 32 | + "else:\n", |
| 33 | + " use += use.upper()\n", |
| 34 | + " use += '!@#$%^&*_+=-'\n", |
| 35 | + " use += '1234567890'\n", |
| 36 | + " \n", |
| 37 | + "password = ''.join(random.choices(use, k = 8))\n", |
| 38 | + "\n", |
| 39 | + "print(password)" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": 4, |
| 45 | + "id": "273891d4", |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [ |
| 48 | + { |
| 49 | + "name": "stdout", |
| 50 | + "output_type": "stream", |
| 51 | + "text": [ |
| 52 | + "Method One\n", |
| 53 | + "UApm1tJm\n", |
| 54 | + "\n", |
| 55 | + "Method Two\n", |
| 56 | + "JxTbSffP\n", |
| 57 | + "\n", |
| 58 | + "Method Three\n", |
| 59 | + "Gnuk6PM7\n", |
| 60 | + "\n", |
| 61 | + "Method Four\n", |
| 62 | + "tw3UA9wP\n" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + "source": [ |
| 67 | + "import string\n", |
| 68 | + "import random\n", |
| 69 | + "\n", |
| 70 | + "class RandomPasswordGenerator:\n", |
| 71 | + " '''Generate Random Password'''\n", |
| 72 | + "\n", |
| 73 | + " def __init__(self, length=8):\n", |
| 74 | + " self.length = length\n", |
| 75 | + " self.strings = string.printable[:62]\n", |
| 76 | + " self.len_strings = len(self.strings) - 1\n", |
| 77 | + "\n", |
| 78 | + " def method_one(self):\n", |
| 79 | + " '''Using random.randint for getting random indexes of self.strings and slicing using that indexes'''\n", |
| 80 | + "\n", |
| 81 | + " password = ''\n", |
| 82 | + "\n", |
| 83 | + " for _ in range(self.length):\n", |
| 84 | + " random_index = random.randint(0, self.len_strings)\n", |
| 85 | + " password += self.strings[random_index]\n", |
| 86 | + "\n", |
| 87 | + " return password\n", |
| 88 | + "\n", |
| 89 | + " def method_two(self):\n", |
| 90 | + " '''Using random.choice for getting the random letter from self.strings and joining them'''\n", |
| 91 | + "\n", |
| 92 | + " password = ''\n", |
| 93 | + "\n", |
| 94 | + " for _ in range(self.length):\n", |
| 95 | + " random_word = random.choice(self.strings)\n", |
| 96 | + " password += random_word\n", |
| 97 | + "\n", |
| 98 | + " return password\n", |
| 99 | + "\n", |
| 100 | + " def method_three(self):\n", |
| 101 | + " '''Using List Comprehension at method_one'''\n", |
| 102 | + "\n", |
| 103 | + " password = [self.strings[random.randint(0, self.len_strings)] for _ in range(self.length)]\n", |
| 104 | + "\n", |
| 105 | + " return ''.join(password)\n", |
| 106 | + "\n", |
| 107 | + " def method_four(self):\n", |
| 108 | + " '''Using List Comprehension at method_two'''\n", |
| 109 | + "\n", |
| 110 | + " password = [random.choice(self.strings) for _ in range(self.length)]\n", |
| 111 | + "\n", |
| 112 | + " return ''.join(password)\n", |
| 113 | + "\n", |
| 114 | + "\n", |
| 115 | + "if __name__ == '__main__':\n", |
| 116 | + " random_password = RandomPasswordGenerator()\n", |
| 117 | + "\n", |
| 118 | + " print('Method One')\n", |
| 119 | + " print(random_password.method_one())\n", |
| 120 | + "\n", |
| 121 | + " print('\\nMethod Two')\n", |
| 122 | + " print(random_password.method_two())\n", |
| 123 | + "\n", |
| 124 | + " print('\\nMethod Three')\n", |
| 125 | + " print(random_password.method_three())\n", |
| 126 | + "\n", |
| 127 | + " print('\\nMethod Four')\n", |
| 128 | + " print(random_password.method_four())" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": null, |
| 134 | + "id": "f91ea505", |
| 135 | + "metadata": {}, |
| 136 | + "outputs": [], |
| 137 | + "source": [] |
| 138 | + } |
| 139 | + ], |
| 140 | + "metadata": { |
| 141 | + "kernelspec": { |
| 142 | + "display_name": "Python 3 (ipykernel)", |
| 143 | + "language": "python", |
| 144 | + "name": "python3" |
| 145 | + }, |
| 146 | + "language_info": { |
| 147 | + "codemirror_mode": { |
| 148 | + "name": "ipython", |
| 149 | + "version": 3 |
| 150 | + }, |
| 151 | + "file_extension": ".py", |
| 152 | + "mimetype": "text/x-python", |
| 153 | + "name": "python", |
| 154 | + "nbconvert_exporter": "python", |
| 155 | + "pygments_lexer": "ipython3", |
| 156 | + "version": "3.9.12" |
| 157 | + } |
| 158 | + }, |
| 159 | + "nbformat": 4, |
| 160 | + "nbformat_minor": 5 |
| 161 | +} |
0 commit comments