|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# **Python Usecases Interview Questions**" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "`1.Given two lists, write a function that returns the elements that are common to both lists.`" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 4, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [ |
| 22 | + { |
| 23 | + "data": { |
| 24 | + "text/plain": [ |
| 25 | + "[3, 4, 5]" |
| 26 | + ] |
| 27 | + }, |
| 28 | + "execution_count": 4, |
| 29 | + "metadata": {}, |
| 30 | + "output_type": "execute_result" |
| 31 | + } |
| 32 | + ], |
| 33 | + "source": [ |
| 34 | + "# given lists \n", |
| 35 | + "l1 = [1, 2, 3, 4, 5]\n", |
| 36 | + "l2 = [3, 4, 5, 6, 7]\n", |
| 37 | + "\n", |
| 38 | + "# function for the commmon elements\n", |
| 39 | + "def find_common_element(list1,list2):\n", |
| 40 | + " common_element = list(set(list1) & set(list2))\n", |
| 41 | + " return common_element\n", |
| 42 | + "\n", |
| 43 | + "find_common_element(l1,l2)" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "metadata": {}, |
| 49 | + "source": [ |
| 50 | + "`2. Write a Python function to merge two dictionaries. If both dictionaries have the same key, prefer the second dictionary's value.`" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": 5, |
| 56 | + "metadata": {}, |
| 57 | + "outputs": [ |
| 58 | + { |
| 59 | + "name": "stdout", |
| 60 | + "output_type": "stream", |
| 61 | + "text": [ |
| 62 | + "{'a': 1, 'b': 3, 'c': 4}\n" |
| 63 | + ] |
| 64 | + } |
| 65 | + ], |
| 66 | + "source": [ |
| 67 | + "# function \n", |
| 68 | + "def merge_two_dictionaries(dict1,dict2):\n", |
| 69 | + " merged = dict1.copy()\n", |
| 70 | + " merged.update(dict2)\n", |
| 71 | + " return merged\n", |
| 72 | + "\n", |
| 73 | + "dict1 = {'a': 1, 'b': 2}\n", |
| 74 | + "dict2 = {'b': 3, 'c': 4}\n", |
| 75 | + "print(merge_two_dictionaries(dict1, dict2)) " |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "markdown", |
| 80 | + "metadata": {}, |
| 81 | + "source": [ |
| 82 | + "`3.Given a list of numbers, write a function to compute the mean, median, and mode.`" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": 7, |
| 88 | + "metadata": {}, |
| 89 | + "outputs": [ |
| 90 | + { |
| 91 | + "name": "stdout", |
| 92 | + "output_type": "stream", |
| 93 | + "text": [ |
| 94 | + "{'mean': 3.888888888888889, 'median': 4, 'mode': 5}\n" |
| 95 | + ] |
| 96 | + } |
| 97 | + ], |
| 98 | + "source": [ |
| 99 | + "# list\n", |
| 100 | + "numbers = [1, 2, 3, 4, 4, 5, 5, 5, 6]\n", |
| 101 | + "\n", |
| 102 | + "from statistics import mean, median, mode\n", |
| 103 | + "def central_measures(numbers):\n", |
| 104 | + " return {\n", |
| 105 | + " 'mean': mean(numbers),\n", |
| 106 | + " 'median': median(numbers),\n", |
| 107 | + " 'mode': mode(numbers) \n", |
| 108 | + " }\n", |
| 109 | + " \n", |
| 110 | + "print(central_measures(numbers))" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "markdown", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "`4.Write a function to check if a given string is a palindrome.`" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "code", |
| 122 | + "execution_count": 8, |
| 123 | + "metadata": {}, |
| 124 | + "outputs": [ |
| 125 | + { |
| 126 | + "name": "stdout", |
| 127 | + "output_type": "stream", |
| 128 | + "text": [ |
| 129 | + "True\n" |
| 130 | + ] |
| 131 | + } |
| 132 | + ], |
| 133 | + "source": [ |
| 134 | + "def is_palindrome(s):\n", |
| 135 | + " return s == s[::-1]\n", |
| 136 | + "\n", |
| 137 | + "string = \"radar\"\n", |
| 138 | + "print(is_palindrome(string))" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "markdown", |
| 143 | + "metadata": {}, |
| 144 | + "source": [ |
| 145 | + "`5.Write a function to compute the factorial of a number using iteration.`" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "cell_type": "code", |
| 150 | + "execution_count": 9, |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [ |
| 153 | + { |
| 154 | + "name": "stdout", |
| 155 | + "output_type": "stream", |
| 156 | + "text": [ |
| 157 | + "120\n" |
| 158 | + ] |
| 159 | + } |
| 160 | + ], |
| 161 | + "source": [ |
| 162 | + "def factorial_iterative(n):\n", |
| 163 | + " result = 1\n", |
| 164 | + " for i in range(2, n+1):\n", |
| 165 | + " result *= i\n", |
| 166 | + " return result\n", |
| 167 | + "\n", |
| 168 | + "number = 5\n", |
| 169 | + "print(factorial_iterative(number)) \n" |
| 170 | + ] |
| 171 | + } |
| 172 | + ], |
| 173 | + "metadata": { |
| 174 | + "kernelspec": { |
| 175 | + "display_name": "base", |
| 176 | + "language": "python", |
| 177 | + "name": "python3" |
| 178 | + }, |
| 179 | + "language_info": { |
| 180 | + "codemirror_mode": { |
| 181 | + "name": "ipython", |
| 182 | + "version": 3 |
| 183 | + }, |
| 184 | + "file_extension": ".py", |
| 185 | + "mimetype": "text/x-python", |
| 186 | + "name": "python", |
| 187 | + "nbconvert_exporter": "python", |
| 188 | + "pygments_lexer": "ipython3", |
| 189 | + "version": "3.9.18" |
| 190 | + } |
| 191 | + }, |
| 192 | + "nbformat": 4, |
| 193 | + "nbformat_minor": 2 |
| 194 | +} |
0 commit comments