|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "1. Write a Python function that takes a list of words and reytrns the length of the longest one" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 60, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [ |
| 15 | + { |
| 16 | + "data": { |
| 17 | + "text/plain": [ |
| 18 | + "(8, 'resberry')" |
| 19 | + ] |
| 20 | + }, |
| 21 | + "execution_count": 60, |
| 22 | + "metadata": {}, |
| 23 | + "output_type": "execute_result" |
| 24 | + } |
| 25 | + ], |
| 26 | + "source": [ |
| 27 | + "list1 = ['apple', 'banana', 'resberry', 'avacado']\n", |
| 28 | + "def get_longest_word(word_list):\n", |
| 29 | + " max_len = 0\n", |
| 30 | + " for word in word_list:\n", |
| 31 | + " if len(word) > max_len:\n", |
| 32 | + " max_len = len(word)\n", |
| 33 | + " result = word\n", |
| 34 | + " return max_len, result\n", |
| 35 | + "\n", |
| 36 | + "get_longest_word(list1)" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "2. Write a Python program to count the number of characters in a string" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": 68, |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [ |
| 51 | + { |
| 52 | + "data": { |
| 53 | + "text/plain": [ |
| 54 | + "11" |
| 55 | + ] |
| 56 | + }, |
| 57 | + "execution_count": 68, |
| 58 | + "metadata": {}, |
| 59 | + "output_type": "execute_result" |
| 60 | + } |
| 61 | + ], |
| 62 | + "source": [ |
| 63 | + "text = \"hello world\"\n", |
| 64 | + "def count_chars(txt):\n", |
| 65 | + " result = 0\n", |
| 66 | + " for char in txt:\n", |
| 67 | + " result += 1\n", |
| 68 | + " return result\n", |
| 69 | + "count_chars(text)" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 76, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [ |
| 77 | + { |
| 78 | + "data": { |
| 79 | + "text/plain": [ |
| 80 | + "11" |
| 81 | + ] |
| 82 | + }, |
| 83 | + "execution_count": 76, |
| 84 | + "metadata": {}, |
| 85 | + "output_type": "execute_result" |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [ |
| 89 | + "# second \n", |
| 90 | + "text = \"hello world\"\n", |
| 91 | + "def count_chars2(txt):\n", |
| 92 | + " return len(list(txt))\n", |
| 93 | + "count_chars2(text)" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "markdown", |
| 98 | + "metadata": {}, |
| 99 | + "source": [ |
| 100 | + "3. Write a Python program to sort (ascending and descending) a dictionary by value" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": 2, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [ |
| 108 | + { |
| 109 | + "name": "stdout", |
| 110 | + "output_type": "stream", |
| 111 | + "text": [ |
| 112 | + "{'a': 1, 'b': 100, 'c': 77, 'd': 66, 'e': 55}\n" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "data": { |
| 117 | + "text/plain": [ |
| 118 | + "[('a', 1), ('e', 55), ('d', 66), ('c', 77), ('b', 100)]" |
| 119 | + ] |
| 120 | + }, |
| 121 | + "execution_count": 2, |
| 122 | + "metadata": {}, |
| 123 | + "output_type": "execute_result" |
| 124 | + } |
| 125 | + ], |
| 126 | + "source": [ |
| 127 | + "dict1 = {'a':1, 'b':100, 'c':77, 'd':66, 'e':55}\n", |
| 128 | + "print(dict1)\n", |
| 129 | + "sorted(dict1.items(),key=lambda item:item[1])\n", |
| 130 | + "\n" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "markdown", |
| 135 | + "metadata": {}, |
| 136 | + "source": [ |
| 137 | + "4. Write a Python program to change a givern string to a new string where the first and last chars have been exchanged." |
| 138 | + ] |
| 139 | + }, |
| 140 | + { |
| 141 | + "cell_type": "code", |
| 142 | + "execution_count": 38, |
| 143 | + "metadata": {}, |
| 144 | + "outputs": [ |
| 145 | + { |
| 146 | + "name": "stdout", |
| 147 | + "output_type": "stream", |
| 148 | + "text": [ |
| 149 | + "ABCello worlXYZ\n" |
| 150 | + ] |
| 151 | + } |
| 152 | + ], |
| 153 | + "source": [ |
| 154 | + "text1 = 'hello world'\n", |
| 155 | + "list1 = list(text1)\n", |
| 156 | + "list1[0] = 'ABC'\n", |
| 157 | + "list1[-1] = 'XYZ'\n", |
| 158 | + "text2 = ''.join(list1)\n", |
| 159 | + "print(text2)" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "markdown", |
| 164 | + "metadata": {}, |
| 165 | + "source": [ |
| 166 | + "5. Write a Python function that takes two lists and returns True if they have at least one common member." |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": 66, |
| 172 | + "metadata": {}, |
| 173 | + "outputs": [ |
| 174 | + { |
| 175 | + "name": "stdout", |
| 176 | + "output_type": "stream", |
| 177 | + "text": [ |
| 178 | + "{'apple', 'banana'}\n", |
| 179 | + "true\n" |
| 180 | + ] |
| 181 | + } |
| 182 | + ], |
| 183 | + "source": [ |
| 184 | + "list1 = ['apple', 'banana', 'resberry', 'avacado']\n", |
| 185 | + "list2 = ['apple', 'banana', 'strawberry', 'cherry']\n", |
| 186 | + "list3 = set(list1).intersection(set(list2))\n", |
| 187 | + "print(list3)\n", |
| 188 | + "if list3:\n", |
| 189 | + " print(\"true\")\n", |
| 190 | + "else:\n", |
| 191 | + " print(\"false\")\n", |
| 192 | + " " |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "markdown", |
| 197 | + "metadata": {}, |
| 198 | + "source": [ |
| 199 | + "6. Write a Python function that return the minimum number of coins ($0.01, $0.1, $0.25, $1) that make a given value." |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "cell_type": "code", |
| 204 | + "execution_count": 57, |
| 205 | + "metadata": {}, |
| 206 | + "outputs": [ |
| 207 | + { |
| 208 | + "name": "stdout", |
| 209 | + "output_type": "stream", |
| 210 | + "text": [ |
| 211 | + "[88, 3, 1, 3]\n", |
| 212 | + "{'$1': 88, '$0.25': 3, '$0.1': 1, '$0.01': 3}\n" |
| 213 | + ] |
| 214 | + } |
| 215 | + ], |
| 216 | + "source": [ |
| 217 | + "def main():\n", |
| 218 | + " return_change = 88.88 # input value here\n", |
| 219 | + " coins_list = [1, 0.25, 0.1, 0.01]\n", |
| 220 | + " coin1 = int(return_change/coins_list[0])\n", |
| 221 | + " cent1 = round(return_change - coin1, 2)\n", |
| 222 | + " coin2 = int(cent1/coins_list[1])\n", |
| 223 | + " cent2 = round(cent1 - coin2*coins_list[1], 2)\n", |
| 224 | + " coin3 = int(cent2/coins_list[2])\n", |
| 225 | + " cent3 = round(cent2 - coin3*coins_list[2], 2)\n", |
| 226 | + " coin4 = int(cent3/coins_list[3])\n", |
| 227 | + " return_coins = [coin1, coin2, coin3, coin4]\n", |
| 228 | + " return_dict = {\"$1\": coin1, \"$0.25\": coin2, \"$0.1\": coin3, \"$0.01\":coin4}\n", |
| 229 | + " print(return_coins)\n", |
| 230 | + " print(return_dict)\n", |
| 231 | + "if __name__ == \"__main__\":\n", |
| 232 | + " main()" |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "code", |
| 237 | + "execution_count": null, |
| 238 | + "metadata": {}, |
| 239 | + "outputs": [], |
| 240 | + "source": [] |
| 241 | + } |
| 242 | + ], |
| 243 | + "metadata": { |
| 244 | + "kernelspec": { |
| 245 | + "display_name": "Python 3", |
| 246 | + "language": "python", |
| 247 | + "name": "python3" |
| 248 | + }, |
| 249 | + "language_info": { |
| 250 | + "codemirror_mode": { |
| 251 | + "name": "ipython", |
| 252 | + "version": 3 |
| 253 | + }, |
| 254 | + "file_extension": ".py", |
| 255 | + "mimetype": "text/x-python", |
| 256 | + "name": "python", |
| 257 | + "nbconvert_exporter": "python", |
| 258 | + "pygments_lexer": "ipython3", |
| 259 | + "version": "3.7.4" |
| 260 | + } |
| 261 | + }, |
| 262 | + "nbformat": 4, |
| 263 | + "nbformat_minor": 2 |
| 264 | +} |
0 commit comments