|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## 4.Define a function to convert a binary number to the corresponding decimal number\n", |
| 8 | + "### Test Cases:\n", |
| 9 | + "* binaryToDecimal(1101) -> 12\n", |
| 10 | + "* binaryToDecimal(1010) -> 10\n", |
| 11 | + "* binaryToDecimal(111000) -> 56" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 235, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [ |
| 19 | + { |
| 20 | + "name": "stdout", |
| 21 | + "output_type": "stream", |
| 22 | + "text": [ |
| 23 | + "13\n", |
| 24 | + "10\n", |
| 25 | + "56\n" |
| 26 | + ] |
| 27 | + } |
| 28 | + ], |
| 29 | + "source": [ |
| 30 | + "def BintoDec(B):\n", |
| 31 | + " s=0\n", |
| 32 | + " p=len(B)-1\n", |
| 33 | + " for i in range(len(B)):\n", |
| 34 | + " s=s+((int(B[i])) * (2**p))\n", |
| 35 | + " p=p-1\n", |
| 36 | + " return s\n", |
| 37 | + "print(BintoDec('1101'))\n", |
| 38 | + "print(BintoDec('1010'))\n", |
| 39 | + "print(BintoDec('111000'))" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": null, |
| 45 | + "metadata": {}, |
| 46 | + "outputs": [], |
| 47 | + "source": [] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": null, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [], |
| 54 | + "source": [] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "markdown", |
| 58 | + "metadata": {}, |
| 59 | + "source": [ |
| 60 | + "## 5. Define a function to convert a decimal number to the corresponding binary number\n", |
| 61 | + "\n", |
| 62 | + "\n", |
| 63 | + "### Test Cases:\n", |
| 64 | + "* decimalToBinary(15) -> 1111\n", |
| 65 | + "* decimalToBinary(1) -> 1" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 314, |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [ |
| 73 | + { |
| 74 | + "name": "stdout", |
| 75 | + "output_type": "stream", |
| 76 | + "text": [ |
| 77 | + "[1, 1, 1, 1]\n", |
| 78 | + "[1]\n", |
| 79 | + "[1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0]\n" |
| 80 | + ] |
| 81 | + } |
| 82 | + ], |
| 83 | + "source": [ |
| 84 | + "def DectoBin(D,m):\n", |
| 85 | + " if D==0:\n", |
| 86 | + " breakpoint\n", |
| 87 | + " else:\n", |
| 88 | + " m.append(D%2)\n", |
| 89 | + " DectoBin(D//2,m)\n", |
| 90 | + " return m[::-1]\n", |
| 91 | + "print(DectoBin(15,[]))\n", |
| 92 | + "print(DectoBin(1,[]))\n", |
| 93 | + "print(DectoBin(6566564564,[]))" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "code", |
| 98 | + "execution_count": null, |
| 99 | + "metadata": {}, |
| 100 | + "outputs": [], |
| 101 | + "source": [] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "## 7. Design a Python script to determine the difference in date for given two dates in YYYY:MM:DD format(0 <= YYYY <= 9999, 1 <= MM <= 12, 1 <= DD <= 31) following the leap year rules. Return the total number of days existing between the two dates.\n", |
| 108 | + "\n", |
| 109 | + "### Test Cases:\n", |
| 110 | + "* dateDifference('.&checktime(2019,05,10,':')', '.&checktime(2019,05,01,':')') -> 9\n", |
| 111 | + "* dateDifference('.&checktime(0003,03,03,':')', '.&checktime(0003,06,06,':')') -> 95" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": 339, |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [ |
| 119 | + { |
| 120 | + "name": "stdout", |
| 121 | + "output_type": "stream", |
| 122 | + "text": [ |
| 123 | + "9\n", |
| 124 | + "96\n" |
| 125 | + ] |
| 126 | + } |
| 127 | + ], |
| 128 | + "source": [ |
| 129 | + "def leapYear(L):\n", |
| 130 | + " return(L% 4==0 and L%100!=0 or L%400==0)\n", |
| 131 | + "def dateDifference(p,q):\n", |
| 132 | + " y= int(p[0:4])-int(q[0:4])\n", |
| 133 | + " m= int(p[5:7])-int(q[5:7])\n", |
| 134 | + " leapYear(y)\n", |
| 135 | + " if True:\n", |
| 136 | + " yd=y*366\n", |
| 137 | + " else:\n", |
| 138 | + " yd=y*365\n", |
| 139 | + " d= int(p[8:10])-int(q[8:10])\n", |
| 140 | + " md=m*31\n", |
| 141 | + " return abs(yd+md+d)\n", |
| 142 | + "\n", |
| 143 | + "print(dateDifference('.&checktime(2019,05,10,':')', '.&checktime(2019,05,01,':')'))\n", |
| 144 | + "print(dateDifference('.&checktime(0003,03,03,':')', '.&checktime(0003,06,06,':')'))\n" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": null, |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [], |
| 152 | + "source": [] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "markdown", |
| 156 | + "metadata": {}, |
| 157 | + "source": [ |
| 158 | + "## 7. Define a function to print the sequence of spiral pattern elements for a given N x N matrix\n", |
| 159 | + "\n", |
| 160 | + "### Test Cases:\n", |
| 161 | + "spiralPattern([[1,2,3], [4,5,6], [7,8,9]]) -> 1 2 3 6 9 8 7 4 5" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "code", |
| 166 | + "execution_count": 350, |
| 167 | + "metadata": {}, |
| 168 | + "outputs": [ |
| 169 | + { |
| 170 | + "data": { |
| 171 | + "text/plain": [ |
| 172 | + "([1, 2, 3, 0], 9, 5, 8, 5, 6, 1, 7, 4)" |
| 173 | + ] |
| 174 | + }, |
| 175 | + "execution_count": 350, |
| 176 | + "metadata": {}, |
| 177 | + "output_type": "execute_result" |
| 178 | + } |
| 179 | + ], |
| 180 | + "source": [ |
| 181 | + "def spiral(m):\n", |
| 182 | + " return m[0],m[1][3],m[2][3],m[3][3],m[3][2],m[3][1],m[3][0],m[2][0],m[1][0]\n", |
| 183 | + "spiral([[1,2,3,0],[4,5,6,9],[7,8,9,5],[1,6,5,8]]) " |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "code", |
| 188 | + "execution_count": 18, |
| 189 | + "metadata": {}, |
| 190 | + "outputs": [ |
| 191 | + { |
| 192 | + "data": { |
| 193 | + "text/plain": [ |
| 194 | + "[1, 2, 3, 0, 9, 5, [1, 6, 5, 8], 7, 4]" |
| 195 | + ] |
| 196 | + }, |
| 197 | + "execution_count": 18, |
| 198 | + "metadata": {}, |
| 199 | + "output_type": "execute_result" |
| 200 | + } |
| 201 | + ], |
| 202 | + "source": [ |
| 203 | + "def Spiral(m,s):\n", |
| 204 | + " for i in range(len(m[0])):\n", |
| 205 | + " s.append(m[0][i])\n", |
| 206 | + " #for i in range(len(m[1])):\n", |
| 207 | + " s.append(m[1][3])\n", |
| 208 | + " #for i in range(len(m[1])):\n", |
| 209 | + " s.append(m[2][3])\n", |
| 210 | + " #for i in range(len(m[3]),0):\n", |
| 211 | + " s.append(*m[3:2:-1])\n", |
| 212 | + " #for i in range(len(m[2])):\n", |
| 213 | + " s.append(m[2][0])\n", |
| 214 | + " #for i in range(len(m[1])):\n", |
| 215 | + " s.append(m[1][0])\n", |
| 216 | + " return(s) \n", |
| 217 | + "Spiral([[1,2,3,0],[4,5,6,9],[7,8,9,5],[1,6,5,8]],[]) " |
| 218 | + ] |
| 219 | + }, |
| 220 | + { |
| 221 | + "cell_type": "code", |
| 222 | + "execution_count": null, |
| 223 | + "metadata": {}, |
| 224 | + "outputs": [], |
| 225 | + "source": [] |
| 226 | + }, |
| 227 | + { |
| 228 | + "cell_type": "code", |
| 229 | + "execution_count": null, |
| 230 | + "metadata": {}, |
| 231 | + "outputs": [], |
| 232 | + "source": [] |
| 233 | + }, |
| 234 | + { |
| 235 | + "cell_type": "code", |
| 236 | + "execution_count": 445, |
| 237 | + "metadata": {}, |
| 238 | + "outputs": [ |
| 239 | + { |
| 240 | + "name": "stdout", |
| 241 | + "output_type": "stream", |
| 242 | + "text": [ |
| 243 | + "2\n", |
| 244 | + "2\n" |
| 245 | + ] |
| 246 | + } |
| 247 | + ], |
| 248 | + "source": [ |
| 249 | + "def StringCount(s,r):\n", |
| 250 | + " print( len(max(s,r)))\n", |
| 251 | + " #for _ in range(len(max(s,r))):\n", |
| 252 | + " return s.count(r)\n", |
| 253 | + " \n", |
| 254 | + "print(StringCount(\"abccddccc\", \"cc\"))\n", |
| 255 | + "print(StringCount(\"1234567891122334455\", \"3\"))\n", |
| 256 | + "print(StringCount('str','substr'))\n", |
| 257 | + "print(StringCount('aaaaaaa', 'aaa'))" |
| 258 | + ] |
| 259 | + }, |
| 260 | + { |
| 261 | + "cell_type": "code", |
| 262 | + "execution_count": 459, |
| 263 | + "metadata": {}, |
| 264 | + "outputs": [ |
| 265 | + { |
| 266 | + "data": { |
| 267 | + "text/plain": [ |
| 268 | + "'[1,2,3,0] [4,5,6,9] [7,8,9,5] [1,6,5,8]'" |
| 269 | + ] |
| 270 | + }, |
| 271 | + "execution_count": 459, |
| 272 | + "metadata": {}, |
| 273 | + "output_type": "execute_result" |
| 274 | + } |
| 275 | + ], |
| 276 | + "source": [ |
| 277 | + "def spiral(m):\n", |
| 278 | + " return ' '.join(m)\n", |
| 279 | + "spiral([\"[1,2,3,0]\",\"[4,5,6,9]\",\"[7,8,9,5]\",\"[1,6,5,8]\"]) " |
| 280 | + ] |
| 281 | + }, |
| 282 | + { |
| 283 | + "cell_type": "code", |
| 284 | + "execution_count": 1, |
| 285 | + "metadata": {}, |
| 286 | + "outputs": [ |
| 287 | + { |
| 288 | + "data": { |
| 289 | + "text/plain": [ |
| 290 | + "'aabbcsdd'" |
| 291 | + ] |
| 292 | + }, |
| 293 | + "execution_count": 1, |
| 294 | + "metadata": {}, |
| 295 | + "output_type": "execute_result" |
| 296 | + } |
| 297 | + ], |
| 298 | + "source": [ |
| 299 | + "def merge(m,n,o):\n", |
| 300 | + " for i in range (len(max(m,n))):\n", |
| 301 | + " o.append(m[i]+n[i])\n", |
| 302 | + " \n", |
| 303 | + " return \"\".join(o)\n", |
| 304 | + "merge('abcd', 'absd',[])" |
| 305 | + ] |
| 306 | + }, |
| 307 | + { |
| 308 | + "cell_type": "code", |
| 309 | + "execution_count": null, |
| 310 | + "metadata": {}, |
| 311 | + "outputs": [], |
| 312 | + "source": [] |
| 313 | + } |
| 314 | + ], |
| 315 | + "metadata": { |
| 316 | + "kernelspec": { |
| 317 | + "display_name": "Python 3", |
| 318 | + "language": "python", |
| 319 | + "name": "python3" |
| 320 | + }, |
| 321 | + "language_info": { |
| 322 | + "codemirror_mode": { |
| 323 | + "name": "ipython", |
| 324 | + "version": 3 |
| 325 | + }, |
| 326 | + "file_extension": ".py", |
| 327 | + "mimetype": "text/x-python", |
| 328 | + "name": "python", |
| 329 | + "nbconvert_exporter": "python", |
| 330 | + "pygments_lexer": "ipython3", |
| 331 | + "version": "3.7.3" |
| 332 | + } |
| 333 | + }, |
| 334 | + "nbformat": 4, |
| 335 | + "nbformat_minor": 2 |
| 336 | +} |
0 commit comments