|
9 | 9 | },
|
10 | 10 | {
|
11 | 11 | "cell_type": "code",
|
12 |
| - "execution_count": 31, |
| 12 | + "execution_count": 1, |
13 | 13 | "metadata": {
|
14 | 14 | "collapsed": true
|
15 | 15 | },
|
|
305 | 305 | "collapsed": true
|
306 | 306 | },
|
307 | 307 | "source": [
|
308 |
| - "#### 2. Check Balanced parentheses of a mathematical expression (checking only for parentheses)" |
| 308 | + "#### 2. Check Balanced parentheses of a mathematical expression (checking only for small parentheses)" |
309 | 309 | ]
|
310 | 310 | },
|
311 | 311 | {
|
312 | 312 | "cell_type": "code",
|
313 |
| - "execution_count": 34, |
314 |
| - "metadata": {}, |
| 313 | + "execution_count": 37, |
| 314 | + "metadata": { |
| 315 | + "collapsed": true |
| 316 | + }, |
315 | 317 | "outputs": [],
|
316 | 318 | "source": [
|
317 | 319 | "def checkBalancedSmallBraces(expression):\n",
|
|
345 | 347 | },
|
346 | 348 | {
|
347 | 349 | "cell_type": "code",
|
348 |
| - "execution_count": 35, |
| 350 | + "execution_count": 38, |
349 | 351 | "metadata": {},
|
350 | 352 | "outputs": [
|
351 | 353 | {
|
|
354 | 356 | "True"
|
355 | 357 | ]
|
356 | 358 | },
|
357 |
| - "execution_count": 35, |
| 359 | + "execution_count": 38, |
358 | 360 | "metadata": {},
|
359 | 361 | "output_type": "execute_result"
|
360 | 362 | }
|
361 | 363 | ],
|
362 | 364 | "source": [
|
363 |
| - "checkBalancedParentheses('()')" |
| 365 | + "checkBalancedSmallBraces('()')" |
364 | 366 | ]
|
365 | 367 | },
|
366 | 368 | {
|
367 | 369 | "cell_type": "code",
|
368 |
| - "execution_count": 36, |
| 370 | + "execution_count": 39, |
369 | 371 | "metadata": {},
|
370 | 372 | "outputs": [
|
371 | 373 | {
|
|
374 | 376 | "False"
|
375 | 377 | ]
|
376 | 378 | },
|
377 |
| - "execution_count": 36, |
| 379 | + "execution_count": 39, |
378 | 380 | "metadata": {},
|
379 | 381 | "output_type": "execute_result"
|
380 | 382 | }
|
381 | 383 | ],
|
382 | 384 | "source": [
|
383 |
| - "checkBalancedParentheses('(((())))))')" |
| 385 | + "checkBalancedSmallBraces('(((())))))')" |
384 | 386 | ]
|
385 | 387 | },
|
386 | 388 | {
|
|
390 | 392 | "#### 3. Check Balanced parentheses of a mathematical expression (checking for all symbols '({[' "
|
391 | 393 | ]
|
392 | 394 | },
|
| 395 | + { |
| 396 | + "cell_type": "code", |
| 397 | + "execution_count": 47, |
| 398 | + "metadata": { |
| 399 | + "collapsed": true |
| 400 | + }, |
| 401 | + "outputs": [], |
| 402 | + "source": [ |
| 403 | + "\n", |
| 404 | + "\n", |
| 405 | + "def matchSymbol(open_sym, close_sym):\n", |
| 406 | + " \"\"\"Check if the open symbol and close symbol are of same kind.\n", |
| 407 | + "\n", |
| 408 | + " Args:\n", |
| 409 | + " open_sym (string): Opening mathematical expression\n", |
| 410 | + " close_sym (string): Closing mathematical expression\n", |
| 411 | + "\n", |
| 412 | + " Returns:\n", |
| 413 | + " bool: If the open_sym and close_sym are similar kind,\n", |
| 414 | + " it will return True otherwise False.\n", |
| 415 | + " \"\"\"\n", |
| 416 | + " open_expression = '({['\n", |
| 417 | + " close_expression = ']})'\n", |
| 418 | + " return open_expression.index(open_sym) == close_expression.index(close_sym)\n", |
| 419 | + "\n", |
| 420 | + "\n", |
| 421 | + "def checkBalancedSymbols(expression):\n", |
| 422 | + " \"\"\"Check if the given expression is balanced mathematical expression or not.\n", |
| 423 | + "\n", |
| 424 | + " Args:\n", |
| 425 | + " expression (string): Expression to be checked.\n", |
| 426 | + "\n", |
| 427 | + " Returns:\n", |
| 428 | + " bool: If the given mathematical expression is balanced,\n", |
| 429 | + " it will return True otherwise False.\n", |
| 430 | + " \"\"\"\n", |
| 431 | + " i = 0\n", |
| 432 | + " balanced = True\n", |
| 433 | + " stack_symbol = Stack()\n", |
| 434 | + " for elm in expression:\n", |
| 435 | + " if elm in '({[':\n", |
| 436 | + " stack_symbol.push(elm)\n", |
| 437 | + " else:\n", |
| 438 | + " if stack_symbol.isEmpty():\n", |
| 439 | + " balanced = False\n", |
| 440 | + " else:\n", |
| 441 | + " open_symbol = stack_symbol.pop()\n", |
| 442 | + " close_symbol = elm\n", |
| 443 | + " if matchSymbol(open_symbol, close_symbol):\n", |
| 444 | + " balanced = True\n", |
| 445 | + " if stack_symbol.isEmpty() and balanced:\n", |
| 446 | + " return True\n", |
| 447 | + " else:\n", |
| 448 | + " return False\n" |
| 449 | + ] |
| 450 | + }, |
| 451 | + { |
| 452 | + "cell_type": "code", |
| 453 | + "execution_count": 48, |
| 454 | + "metadata": {}, |
| 455 | + "outputs": [ |
| 456 | + { |
| 457 | + "data": { |
| 458 | + "text/plain": [ |
| 459 | + "True" |
| 460 | + ] |
| 461 | + }, |
| 462 | + "execution_count": 48, |
| 463 | + "metadata": {}, |
| 464 | + "output_type": "execute_result" |
| 465 | + } |
| 466 | + ], |
| 467 | + "source": [ |
| 468 | + "checkBalancedSymbols('({{[]}})')" |
| 469 | + ] |
| 470 | + }, |
| 471 | + { |
| 472 | + "cell_type": "code", |
| 473 | + "execution_count": 49, |
| 474 | + "metadata": {}, |
| 475 | + "outputs": [ |
| 476 | + { |
| 477 | + "data": { |
| 478 | + "text/plain": [ |
| 479 | + "False" |
| 480 | + ] |
| 481 | + }, |
| 482 | + "execution_count": 49, |
| 483 | + "metadata": {}, |
| 484 | + "output_type": "execute_result" |
| 485 | + } |
| 486 | + ], |
| 487 | + "source": [ |
| 488 | + "checkBalancedSymbols('[{]')" |
| 489 | + ] |
| 490 | + }, |
| 491 | + { |
| 492 | + "cell_type": "markdown", |
| 493 | + "metadata": { |
| 494 | + "collapsed": true |
| 495 | + }, |
| 496 | + "source": [ |
| 497 | + "#### 4. Convert Decimal to binary numbers" |
| 498 | + ] |
| 499 | + }, |
| 500 | + { |
| 501 | + "cell_type": "code", |
| 502 | + "execution_count": 4, |
| 503 | + "metadata": { |
| 504 | + "collapsed": true |
| 505 | + }, |
| 506 | + "outputs": [], |
| 507 | + "source": [ |
| 508 | + "def decimalToBinary(decimal_number):\n", |
| 509 | + " \"\"\"Convert given decimal number to binary number.\n", |
| 510 | + "\n", |
| 511 | + " Here, we use remainder operator to store the rem. to our stack.\n", |
| 512 | + " And, at the same time the given decimal number is being\n", |
| 513 | + " reduced to half since '2' act as a base value for\n", |
| 514 | + " binary conversion.\n", |
| 515 | + " '//' is being used to performs integer division.\n", |
| 516 | + "\n", |
| 517 | + " Args:\n", |
| 518 | + " decimal_number (int): Decimal number to be converted to binary\n", |
| 519 | + "\n", |
| 520 | + " Returns:\n", |
| 521 | + " str: Binary representation of given decimal number.\n", |
| 522 | + " \"\"\"\n", |
| 523 | + " binary_string = \"\"\n", |
| 524 | + " stack_bin = Stack()\n", |
| 525 | + " while decimal_number:\n", |
| 526 | + " remainder = decimal_number % 2\n", |
| 527 | + " stack_bin.push(remainder)\n", |
| 528 | + " decimal_number = decimal_number // 2\n", |
| 529 | + " while not stack_bin.isEmpty():\n", |
| 530 | + " binary_string += str(stack_bin.pop())\n", |
| 531 | + " return binary_string\n" |
| 532 | + ] |
| 533 | + }, |
| 534 | + { |
| 535 | + "cell_type": "code", |
| 536 | + "execution_count": 5, |
| 537 | + "metadata": {}, |
| 538 | + "outputs": [ |
| 539 | + { |
| 540 | + "data": { |
| 541 | + "text/plain": [ |
| 542 | + "'100'" |
| 543 | + ] |
| 544 | + }, |
| 545 | + "execution_count": 5, |
| 546 | + "metadata": {}, |
| 547 | + "output_type": "execute_result" |
| 548 | + } |
| 549 | + ], |
| 550 | + "source": [ |
| 551 | + "decimalToBinary(4)" |
| 552 | + ] |
| 553 | + }, |
| 554 | + { |
| 555 | + "cell_type": "code", |
| 556 | + "execution_count": 6, |
| 557 | + "metadata": {}, |
| 558 | + "outputs": [ |
| 559 | + { |
| 560 | + "data": { |
| 561 | + "text/plain": [ |
| 562 | + "'1101111'" |
| 563 | + ] |
| 564 | + }, |
| 565 | + "execution_count": 6, |
| 566 | + "metadata": {}, |
| 567 | + "output_type": "execute_result" |
| 568 | + } |
| 569 | + ], |
| 570 | + "source": [ |
| 571 | + "decimalToBinary(111)" |
| 572 | + ] |
| 573 | + }, |
| 574 | + { |
| 575 | + "cell_type": "code", |
| 576 | + "execution_count": 12, |
| 577 | + "metadata": {}, |
| 578 | + "outputs": [ |
| 579 | + { |
| 580 | + "ename": "SyntaxError", |
| 581 | + "evalue": "invalid syntax (<ipython-input-12-d89f2bb3b4bb>, line 14)", |
| 582 | + "output_type": "error", |
| 583 | + "traceback": [ |
| 584 | + "\u001b[0;36m File \u001b[0;32m\"<ipython-input-12-d89f2bb3b4bb>\"\u001b[0;36m, line \u001b[0;32m14\u001b[0m\n\u001b[0;31m for i random.sample(range(min_value, max_value),k=incr):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" |
| 585 | + ] |
| 586 | + } |
| 587 | + ], |
| 588 | + "source": [ |
| 589 | + "%matplotlib inline\n", |
| 590 | + "from matplotlib import pyplot\n", |
| 591 | + "\n", |
| 592 | + "import random\n", |
| 593 | + "import timeit\n", |
| 594 | + "from functools import partial\n", |
| 595 | + "def plot_time_complexity(fn, min_value, max_value, incr, no_of_tests):\n", |
| 596 | + " \"\"\"\n", |
| 597 | + " Plotting time complexity based on given function.\n", |
| 598 | + " \"\"\"\n", |
| 599 | + " x = []\n", |
| 600 | + " y = []\n", |
| 601 | + " my_randoms = random.sample(range(min_value, max_value),k=incr)\n", |
| 602 | + "# for i random.sample(range(min_value, max_value),k=incr):\n", |
| 603 | + "# N = i\n", |
| 604 | + "# timer = timeit.Timer(partial(fn, N))\n", |
| 605 | + "# t = timer.timeit(number=no_of_tests)\n", |
| 606 | + "# x.append(i)\n", |
| 607 | + "# y.append(t)\n", |
| 608 | + "# p1 = pyplot.plot(x, y, 'o', label = fn.__name__)\n", |
| 609 | + "# pyplot.legend(loc='upper left')\n", |
| 610 | + "# pyplot.xlabel('data size', fontsize=18)\n", |
| 611 | + "# pyplot.ylabel('execution time', fontsize=16)" |
| 612 | + ] |
| 613 | + }, |
393 | 614 | {
|
394 | 615 | "cell_type": "code",
|
395 | 616 | "execution_count": null,
|
|
398 | 619 | },
|
399 | 620 | "outputs": [],
|
400 | 621 | "source": [
|
401 |
| - "def CheckBalanced" |
| 622 | + "decimalToBinary(concatenation, 1, 10, 100000, 1000)" |
402 | 623 | ]
|
403 | 624 | }
|
404 | 625 | ],
|
|
0 commit comments