diff --git a/12_09_practice.ipynb b/12_09_practice.ipynb new file mode 100644 index 0000000..21fdfb5 --- /dev/null +++ b/12_09_practice.ipynb @@ -0,0 +1,1465 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "9gU2J0LrNqeo" + }, + "source": [ + " # Python Practice \n", + " \n", + " ### Ndugba" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sZTZXlXENqes" + }, + "source": [ + "## Problem # 1\n", + " Biggie Size - Given a list, write a function that changes all \n", + " positive numbers in the list to \"big\". Example: make_it_big([-1, 3, 5, -5]) \n", + " returns that same list, #changed to [-1, \"big\", \"big\", -5]." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "id": "RAxBiZ8iNqet" + }, + "outputs": [], + "source": [ + "##how can i remove the none at the end? \n", + "def positive_numbers(list):\n", + " for number in list:\n", + " if number >0:\n", + " print(\"big\")\n", + " else:\n", + " print(number)\n", + " return" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "-1\n", + "big\n", + "big\n", + "-5\n", + "None\n" + ] + } + ], + "source": [ + "example1= [-1,3,5,-5]\n", + "print(positive_numbers(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def positive_numbers(list):\n", + " for index, value in enumerate(list):\n", + " if value>0:\n", + " list[index]=\"big\"\n", + " \n", + " return list" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, 'big', 'big', -5]\n" + ] + } + ], + "source": [ + "example1= [-1,3,5,-5]\n", + "print(positive_numbers(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "def positive_numbers(list):\n", + " for number in range(len(list)):\n", + " if list[number] >0:\n", + " list[number]= \"big\"\n", + " return list" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": { + "id": "8oZQrYtRNqeu" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, 'big', 'big', -5]\n" + ] + } + ], + "source": [ + "example1= [-1,3,5,-5]\n", + "print(positive_numbers(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "gPn6w5wUNqev" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JzfvnT7pNqev" + }, + "source": [ + "## Problem # 2\n", + "\n", + "Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number)." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "5iCwby6zNqew" + }, + "source": [ + "unsure what question is asking- shouldnt the above response be (-1,1,1,2) ? or (-1, 0, 1, 2)?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "k5UN1dArNqew" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "mfBXyk9DNqew" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tSB-tUOsNqex" + }, + "source": [ + "## Problem # 3\n", + "#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "id": "jWqV7ZlbNqex" + }, + "outputs": [], + "source": [ + "\n", + "def sum_total(list):\n", + " return sum(list)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "example1= [-1,3,5,-5]\n", + "print(sum_total(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "16\n" + ] + } + ], + "source": [ + "example1= [1,3,5,7]\n", + "print(sum_total(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": { + "id": "1GzuwOWkNqex" + }, + "outputs": [], + "source": [ + "def sum(list):\n", + " counter = 0\n", + " for number in range(len(list)):\n", + " counter = counter + list[number]\n", + " return counter" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n" + ] + } + ], + "source": [ + "example1= [-1,3,5,-5]\n", + "print(sum(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": { + "id": "g-vN8jLJNqex" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "example1= [1,2,3,4]\n", + "print(sum(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "def sum_1(list):\n", + " counter = 0\n", + " for number in list:\n", + " counter = counter + number\n", + " return counter" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "29\n" + ] + } + ], + "source": [ + "example1= [1,21,3,4]\n", + "print(sum_1(example1))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LwpqDMt9Nqey" + }, + "source": [ + "## Problem # 4\n", + "#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "id": "kYXLedkTNqey" + }, + "outputs": [], + "source": [ + "def average(list):\n", + " return sum(list)/len(list)\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "id": "kRhAbZeJNqey" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5.0\n" + ] + } + ], + "source": [ + "example1 = [2,4,6,8]\n", + "print(average(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": { + "id": "HEsfr27nNqey", + "outputId": "19941bed-04e5-44f3-be9b-70d1a7970575" + }, + "outputs": [], + "source": [ + "def average_1(list):\n", + " sum=0\n", + " for number in list:\n", + " sum =sum + number\n", + " return sum/len(list)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5.0\n" + ] + } + ], + "source": [ + "example1 = [2,4,6,8]\n", + "print(average(example1))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YAKtJW_2Nqez" + }, + "source": [ + "## Problem # 5\n", + "#Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "id": "uePXZ34DNqez" + }, + "outputs": [], + "source": [ + "def length(list):\n", + " return len(list)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "id": "hwrl1___Nqez" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "print(length(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "id": "k9AvSWdeNqez", + "outputId": "5697a6d9-921d-46a0-9b78-9fe902b48279" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4,6,7,8,11,21]\n", + "print(length(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "hMW3LplCNqe0" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "N3d-hUulNqe0" + }, + "source": [ + "## Problem # 6\n", + "#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": { + "id": "rSX2H0lWNqe0" + }, + "outputs": [], + "source": [ + "def minimum(list):\n", + " return min(list)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "Jz6AZ0NNNqe0", + "outputId": "bee9951f-7111-408d-a428-fd5fda0cc448" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "-3\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "example2 = [-1,-2,-3]\n", + "#example3 = [ , ]\n", + "print(min(example1))\n", + "print(min(example2))\n", + "#print(min(example3))" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "id": "93c4igrJNqe1", + "outputId": "2c27022c-4d15-46ee-9740-c48f04b2f60f" + }, + "outputs": [], + "source": [ + "def minimum_1(list):\n", + " if len(list) > 0:\n", + " return min(list)\n", + " else: \n", + " return False\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "-3\n", + "False\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "example2 = [-1,-2,-3]\n", + "example3 = [ ]\n", + "print(minimum_1(example1))\n", + "print(minimum_1(example2))\n", + "print(minimum_1(example3))" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "def minimum_2(list):\n", + " if list!=[]:\n", + " return min(list)\n", + " else: \n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "-3\n", + "False\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "example2 = [-1,-2,-3]\n", + "example3 = [ ]\n", + "print(minimum_2(example1))\n", + "print(minimum_2(example2))\n", + "print(minimum_2(example3))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ESt1sSVYNqe1" + }, + "source": [ + "## Problem # 7\n", + "#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "id": "Jw7vTtEYNqe1" + }, + "outputs": [], + "source": [ + "def maximum(list):\n", + " return max(list)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "id": "SO5zJ9bpNqe1" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "-1\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "example2 = [-1,-2,-3]\n", + "#example3 = [ , ]\n", + "print(maximum(example1))\n", + "print(maximum(example2))\n", + "#print(maximum(example3))" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "def maximum_1(list):\n", + " if len(list) > 0:\n", + " return max(list)\n", + " else: \n", + " return False\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "-1\n", + "False\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "example2 = [-1,-2,-3]\n", + "example3 = [ ]\n", + "print(maximum_1(example1))\n", + "print(maximum_1(example2))\n", + "print(maximum_1(example3))" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "def maximum_2(list):\n", + " if list!=[]:\n", + " return max(list)\n", + " else: \n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "-1\n", + "False\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "example2 = [-1,-2,-3]\n", + "example3 = [ ]\n", + "print(maximum_2(example1))\n", + "print(maximum_2(example2))\n", + "print(maximum_2(example3))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wPMfhkmoNqe1" + }, + "source": [ + "## Problem # 8\n", + "Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": { + "id": "YnqAe676Nqe1" + }, + "outputs": [], + "source": [ + "def stats(list):\n", + " return max(list), min(list), (sum(list)/len(list)), sum(list)" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "id": "qafDcDvENqe2" + }, + "outputs": [], + "source": [ + "example1 = [1,2,3,4]" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "id": "Q_Gh02VrNqe2", + "outputId": "0f01d690-0dfb-4d3f-c49c-101f3a04344e" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(40, 10, 25.0, 100)\n", + "(35, -15, 8.75, 35)\n", + "(5, 1, 3.0, 15)\n" + ] + } + ], + "source": [ + "example1 = [10,20,30,40]\n", + "example2 = [-15,25,35,-10]\n", + "example3 = [1,2,3,4,5]\n", + "print(stats(example1))\n", + "print(stats(example2))\n", + "print(stats(example3))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Eb0IPondNqe2" + }, + "source": [ + "## Problem # 9\n", + "#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": { + "id": "Q4EoccmGNqe2" + }, + "outputs": [], + "source": [ + "def reverse(list):\n", + " return(list[::-1])" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "id": "qm9bN5w1Nqe2" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[4, 3, 2, 1]\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "print(reverse(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse_1(list):\n", + " #item = len(list) -1\n", + " for item in range(len(list)):\n", + " if len(list): \n", + " print (list[-(item+1)])\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "3\n", + "2\n", + "1\n", + "None\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "print(reverse_1(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse_2(list):\n", + " #item = len(list) -1\n", + " for item in range(len(list)-1,-1,-1):\n", + " print (list[item])\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "3\n", + "2\n", + "1\n", + "None\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "print(reverse_2(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse_3(list):\n", + " for item in reversed(list):\n", + " print(item)" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "3\n", + "2\n", + "1\n", + "None\n" + ] + } + ], + "source": [ + "example1 = [1,2,3,4]\n", + "print(reverse_3(example1))" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "def reverse_4(list):\n", + " new_list=[]\n", + " for item in range(len(list)-1,-1,-1):\n", + " new_list.append(item)\n", + " return new_list" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 2, 1, 0]\n" + ] + } + ], + "source": [ + "example4 = [1,2,3,4]\n", + "print(reverse_4(example4))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "c5y04VShNqe3" + }, + "source": [ + "## Problem # 10\n", + "#Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome." + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": { + "id": "EITBREfpNqe3" + }, + "outputs": [], + "source": [ + "def palindrome(string):\n", + " string = string\n", + " new_string = string[::-1]\n", + " if new_string == string:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": { + "id": "Y4fP57RKNqe3" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "string = 'radar'\n", + "print(palindrome(string))\n", + "string2 = 'Borrow or rob'\n", + "print(palindrome(string2))" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": { + "id": "AgIETBIvNqe3" + }, + "outputs": [], + "source": [ + "def palindrome_1(string):\n", + " string = string\n", + " new_string = reversed(string)\n", + " if list(new_string) == list(string):\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "string3 = 'madam'\n", + "print(palindrome_1(string3))\n", + "string2 = 'peaceful'\n", + "print(palindrome_1(string2))" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [], + "source": [ + "def palindrome_2(string):\n", + " string_lower = string.lower()\n", + " string_lower_no_punctuation = ''.join(char for char in string_lower if char.isalnum())\n", + " new_string = string_lower_no_punctuation[::-1]\n", + " if new_string == string_lower_no_punctuation:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "True\n", + "True\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "string = 'madam'\n", + "print(palindrome_2(string))\n", + "string2 = 'peaceful'\n", + "print(palindrome_2(string2))\n", + "string3 = 'Radar'\n", + "print(palindrome_2(string3))\n", + "string4 = 'Do geese see God?'\n", + "print(palindrome_2(string4))\n", + "string5 = 'Mr. Owl ate my metal worm!'\n", + "print(palindrome_2(string5))\n", + "string6 = '12 34 5432'\n", + "print(palindrome_2(string6))\n", + "string7 = '02-02/2020'\n", + "print(palindrome_2(string7))" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [], + "source": [ + "def palindrome_3(string):\n", + " string_lower = string.lower()\n", + " string_lower_no_punctuation = ''.join(char for char in string_lower if char.isalnum())\n", + " new_string = reversed(string_lower_no_punctuation)\n", + " if list(new_string) == list(string_lower_no_punctuation):\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n", + "True\n", + "True\n", + "True\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "string = 'madam'\n", + "print(palindrome_3(string))\n", + "string2 = 'peaceful'\n", + "print(palindrome_3(string2))\n", + "string3 = 'Radar'\n", + "print(palindrome_3(string3))\n", + "string4 = 'Do geese see God?'\n", + "print(palindrome_3(string4))\n", + "string5 = 'Mr. Owl ate my metal worm!'\n", + "print(palindrome_3(string5))\n", + "string6 = '12 34 5432'\n", + "print(palindrome_3(string6))\n", + "string7 = '02-02/2020'\n", + "print(palindrome_3(string7))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ot4dYFZQNqe4" + }, + "source": [ + "## Problem # 11\n", + "#Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions:\n", + " #If the number is a multiple of 3, print “Fizz” instead of the number.\n", + " #If the number is a multiple of 5, print “Buzz” instead of the number.\n", + " #If the number is a multiple of 3 and 5, print “FizzBuzz” instead of the number." + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": { + "id": "uwMS95AbNqe4" + }, + "outputs": [], + "source": [ + "def fizzbuzz(number):\n", + " while number >=1 and number <=100:\n", + " if number %3 ==0 and number %5 ==0:\n", + " number = \"FizzBuzz\"\n", + " elif number %3 ==0:\n", + " number= \"Fizz\"\n", + " elif number %5 ==0:\n", + " number= \"Buzz\"\n", + " else:\n", + " number = number\n", + " return number\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fizz\n", + "FizzBuzz\n", + "13\n", + "Buzz\n" + ] + } + ], + "source": [ + "number1 = (33)\n", + "number2 = (15)\n", + "number3 = (13)\n", + "number4 = (25)\n", + "print(fizzbuzz(number1))\n", + "print(fizzbuzz(number2))\n", + "print(fizzbuzz(number3))\n", + "print(fizzbuzz(number4))\n" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "Fizz\n", + "4\n", + "Buzz\n", + "Fizz\n", + "7\n", + "8\n", + "Fizz\n", + "Buzz\n", + "11\n", + "Fizz\n", + "13\n", + "14\n", + "FizzBuzz\n", + "16\n", + "17\n", + "Fizz\n", + "19\n", + "Buzz\n", + "Fizz\n", + "22\n", + "23\n", + "Fizz\n", + "Buzz\n", + "26\n", + "Fizz\n", + "28\n", + "29\n", + "FizzBuzz\n", + "31\n", + "32\n", + "Fizz\n", + "34\n", + "Buzz\n", + "Fizz\n", + "37\n", + "38\n", + "Fizz\n", + "Buzz\n", + "41\n", + "Fizz\n", + "43\n", + "44\n", + "FizzBuzz\n", + "46\n", + "47\n", + "Fizz\n", + "49\n", + "Buzz\n", + "Fizz\n", + "52\n", + "53\n", + "Fizz\n", + "Buzz\n", + "56\n", + "Fizz\n", + "58\n", + "59\n", + "FizzBuzz\n", + "61\n", + "62\n", + "Fizz\n", + "64\n", + "Buzz\n", + "Fizz\n", + "67\n", + "68\n", + "Fizz\n", + "Buzz\n", + "71\n", + "Fizz\n", + "73\n", + "74\n", + "FizzBuzz\n", + "76\n", + "77\n", + "Fizz\n", + "79\n", + "Buzz\n", + "Fizz\n", + "82\n", + "83\n", + "Fizz\n", + "Buzz\n", + "86\n", + "Fizz\n", + "88\n", + "89\n", + "FizzBuzz\n", + "91\n", + "92\n", + "Fizz\n", + "94\n", + "Buzz\n", + "Fizz\n", + "97\n", + "98\n", + "Fizz\n", + "Buzz\n" + ] + } + ], + "source": [ + "# if I wanted to return \n", + "for number in range(1,101):\n", + " if number %3 ==0 and number %5 ==0:\n", + " number = \"FizzBuzz\"\n", + " elif number %3 ==0:\n", + " number= \"Fizz\"\n", + " elif number %5 ==0:\n", + " number= \"Buzz\"\n", + " else:\n", + " number = number\n", + " print(number)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "FQhz6vTfNqe4" + }, + "source": [ + "## Problem # 12\n", + "#Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is,\n", + " #F(0) = 0, F(1) = 1\n", + " #F(n) = F(n - 1) + F(n - 2), for n > 1.\n", + " #Create a function that accepts any number and will create a sequence based on the fibonacci sequence." + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": { + "id": "dvuur7iFNqe5" + }, + "outputs": [], + "source": [ + "#assuming postive numbers\n", + "def Fibonacci(number):\n", + " if number < 0:\n", + " return False\n", + " elif number ==0:\n", + " number =0\n", + " elif number ==1:\n", + " number= 1\n", + " else:\n", + " number = (number - 1) + (number - 2)\n", + " return number" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": { + "id": "I3vi-OCfNqe5" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "0\n", + "1\n", + "7\n", + "57\n" + ] + } + ], + "source": [ + "number1 = (-5)\n", + "number2 = (0)\n", + "number3 = (1)\n", + "number4 = (5)\n", + "number5 = (30)\n", + "print(Fibonacci(-5))\n", + "print(Fibonacci(0))\n", + "print(Fibonacci(1))\n", + "print(Fibonacci(5))\n", + "print(Fibonacci(30))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "pzu61QNbNqe5", + "outputId": "568d65ce-2007-42b8-dec4-8ba1bdadb04b" + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "8bb2S7mPNqe5" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.9.13" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/12_09_practice.pdf b/12_09_practice.pdf new file mode 100644 index 0000000..45ac4f5 Binary files /dev/null and b/12_09_practice.pdf differ diff --git a/12_09_practice.py b/12_09_practice.py index 7914563..ba77187 100644 --- a/12_09_practice.py +++ b/12_09_practice.py @@ -1,29 +1,456 @@ -#Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5]. - -#Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number). - -#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10 - -#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5 - -#Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4 - -#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. -# -#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. - -#Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list. - -#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews. - -#Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome. - -#Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions: - #If the number is a multiple of 3, print “Fizz” instead of the number. - #If the number is a multiple of 5, print “Buzz” instead of the number. - #If the number is a multiple of 3 and 5, print “FizzBuzz” instead of the number. - -#Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is, - #F(0) = 0, F(1) = 1 - #F(n) = F(n - 1) + F(n - 2), for n > 1. - #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. \ No newline at end of file +#!/usr/bin/env python +# coding: utf-8 + +# # Python Practice +# +# ### Ndugba + +# ## Problem # 1 +# Biggie Size - Given a list, write a function that changes all +# positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) +# returns that same list, #changed to [-1, "big", "big", -5]. + +# In[1]: + + +def positive_numbers(list): + for number in range(len(list)): + if list[number] >0: + list[number] ="big" + else: + return list + return list + + +# In[2]: + + +example1= [-1,3,5,-5] + + +# In[3]: + + +print(positive_numbers(example1)) + + +# In[ ]: + + + + + +# ## Problem # 2 +# +# Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number). + +# unsure what question is asking- shouldnt the above response be (-1,1,1,2) ? or (-1, 0, 1, 2)? + +# In[ ]: + + + + + +# In[ ]: + + + + + +# ## Problem # 3 +# #SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10 + +# In[4]: + + +##why doesnt this work?? but works below? +def sumtotal(list): + return sum(list) + + +# In[5]: + + +def sum(list): + counter = 0 + for number in range(len(list)): + counter = counter + list[number] + return counter + + +# In[6]: + + +example1= [1,2,3,4] + + +# In[7]: + + +print(sumtotal(example1)) + + +# In[8]: + + +print(sum(example1)) + + +# In[ ]: + + + + + +# ## Problem # 4 +# #Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5 + +# In[9]: + + +def average(list): + return sum(list)/len(list) + + + + +# In[10]: + + +example1 = [1,2,3,4] + + +# In[11]: + + +print(average(example1)) + + +# In[ ]: + + + + + +# ## Problem # 5 +# #Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4 + +# In[12]: + + +def length(list): + return len(list) + + +# In[13]: + + +example1 = [1,2,3,4] + + +# In[14]: + + +print(length(example1)) + + +# In[ ]: + + + + + +# ## Problem # 6 +# #Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. +# + +# In[15]: + + +def min(list): + return min(list) + + +# In[16]: + + +example1 = [1,2,3,4] +example2 = [-1,-2,-3] +example3 = [ , ] + + +# In[18]: + + +print(min(example1)) +print(min(example2)) +#print(min(example3)) + + +# ## Problem # 7 +# #Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. + +# In[19]: + + +def max(list): + return max(list) + + +# In[21]: + + +example1 = [1,2,3,4] +example2 = [-1,-2,-3] +#example3 = [ , ] + + +# In[22]: + + +print(max(example1)) +print(max(example2)) +#print(max(example3)) + + +# ## Problem # 8 +# Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list. + +# In[32]: + + +def stats(list): + return max(list), min(list), (sum(list)/len(list)), sum(list) + + +# In[33]: + + +def stats1(list): + return sum(list) + + +# In[34]: + + +example1 = [1,2,3,4] + + +# In[35]: + + +print(stats(example1)) + + +# In[30]: + + +print(stats1(example1)) + + +# In[31]: + + +print(average(example1)) + + +# ## Problem # 9 +# #ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews. + +# In[27]: + + +def reverse(list): + return(list[::-1]) + + +# In[28]: + + +example1 = [1,2,3,4] + + +# In[29]: + + +print(reverse(example1)) + + +# ## Problem # 10 +# #Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome. + +# In[36]: + + +def palindrome(string): + string = string + new_string = string[::-1] + if new_string == string: + return True + else: + return False + + +# In[37]: + + +string = 'radar' + + +# In[38]: + + +string2 = 'radarl' + + +# In[39]: + + +print(palindrome(string)) + + +# In[40]: + + +print(palindrome(string2)) + + +# ## Problem # 11 +# #Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions: +# #If the number is a multiple of 3, print “Fizz” instead of the number. +# #If the number is a multiple of 5, print “Buzz” instead of the number. +# #If the number is a multiple of 3 and 5, print “FizzBuzz” instead of the number. + +# In[41]: + + +def fizzbuzz(number): + while number >=1 and number <=100: + if number %3 ==0 and number %5 ==0: + number = "FizzBuzz" + elif number %3 ==0: + number= "Fizz" + elif number %5 ==0: + number= "Buzz" + else: + number = number + return number + + + + +# In[42]: + + +# if I wanted to return + +def fizzbuzz2(list): + for number in range(len(list)): + if list[number] %3 ==0 and list[number] %5 ==0: + list[number] = "FizzBuzz" + elif list[number] %3 ==0: + list[number]= "Fizz" + elif list[number] %5 ==0: + list[number]= "Buzz" + else: + list[number] = list[number] + return list[number] + + + +# In[43]: + + +number1 = (33) +number2 = (15) +number3 = (13) +number4 = (25) + + +# In[44]: + + +number5 = [33, 15, 13, 25, 26, 72] + + +# In[45]: + + +print(fizzbuzz(number1)) +print(fizzbuzz(number2)) +print(fizzbuzz(number3)) +print(fizzbuzz(number4)) + + +# In[46]: + + +print(fizzbuzz2(number5)) + + +# ## Problem # 12 +# #Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is, +# #F(0) = 0, F(1) = 1 +# #F(n) = F(n - 1) + F(n - 2), for n > 1. +# #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. + +# In[47]: + + +#assuming postive numbers +def Fibonacci(number): + if number < 0: + return False + elif number ==0: + number =0 + elif number ==1: + number= 1 + else: + number = (number - 1) + (number - 2) + return number + + +# In[48]: + + +number1 = (-5) +number2 = (0) +number3 = (1) +number4 = (5) +number5 = (30) + + +# In[49]: + + +print(Fibonacci(-5)) +print(Fibonacci(0)) +print(Fibonacci(1)) +print(Fibonacci(5)) +print(Fibonacci(30)) + + +# In[ ]: + + + + + +# In[ ]: + + + + + +# In[ ]: + + + + + +# In[ ]: + + + +