{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [], "authorship_tag": "ABX9TyNGCO6vWPDYSizo5JW4ISI9", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "# Ordenar pilas: procedimiento lento" ], "metadata": { "id": "ONvCQucU7FMg" } }, { "cell_type": "code", "execution_count": 31, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "aL8IU9Am7CM6", "outputId": "15b796d2-869b-4d8c-c7f0-1be13d7732de" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "a: [110, 708, 986, 663, 318, 263, 506, 462, 896, 643, 746, 531, 857, 588, 496, 47, 365, 151, 521, 360, 756, 678, 50, 828, 484, 661, 988, 963, 405, 660, 127, 822, 473, 787, 595, 899, 565, 341, 112, 539, 257, 189, 981, 605, 868, 313, 310, 908, 197, 312, 796, 476, 575, 34, 612, 562, 675, 619, 996, 798, 956, 934, 149, 634, 633, 845, 469, 122, 719, 129, 831, 514, 169, 202, 11, 105, 97, 222, 1, 192, 206, 693, 574, 628, 133, 270, 279, 952, 930, 236, 450, 997, 446, 39, 294, 749, 354, 373, 335, 419, 754, 594, 646, 439, 109, 775, 918, 75, 812, 402, 41, 846, 482, 790, 620, 983, 960, 49, 290, 951, 973, 791, 844, 502, 688, 164, 914, 115, 68, 307, 610, 387, 939, 274, 356, 840, 9, 183, 483, 194, 803, 823, 131, 466, 94, 625, 98, 913, 291, 653, 727, 478, 668, 126, 437, 120, 885, 971, 5, 566, 199, 277, 784, 461, 427, 330, 296, 143, 992, 244, 208, 201, 485, 500, 299, 172, 639, 736, 347, 393, 276, 917, 91, 210, 2, 884, 613, 195, 512, 679, 316, 714, 720, 886, 954, 329, 765, 336, 378, 333, 946, 632, 140, 234, 301, 640, 929, 617, 766, 495, 33, 669, 938, 411, 25, 156, 898, 220, 170, 464, 744, 789, 287, 273, 306, 207, 998, 470, 961, 353, 801, 667, 343, 547, 856, 36, 924, 89, 241, 416, 305, 863, 292, 191, 728, 224, 615, 337, 723, 300, 78, 145, 894, 315, 748, 404, 398, 303, 738, 368, 522, 852, 504, 587, 339, 22, 942, 702, 753, 881, 283, 326, 609, 242, 21, 838, 980, 591, 859, 869, 394, 176, 456, 874, 119, 825, 297, 526, 240, 860, 431, 214, 487, 372, 813, 731, 407, 146, 181, 59, 573, 225, 627, 941, 701, 82, 92, 962, 541, 872, 492, 64, 554, 695, 932, 742, 658, 747, 781, 379, 901, 23, 458, 132, 947, 258, 399, 518, 27, 237, 739, 862, 665, 265, 401, 949, 38, 182, 928, 188, 644, 357, 124, 51, 288, 77, 957, 320, 538, 261, 252, 361, 786, 800, 58, 792, 304, 804, 516, 549, 926, 647, 690, 782, 763, 687, 79, 60, 807, 905, 760, 14, 163, 96, 384, 861, 73, 887, 53, 286, 19, 144, 367, 732, 686, 648, 107, 797, 698, 525, 179, 650, 418, 683, 593, 757, 8, 529, 793, 891, 821, 767, 830, 570, 561, 212, 604, 548, 568, 652, 815, 135, 910, 585, 388, 751, 912, 4, 441, 61, 733, 649, 713, 465, 834, 584, 542, 415, 157, 351, 890, 386, 30, 515, 490, 718, 12, 836, 572, 709, 601, 534, 184, 42, 999, 426, 43, 607, 626, 380, 374, 855, 933, 281, 26, 851, 346, 673, 219, 321, 377, 298, 850, 414, 553, 250, 445, 366, 491, 785, 226, 395, 116, 168, 355, 218, 769, 530, 175, 618, 948, 314, 278, 114, 795, 243, 85, 722, 422, 745, 475, 223, 656, 509, 180, 443, 424, 3, 425, 953, 847, 532, 806, 689, 878, 32, 606, 332, 975, 328, 440, 480, 839, 216, 581, 597, 350, 670, 832, 833, 780, 824, 65, 282, 57, 376, 768, 293, 493, 528, 940, 944, 557, 755, 166, 245, 589, 118, 302, 586, 28, 494, 598, 799, 165, 498, 603, 517, 843, 771, 674, 249, 558, 123, 969, 922, 84, 931, 55, 864, 880, 103, 889, 134, 657, 721, 876, 696, 967, 684, 842, 486, 571, 88, 616, 471, 248, 95, 849, 488, 83, 903, 579, 423, 48, 829, 919, 430, 916, 895, 37, 582, 567, 935, 599, 697, 155, 556, 511, 773, 44, 232, 87, 637, 271, 344, 385, 102, 841, 322, 655, 231, 752, 704, 152, 867, 229, 76, 413, 391, 537, 569, 681, 764, 358, 915, 987, 550, 455, 338, 774, 900, 897, 479, 454, 826, 15, 950, 289, 308, 540, 247, 20, 499, 642, 348, 311, 972, 672, 477, 712, 230, 635, 359, 81, 137, 63, 664, 805, 363, 544, 546, 489, 161, 397, 111, 927, 438, 545, 342, 190, 420, 762, 716, 725, 29, 52, 452, 148, 873, 62, 70, 463, 726, 820, 436, 69, 334, 680, 835, 965, 776, 848, 18, 968, 100, 715, 268, 882, 324, 285, 580, 662, 147, 624, 246, 267, 602, 507, 266, 128, 520, 345, 527, 136, 408, 638, 984, 810, 153, 645, 173, 459, 174, 995, 177, 902, 551, 17, 193, 130, 694, 187, 142, 707, 703, 976, 389, 808, 761, 958, 264, 159, 888, 381, 167, 596, 80, 451, 993, 865, 802, 985, 457, 883, 959, 877, 262, 510, 907, 396, 543, 272, 46, 592, 819, 978, 982, 955, 319, 750, 989, 409, 447, 467, 67, 641, 629, 827, 945, 654, 811, 564, 56, 211, 734, 523, 93, 691, 203, 370, 794, 198, 433, 729, 552, 185, 711, 327, 740, 621, 178, 253, 141, 818, 101, 383, 113, 186, 86, 682, 54, 121, 444, 630, 503, 608, 6, 215, 256, 284, 614, 559, 921, 331, 403, 893, 631, 700, 920, 685, 943, 936, 221, 371, 117, 536, 392, 227, 217, 814, 453, 600, 460, 448, 442, 622, 251, 555, 779, 325, 970, 758, 204, 362, 400, 737, 239, 295, 966, 238, 501, 369, 162, 519, 772, 160, 879, 699, 854, 24, 417, 260, 809, 706, 590, 106, 659, 474, 472, 671, 508, 911, 280, 40, 66, 892, 158, 90, 979, 743, 125, 513, 705, 677, 906, 205, 777, 432, 925, 576, 994, 563, 505, 964, 717, 35, 235, 71, 535, 724, 611, 410, 213, 309, 990, 778, 72, 382, 651, 364, 923, 412, 866, 428, 937, 816, 741, 228, 837, 524, 692, 974, 275, 481, 7, 104, 730, 578, 533, 759, 583, 870, 623, 406, 636, 434, 150, 853, 390, 788, 977, 349, 468, 577, 1000, 171, 233, 497, 269, 666, 560, 209, 16, 10, 138, 875, 770, 45, 99, 259, 254, 317, 421, 74, 352, 991, 909, 871, 904, 323, 783, 735, 154, 340, 13, 108, 817, 429, 435, 375, 255, 31, 196, 676, 200, 139, 858, 449, 710]\n", "b: []\n", "a: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559, 560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623, 624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639, 640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655, 656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687, 688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703, 704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735, 736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751, 752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767, 768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799, 800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831, 832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847, 848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911, 912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991, 992, 993, 994, 995, 996, 997, 998, 999, 1000]\n", "b: []\n", "contador: 255646\n" ] } ], "source": [ "from random import sample, seed\n", "seed()\n", "\n", "def sa(a,b):\n", " if len(a) > 1: a[0],a[1] = a[1],a[0]\n", " return a,b\n", "def sb(a,b):\n", " if len(b) > 1: b[0],b[1] = b[1],b[0]\n", " return a,b\n", "def ss(a,b):\n", " sa(a,b)\n", " sb(a,b)\n", " return a,b\n", "def pa(a,b):\n", " if len(b) > 0:\n", " a.insert(0, b[0])\n", " b.pop(0)\n", " return a,b\n", "def pb(a,b):\n", " if len(a) > 0:\n", " b.insert(0, a[0])\n", " a.pop(0)\n", " return a,b\n", "def ra(a,b):\n", " if len(a) > 1: a.append(a.pop(0))\n", " return a,b\n", "def rb(a,b):\n", " if len(b) > 1: b.append(b.pop(0))\n", " return a,b\n", "def rr(a,b):\n", " ra(a,b)\n", " rb(a,b)\n", " return a,b\n", "def rra(a,b):\n", " if len(a) > 1: a.insert(0, a.pop())\n", " return a,b\n", "def rrb(a,b):\n", " if len(b) > 1: b.insert(0, b.pop())\n", " return a,b\n", "def rrr(a,b):\n", " rra(a,b)\n", " rrb(a,b)\n", " return a,b\n", "\n", "if __name__ == \"__main__\":\n", " # Generación de la pila a\n", " n = 1000 # número de elementos de la pila\n", " a = sample(range(1, n+1), n)\n", " #a = [2,1,3,6,5,8] # valores de ejemplo\n", " b = []\n", " print('a:', a)\n", " print('b:', b)\n", " contador = 0\n", " while len(a)>1: # se repite hasta que solo quede un elemento en la pila a\n", " # buscamos el mínimo y vamos haciendo ra hasta que el mínimo esté el primero\n", " while min(a) != a[0]:\n", " ra(a,b); contador += 1\n", " pb(a,b); contador += 1 # luego hacemos pb\n", " # luego se hace pa pa pa pa hasta que no quede nadie en la pila b\n", " while len(b)>0:\n", " pa(a,b); contador += 1\n", " \n", " print('a:', a)\n", " print('b:', b)\n", " print(\"contador:\", contador)" ] }, { "cell_type": "code", "source": [ "#!curl parrot.live" ], "metadata": { "id": "3rWWqYAafkBj" }, "execution_count": null, "outputs": [] } ] }