|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "27900b66-b4c4-4f33-aadf-c6cd03e62916", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "</> Code Challenge: Factorials" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "id": "f2b7798c-1d83-47e6-9b90-b1f5c4ac989e", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "def factorial(num):\n", |
| 21 | + " # Your code goes here\n", |
| 22 | + " pass" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 5, |
| 28 | + "id": "6617f97f-b644-4d05-8f76-64dcd32b1010", |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [ |
| 31 | + { |
| 32 | + "name": "stdout", |
| 33 | + "output_type": "stream", |
| 34 | + "text": [ |
| 35 | + "None\n", |
| 36 | + "None\n", |
| 37 | + "None\n" |
| 38 | + ] |
| 39 | + } |
| 40 | + ], |
| 41 | + "source": [ |
| 42 | + "#Step 1 - print function test cases\n", |
| 43 | + "def factorial(num):\n", |
| 44 | + " return None\n", |
| 45 | + "\n", |
| 46 | + "print(factorial(0))\n", |
| 47 | + "print(factorial(5))\n", |
| 48 | + "print(factorial('spam spam spam'))" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": 12, |
| 54 | + "id": "0493e97c-190a-4b06-b917-a7220288f21c", |
| 55 | + "metadata": {}, |
| 56 | + "outputs": [ |
| 57 | + { |
| 58 | + "name": "stdout", |
| 59 | + "output_type": "stream", |
| 60 | + "text": [ |
| 61 | + "Is int\n", |
| 62 | + "Is int\n", |
| 63 | + "Not int\n" |
| 64 | + ] |
| 65 | + } |
| 66 | + ], |
| 67 | + "source": [ |
| 68 | + "#Step 2 - check number for being an integer\n", |
| 69 | + "def factorial(num):\n", |
| 70 | + " if not isinstance(num, int) or num < 0:\n", |
| 71 | + " return \"Not int\"\n", |
| 72 | + " \n", |
| 73 | + " else:\n", |
| 74 | + " return \"Is int\"\n", |
| 75 | + " \n", |
| 76 | + "print(factorial(0))\n", |
| 77 | + "print(factorial(5))\n", |
| 78 | + "print(factorial('spam spam spam'))" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": 13, |
| 84 | + "id": "298ea029-2867-4198-8d91-fac7fae737c7", |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [ |
| 87 | + { |
| 88 | + "name": "stdout", |
| 89 | + "output_type": "stream", |
| 90 | + "text": [ |
| 91 | + "Is Int\n", |
| 92 | + "Is Int - do factorial calc\n", |
| 93 | + "Not Int\n" |
| 94 | + ] |
| 95 | + } |
| 96 | + ], |
| 97 | + "source": [ |
| 98 | + "#Step 3 - add elif for simple cases\n", |
| 99 | + "def factorial(num):\n", |
| 100 | + " if not isinstance(num, int) or num < 0:\n", |
| 101 | + " return \"Not Int\"\n", |
| 102 | + " \n", |
| 103 | + " elif num == 0 or num ==1:\n", |
| 104 | + " return \"Is Int\"\n", |
| 105 | + " \n", |
| 106 | + " else:\n", |
| 107 | + " return \"Is Int - do factorial calc\"\n", |
| 108 | + " \n", |
| 109 | + "print(factorial(0))\n", |
| 110 | + "print(factorial(5))\n", |
| 111 | + "print(factorial('spam spam spam'))" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": 14, |
| 117 | + "id": "eb34c395-03cc-4c6c-b419-6d52454df2fc", |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [ |
| 120 | + { |
| 121 | + "name": "stdout", |
| 122 | + "output_type": "stream", |
| 123 | + "text": [ |
| 124 | + "1\n", |
| 125 | + "120\n", |
| 126 | + "None\n" |
| 127 | + ] |
| 128 | + } |
| 129 | + ], |
| 130 | + "source": [ |
| 131 | + "#Step 3 - add return values & equation for a factorial\n", |
| 132 | + "def factorial(num):\n", |
| 133 | + " if not isinstance(num, int) or num < 0:\n", |
| 134 | + " return None\n", |
| 135 | + " \n", |
| 136 | + " elif num == 0 or num ==1:\n", |
| 137 | + " return 1\n", |
| 138 | + " \n", |
| 139 | + " else:\n", |
| 140 | + " return num * factorial(num - 1)\n", |
| 141 | + " \n", |
| 142 | + "print(factorial(0))\n", |
| 143 | + "print(factorial(5))\n", |
| 144 | + "print(factorial('spam spam spam'))" |
| 145 | + ] |
| 146 | + } |
| 147 | + ], |
| 148 | + "metadata": { |
| 149 | + "kernelspec": { |
| 150 | + "display_name": "Python 3 (ipykernel)", |
| 151 | + "language": "python", |
| 152 | + "name": "python3" |
| 153 | + }, |
| 154 | + "language_info": { |
| 155 | + "codemirror_mode": { |
| 156 | + "name": "ipython", |
| 157 | + "version": 3 |
| 158 | + }, |
| 159 | + "file_extension": ".py", |
| 160 | + "mimetype": "text/x-python", |
| 161 | + "name": "python", |
| 162 | + "nbconvert_exporter": "python", |
| 163 | + "pygments_lexer": "ipython3", |
| 164 | + "version": "3.12.5" |
| 165 | + } |
| 166 | + }, |
| 167 | + "nbformat": 4, |
| 168 | + "nbformat_minor": 5 |
| 169 | +} |
0 commit comments