|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "collapsed": true |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "# Final Capstone Projects\n", |
| 10 | + "*A collection of solved <b>Python</b> Exercises, feel free to test your skills and contribute with your own solutions!*\n", |
| 11 | + "<br>\n", |
| 12 | + "<b> If you wish to try out the unsolved exercises, please check the [Python Capstone Projects](https://github.com/dizzydroid/Python-Capstone-Projects/blob/main/Python%20Capstone%20Projects.ipynb) notebook. </b>" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "markdown", |
| 17 | + "metadata": {}, |
| 18 | + "source": [ |
| 19 | + "**Fibonacci Sequence** - Enter a number and have the program generate the Fibonacci sequence to that number or to the Nth number.\n", |
| 20 | + "The Fibonacci sequence is a series of numbers where a number is the addition of the last two numbers, starting with 0, and 1." |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "metadata": {}, |
| 27 | + "outputs": [], |
| 28 | + "source": [ |
| 29 | + "num = int(input(\"Enter a number: \"))\n", |
| 30 | + "def fibonacci(num):\n", |
| 31 | + " if num <= 1:\n", |
| 32 | + " return num\n", |
| 33 | + " else:\n", |
| 34 | + " return(fibonacci(num-1) + fibonacci(num-2))\n", |
| 35 | + "print(\"Fibonacci sequence:\")\n", |
| 36 | + "for i in range(num):\n", |
| 37 | + " print(fibonacci(i))" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "markdown", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "**Find PI to the Nth Digit** - Enter a number and have the program generate π (pi) up to that many decimal places. \n", |
| 45 | + " Keep a limit to how far the program will go." |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": null, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "from math import pi\n", |
| 55 | + "num = int(input(\"How big do you want your pi?\"))\n", |
| 56 | + "if num > 15:\n", |
| 57 | + " print(\"Sorry, that's too big!\")\n", |
| 58 | + "else:\n", |
| 59 | + " print(round(pi, num))" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "markdown", |
| 64 | + "metadata": {}, |
| 65 | + "source": [ |
| 66 | + "**Find e to the Nth Digit** - Just like the previous problem, but with e instead of π (pi). \n", |
| 67 | + "Enter a number and have the program generate e up to that many decimal places. Keep a limit to how far the program will go." |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "code", |
| 72 | + "execution_count": null, |
| 73 | + "metadata": {}, |
| 74 | + "outputs": [], |
| 75 | + "source": [ |
| 76 | + "from math import e\n", |
| 77 | + "num = int(input(\"How big would you like the e?\"))\n", |
| 78 | + "if num > 15:\n", |
| 79 | + " print(\"Sorry, we don't have that size\")\n", |
| 80 | + "else:\n", |
| 81 | + " print(round(e,num))" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "metadata": {}, |
| 87 | + "source": [ |
| 88 | + "**Prime Factorization** - Have the user enter a number and find all Prime Factors (if there are any) and display them.\n" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": null, |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "import math\n", |
| 98 | + "num = int(input(\"Enter a number to find the primes included!\"))\n", |
| 99 | + "def is_prime(num):\n", |
| 100 | + " if num < 2:\n", |
| 101 | + " return False\n", |
| 102 | + " for i in range(2, int(math.sqrt(num))+1):\n", |
| 103 | + " if num % i == 0:\n", |
| 104 | + " return False\n", |
| 105 | + " return True\n", |
| 106 | + "for i in range(num):\n", |
| 107 | + " if is_prime(i):\n", |
| 108 | + " print(i)" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "markdown", |
| 113 | + "metadata": {}, |
| 114 | + "source": [ |
| 115 | + "**Find Cost of Tile to Cover W x H Floor** - Calculate the total cost of tile it would take to cover a floor plan of width and height, using a cost entered by the user." |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": null, |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [ |
| 124 | + "width = int(input(\"Enter width of floor: \"))\n", |
| 125 | + "height = int(input(\"Enter height of floor: \"))\n", |
| 126 | + "cost = float(input(\"Enter cost of a single tile: \"))\n", |
| 127 | + "totalcost = width * height * cost\n", |
| 128 | + "print(f\"The total cost to cover this floor is: ${totalcost:.2f}\")" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "markdown", |
| 133 | + "metadata": {}, |
| 134 | + "source": [ |
| 135 | + "**Binary to Decimal and Back Converter** - Develop a converter to convert a decimal number to binary or a binary number to its decimal equivalent." |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": null, |
| 141 | + "metadata": {}, |
| 142 | + "outputs": [], |
| 143 | + "source": [ |
| 144 | + "def binToDec(binNum):\n", |
| 145 | + " decimal = 0\n", |
| 146 | + " binNum = str(binNum)\n", |
| 147 | + " binNum = binNum[::-1]\n", |
| 148 | + " for index, digit in enumerate(binNum):\n", |
| 149 | + " if digit == '1':\n", |
| 150 | + " decimal += 2*index\n", |
| 151 | + " return decimal\n", |
| 152 | + "\n", |
| 153 | + "def decToBin(decNum):\n", |
| 154 | + " binary = bin(decNum)[2::]\n", |
| 155 | + " return binary\n", |
| 156 | + "\n", |
| 157 | + "choice = input('Type \"1\" to convert Decimal into Binary or \"2\" to convert Binary into Decimal: ')\n", |
| 158 | + "if choice == \"1\":\n", |
| 159 | + " num_dec = int(input(\"Enter the Decimal number you wish to convert: \"))\n", |
| 160 | + " num_bin = decToBin(num_dec)\n", |
| 161 | + " print(f\"The Binary equivalent to {num_dec} is {num_bin}\")\n", |
| 162 | + "elif choice == \"2\":\n", |
| 163 | + " num_bin = int(input(\"Enter the Binary number you wish to convert: \"))\n", |
| 164 | + " num_dec = binToDec(num_bin)\n", |
| 165 | + " print(f\"The Decimal equivalent to {num_bin} is {num_dec}\")" |
| 166 | + ] |
| 167 | + }, |
| 168 | + { |
| 169 | + "cell_type": "markdown", |
| 170 | + "metadata": {}, |
| 171 | + "source": [ |
| 172 | + "**Calculator** - A simple calculator to do basic operators. Make it a scientific calculator for added complexity." |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": null, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [], |
| 180 | + "source": [ |
| 181 | + "import math\n", |
| 182 | + "def add(x,y):\n", |
| 183 | + " return x + y\n", |
| 184 | + "def subt(x,y):\n", |
| 185 | + " return x - y\n", |
| 186 | + "def multip(x,y):\n", |
| 187 | + " return x * y\n", |
| 188 | + "def divide(x,y):\n", |
| 189 | + " if y == 0:\n", |
| 190 | + " return \"Undefined (division by zero)\"\n", |
| 191 | + " return x / y\n", |
| 192 | + "def power(x,y):\n", |
| 193 | + " return x ** y\n", |
| 194 | + "def square_root(x):\n", |
| 195 | + " return math.sqrt(x)\n", |
| 196 | + "def log_base10(x):\n", |
| 197 | + " return math.log10(x)\n", |
| 198 | + "def ln(x):\n", |
| 199 | + " return math.log(x)\n", |
| 200 | + "def sin(x):\n", |
| 201 | + " return math.sin(x)\n", |
| 202 | + "def cos(x):\n", |
| 203 | + " return math.cos(x)\n", |
| 204 | + "def tan(x):\n", |
| 205 | + " return math.tan(x)\n", |
| 206 | + "\n", |
| 207 | + "def main():\n", |
| 208 | + " while True:\n", |
| 209 | + " print(\"\\nOptions:\")\n", |
| 210 | + " print(\"\"\"\n", |
| 211 | + "Enter \"add\" or \"+\" for addition\n", |
| 212 | + "Enter \"sub\" or \"-\" for subtraction\n", |
| 213 | + "Enter \"multiply\" or \"x\" for multiplication\n", |
| 214 | + "Enter \"division\" or \"/\" for division\n", |
| 215 | + "Enter \"pow\" for raising to a power\n", |
| 216 | + "Enter \"sqrt\" for square root\n", |
| 217 | + "Enter \"log\" for log of base 10\n", |
| 218 | + "Enter \"ln\" for natural log\n", |
| 219 | + "Enter \"sin\", \"cos\" or \"tan\" for trig. operations\n", |
| 220 | + "Enter \"quit\" to end the program\"\"\")\n", |
| 221 | + " user_input = input(\": \")\n", |
| 222 | + " \n", |
| 223 | + " if user_input == \"quit\":\n", |
| 224 | + " break\n", |
| 225 | + " elif (user_input in (\"sin\",\"cos\",\"tan\",\"sqrt\",\"log\",\"ln\")):\n", |
| 226 | + " x = float(input(\"Enter a number: \"))\n", |
| 227 | + " elif (user_input in (\"add\",\"+\",\"sub\",\"-\",\"multiply\",\"x\",\"division\",\"/\",\"pow\")):\n", |
| 228 | + " x = float(input(\"Enter the first number: \"))\n", |
| 229 | + " y = float(input(\"Enter the second number: \"))\n", |
| 230 | + " else:\n", |
| 231 | + " continue\n", |
| 232 | + "\n", |
| 233 | + " if user_input == \"add\" or user_input == \"+\":\n", |
| 234 | + " print(add(x,y))\n", |
| 235 | + " elif user_input == \"sub\" or user_input == \"-\":\n", |
| 236 | + " print(subt(x,y))\n", |
| 237 | + " elif user_input == \"multiply\" or user_input == \"x\":\n", |
| 238 | + " print(multip(x,y))\n", |
| 239 | + " elif user_input == \"division\" or user_input == \"/\":\n", |
| 240 | + " print(divide(x,y))\n", |
| 241 | + " elif user_input == \"pow\":\n", |
| 242 | + " print(power(x,y))\n", |
| 243 | + " elif user_input == \"sqrt\":\n", |
| 244 | + " print(square_root(x))\n", |
| 245 | + " elif user_input == \"log\":\n", |
| 246 | + " if x <= 0:\n", |
| 247 | + " print(\"x should be greater than 0 for log operations.\")\n", |
| 248 | + " continue\n", |
| 249 | + " print(log_base10(x))\n", |
| 250 | + " elif user_input == \"ln\":\n", |
| 251 | + " if x <= 0:\n", |
| 252 | + " print(\"x should be greater than 0 for ln operations.\")\n", |
| 253 | + " continue\n", |
| 254 | + " print(ln(x))\n", |
| 255 | + " elif user_input == \"sin\":\n", |
| 256 | + " print(sin(x))\n", |
| 257 | + " elif user_input == \"cos\":\n", |
| 258 | + " print(cos(x))\n", |
| 259 | + " elif user_input == \"tan\":\n", |
| 260 | + " print(tan(x))\n", |
| 261 | + " \n", |
| 262 | + "if __name__ == \"__main__\":\n", |
| 263 | + " main()" |
| 264 | + ] |
| 265 | + } |
| 266 | + ], |
| 267 | + "metadata": { |
| 268 | + "kernelspec": { |
| 269 | + "display_name": "Python 3", |
| 270 | + "language": "python", |
| 271 | + "name": "python3" |
| 272 | + }, |
| 273 | + "language_info": { |
| 274 | + "codemirror_mode": { |
| 275 | + "name": "ipython", |
| 276 | + "version": 3 |
| 277 | + }, |
| 278 | + "file_extension": ".py", |
| 279 | + "mimetype": "text/x-python", |
| 280 | + "name": "python", |
| 281 | + "nbconvert_exporter": "python", |
| 282 | + "pygments_lexer": "ipython3", |
| 283 | + "version": "3.10.12" |
| 284 | + } |
| 285 | + }, |
| 286 | + "nbformat": 4, |
| 287 | + "nbformat_minor": 1 |
| 288 | +} |
0 commit comments