diff --git a/main.ipynb b/main.ipynb index b05630a..5e7c346 100644 --- a/main.ipynb +++ b/main.ipynb @@ -37,19 +37,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def greater(a,b):\n", - "#your code here" + "#your code here\n", + "#if statement for checking which number is greater\n", + " if a > b:\n", + " return a\n", + "#else statement for returning the other number\n", + " else:\n", + " return b\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'test_greater' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[14]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m# This will test your function \u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[43mtest_greater\u001b[49m(greater)\n", + "\u001b[31mNameError\u001b[39m: name 'test_greater' is not defined" + ] + } + ], "source": [ "# This will test your function \n", "test_greater(greater)" @@ -57,11 +75,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "99\n", + "100000000\n" + ] + } + ], "source": [ - "#your code here" + "#your code here\n", + "print(greater (2,99))\n", + "print(greater (1011,100000000))\n" ] }, { @@ -73,21 +102,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ - "#your code here" + "#your code here\n", + "def greatest (lst):\n", + " #start with the first number\n", + " largest=lst[0]\n", + " for num in lst:\n", + " if num > largest:\n", + " largest=num\n", + " return largest" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "502\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_greatest(greatest)" + "result=greatest([4,23,1,99,502,2,4,6,74])\n", + "print(result)" ] }, { @@ -99,24 +144,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "def sum_all(lst):\n", - "#your code here" + "#your code here\n", + " #start with total of 0\n", + " total = 0\n", + " #going through each number in the list \n", + " for num in lst:\n", + " #add each number to total\n", + " total += num\n", + " #return total\n", + " return total" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_sum(sum_all)" + "print(sum_all([1,2,3,4,5,6,7,8,9,10]))" ] }, { @@ -128,24 +189,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "def mult_all(lst):\n", - "#your code here" + "#your code here\n", + "#start from 1\n", + " total = 1\n", + "#going through each number in the list\n", + " for num in lst:\n", + "#multiply each one\n", + " total *= num\n", + "#return total\n", + " return total" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 30, "metadata": { "scrolled": true }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3628800\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_mult(mult_all)" + "print(mult_all([1,2,3,4,5,6,7,8,9,10]))" ] }, { @@ -157,22 +234,42 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def oper_all(arr, oper = \"*\"):\n", - "#your code here" + "#your code here\n", + " if oper ==\"+\":\n", + " total=0\n", + " for num in arr:\n", + " total += num\n", + " return total \n", + " elif oper==\"*\":\n", + " total=1\n", + " for num in arr:\n", + " total *=num\n", + " return total" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55\n", + "3628800\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_operations(oper_all)" + "print(oper_all([1,2,3,4,5,6,7,8,9,10],\"+\"))\n", + "print(oper_all([1,2,3,4,5,6,7,8,9,10],\"*\"))" ] }, { @@ -213,12 +310,31 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "40320\n", + "8683317618811886495518194401280000000\n", + "620448401733239439360000\n", + "1\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_factorial(factorial)" + "def factorial(n):\n", + " result = 1\n", + " for i in range(1, n + 1):\n", + " result = result * i\n", + " return result\n", + "print(factorial(8))\n", + "print(factorial(33))\n", + "print(factorial(24))\n", + "print(factorial(0))" ] }, { @@ -232,22 +348,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def unique(lst_un):\n", - "#your code here" + " #create empty list\n", + " uniques = []\n", + " #loop over each item in the list\n", + " for item in lst_un:\n", + " #check if item is in uniques list, append if not\n", + " if item not in uniques:\n", + " uniques.append(item)\n", + " return uniques" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 40, 9]\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_unique(unique)" + "print(unique([1,2,3,4,5,5,40,5,5,5,5,5,9,2,4,3,2,1]))" ] }, { @@ -260,22 +391,48 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def mode_counter(arr):\n", - "#your code here" + "#your code here\n", + " #Initialize empty dictionary\n", + " counts={} \n", + " #loop through each number in array\n", + " for num in arr:\n", + " if num in counts:\n", + " counts[num] +=1\n", + " else:\n", + " counts[num]=1\n", + " most_common=None\n", + " max_count=0\n", + "\n", + " for num, count in counts.items():\n", + " if count > max_count:\n", + " max_count = count\n", + " most_common = num\n", + " return most_common\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8\n", + "b\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_mode(mode_counter)" + "print(mode_counter([1,2,3,4,5,6,6,7,7,8,8,0,8,8,8,8,9,0,7,5,0,0,0,8,4,2,]))\n", + "print(mode_counter(['a','a','a','b','b','b','g','g','h','j','u','z','r','s','c','c','n','j','g','d','s','a','f','b','b','b']))\n" ] }, { @@ -292,18 +449,42 @@ "metadata": {}, "outputs": [], "source": [ + "#your code here\n", + "# Create function\n", "def st_dev(list_sd):\n", + " # Find the mean\n", + " mean = sum(list_sd) / len(list_sd)\n", + " # Find squared differences\n", + " squared_diffs =[]\n", + " for num in list_sd:\n", + " diff= num-mean\n", + " squared_diffs.append(diff**2)\n", + "# Findmean of squared difference\n", + " variance = sum(squared_diffs) / len(squared_diffs)\n", + " st_dev = variance ** 0.5\n", + " return st_dev\n", + "\n", "#your code here" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2.8722813232690143\n", + "3.960744879438715\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_stdev(st_dev)" + "print(st_dev([1,2,3,4,5,6,7,8,9,10]))\n", + "print(st_dev([1,0,2,10]))" ] }, { @@ -315,22 +496,41 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "def pangram(string):\n", - "#your code here" + " # your code here\n", + " seen = set()\n", + " for ch in string.lower():\n", + " if \"a\" <= ch <= \"z\":\n", + " seen.add(ch)\n", + " if len(seen) == 26:\n", + " return True\n", + " return len(seen) == 26" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "False\n", + "True\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_pangram(pangram)" + "print(pangram(\"I went to a birthday party on a ship and I saw a zebra crossing the road\"))\n", + "print(pangram(\"I have a question!\"))\n", + "print(pangram(\"I know the alphabet: abcdefghijklmnopqrstuvwxyz!\"))" ] }, { @@ -344,22 +544,55 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 53, "metadata": {}, "outputs": [], "source": [ "def sort_alpha(string):\n", - "#your code here" + "#your code here\n", + " words=[]\n", + " #Split the string into words\n", + " current=\"\"\n", + " for ch in string:\n", + " if ch != \",\":\n", + " current += ch\n", + " else:\n", + " words.append(current)\n", + " current = \"\"\n", + " #Append the last word\n", + " words.append(current)\n", + " #Sort the words\n", + " sorted_words = sorted(words)\n", + " #Join the words back into a single string\n", + " result = \"\"\n", + " for i in range(len(sorted_words)):\n", + " result += sorted_words[i]\n", + " if i != len(sorted_words) - 1:\n", + " result += \",\"\n", + " return result\n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Anaconda,Python,Rattlesnake,Sniper\n", + "Alligator,Bear,Cat,Dog,Monkey,Zebra\n", + "Chicago,Houston,Los Angeles,New York,San Francisco,Tokyo\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_alpha(sort_alpha)" + "print(sort_alpha(\"Python,Anaconda,Sniper,Rattlesnake\"))\n", + "print(sort_alpha(\"Zebra,Alligator,Monkey,Dog,Cat,Bear\"))\n", + "print(sort_alpha(\"Tokyo,New York,San Francisco,Los Angeles,Chicago,Houston\"))\n" ] }, { @@ -371,28 +604,83 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "def check_pass(password):\n", - "#your code here" + "#your code here\n", + " if len(password) < 8:\n", + " return False\n", + " #Initialize conditions\n", + " has_upper = False\n", + " has_lower = False\n", + " has_digit = False\n", + " has_special = False\n", + " has_special = False\n", + " #Check each character in the password\n", + " for ch in password:\n", + " #Check for each condition\n", + " if ch.isupper():\n", + " has_upper = True\n", + " elif ch.islower():\n", + " has_lower = True\n", + " elif ch.isdigit():\n", + " has_digit = True\n", + " else:\n", + " has_special = True\n", + " #Return True if all conditions are met\n", + " if has_lower and has_upper and has_digit and has_special:\n", + " return True\n", + " else:\n", + " return False\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 68, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(check_pass(\"Forgot1P@ssword!\"))" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "False\n", + "True\n" + ] + } + ], "source": [ "# This will test your function \n", - "test_pass(check_pass)" + "print(check_pass(\"Forgot1P@ssword!\"))\n", + "print(check_pass(\"Password123ABC\"))\n", + "print(check_pass(\"Short1!\"))\n", + "print(check_pass(\"n20424982N1!\"))" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -406,12 +694,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } + "version": "3.13.5" } }, "nbformat": 4,