├── .gitignore ├── LICENSE ├── README.md └── python-for-competitive-programming.ipynb /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | 55 | # Sphinx documentation 56 | docs/_build/ 57 | 58 | # PyBuilder 59 | target/ 60 | 61 | #Ipython Notebook 62 | .ipynb_checkpoints 63 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Alex Palcuie 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-for-competitive-programming 2 | 3 | I'm holding a class at the Advanced Programming course, where I will introduce the students in a little Python. This repo has a nice IPython Notebook that serves as reference. 4 | 5 | ## Problems to solve 6 | 7 | * [Link/Cut Tree](http://codeforces.com/contest/614/problem/A) 8 | * [Bulbs](http://codeforces.com/contest/615/problem/A) 9 | * [Pasha and Stick](http://codeforces.com/contest/610/problem/A) 10 | * [Vika and Squares](http://codeforces.com/contest/610/problem/B) 11 | -------------------------------------------------------------------------------- /python-for-competitive-programming.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# Python for competitive programming" 8 | ] 9 | }, 10 | { 11 | "cell_type": "code", 12 | "execution_count": 1, 13 | "metadata": { 14 | "collapsed": false 15 | }, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "Hello World!\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "# This is a comment in Python\n", 27 | "\n", 28 | "print(\"Hello World!\")" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "## Numbers" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 2, 41 | "metadata": { 42 | "collapsed": false 43 | }, 44 | "outputs": [ 45 | { 46 | "data": { 47 | "text/plain": [ 48 | "4" 49 | ] 50 | }, 51 | "execution_count": 2, 52 | "metadata": {}, 53 | "output_type": "execute_result" 54 | } 55 | ], 56 | "source": [ 57 | "2 + 2" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "metadata": { 64 | "collapsed": true 65 | }, 66 | "outputs": [], 67 | "source": [ 68 | "x = 5" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 4, 74 | "metadata": { 75 | "collapsed": false 76 | }, 77 | "outputs": [ 78 | { 79 | "data": { 80 | "text/plain": [ 81 | "2.5" 82 | ] 83 | }, 84 | "execution_count": 4, 85 | "metadata": {}, 86 | "output_type": "execute_result" 87 | } 88 | ], 89 | "source": [ 90 | "x / 2" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 5, 96 | "metadata": { 97 | "collapsed": false 98 | }, 99 | "outputs": [ 100 | { 101 | "data": { 102 | "text/plain": [ 103 | "2" 104 | ] 105 | }, 106 | "execution_count": 5, 107 | "metadata": {}, 108 | "output_type": "execute_result" 109 | } 110 | ], 111 | "source": [ 112 | "x // 2" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 6, 118 | "metadata": { 119 | "collapsed": false 120 | }, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | "1024" 126 | ] 127 | }, 128 | "execution_count": 6, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "2 ** 10" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 7, 140 | "metadata": { 141 | "collapsed": false 142 | }, 143 | "outputs": [ 144 | { 145 | "data": { 146 | "text/plain": [ 147 | "19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376" 148 | ] 149 | }, 150 | "execution_count": 7, 151 | "metadata": {}, 152 | "output_type": "execute_result" 153 | } 154 | ], 155 | "source": [ 156 | "2 ** 10000" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 8, 162 | "metadata": { 163 | "collapsed": false 164 | }, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "text/plain": [ 169 | "30103" 170 | ] 171 | }, 172 | "execution_count": 8, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "len(str(2 ** 100000))" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": {}, 184 | "source": [ 185 | "## Strings" 186 | ] 187 | }, 188 | { 189 | "cell_type": "code", 190 | "execution_count": 9, 191 | "metadata": { 192 | "collapsed": true 193 | }, 194 | "outputs": [], 195 | "source": [ 196 | "s = \"python\"" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": 10, 202 | "metadata": { 203 | "collapsed": false 204 | }, 205 | "outputs": [ 206 | { 207 | "data": { 208 | "text/plain": [ 209 | "'pythonpython'" 210 | ] 211 | }, 212 | "execution_count": 10, 213 | "metadata": {}, 214 | "output_type": "execute_result" 215 | } 216 | ], 217 | "source": [ 218 | "s + s" 219 | ] 220 | }, 221 | { 222 | "cell_type": "code", 223 | "execution_count": 11, 224 | "metadata": { 225 | "collapsed": false 226 | }, 227 | "outputs": [ 228 | { 229 | "data": { 230 | "text/plain": [ 231 | "'pythonpythonpython'" 232 | ] 233 | }, 234 | "execution_count": 11, 235 | "metadata": {}, 236 | "output_type": "execute_result" 237 | } 238 | ], 239 | "source": [ 240 | "s * 3" 241 | ] 242 | }, 243 | { 244 | "cell_type": "code", 245 | "execution_count": 12, 246 | "metadata": { 247 | "collapsed": false 248 | }, 249 | "outputs": [ 250 | { 251 | "name": "stdout", 252 | "output_type": "stream", 253 | "text": [ 254 | "infoclock\n" 255 | ] 256 | } 257 | ], 258 | "source": [ 259 | "s = input() # type infoclock" 260 | ] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": 13, 265 | "metadata": { 266 | "collapsed": false 267 | }, 268 | "outputs": [ 269 | { 270 | "data": { 271 | "text/plain": [ 272 | "'i'" 273 | ] 274 | }, 275 | "execution_count": 13, 276 | "metadata": {}, 277 | "output_type": "execute_result" 278 | } 279 | ], 280 | "source": [ 281 | "s[0]" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 14, 287 | "metadata": { 288 | "collapsed": false 289 | }, 290 | "outputs": [ 291 | { 292 | "data": { 293 | "text/plain": [ 294 | "'n'" 295 | ] 296 | }, 297 | "execution_count": 14, 298 | "metadata": {}, 299 | "output_type": "execute_result" 300 | } 301 | ], 302 | "source": [ 303 | "s[1]" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": 15, 309 | "metadata": { 310 | "collapsed": false 311 | }, 312 | "outputs": [ 313 | { 314 | "data": { 315 | "text/plain": [ 316 | "'k'" 317 | ] 318 | }, 319 | "execution_count": 15, 320 | "metadata": {}, 321 | "output_type": "execute_result" 322 | } 323 | ], 324 | "source": [ 325 | "s[-1]" 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 16, 331 | "metadata": { 332 | "collapsed": false 333 | }, 334 | "outputs": [ 335 | { 336 | "data": { 337 | "text/plain": [ 338 | "'c'" 339 | ] 340 | }, 341 | "execution_count": 16, 342 | "metadata": {}, 343 | "output_type": "execute_result" 344 | } 345 | ], 346 | "source": [ 347 | "s[-2]" 348 | ] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "execution_count": 17, 353 | "metadata": { 354 | "collapsed": false 355 | }, 356 | "outputs": [ 357 | { 358 | "data": { 359 | "text/plain": [ 360 | "'nfoclock'" 361 | ] 362 | }, 363 | "execution_count": 17, 364 | "metadata": {}, 365 | "output_type": "execute_result" 366 | } 367 | ], 368 | "source": [ 369 | "s[1:]" 370 | ] 371 | }, 372 | { 373 | "cell_type": "code", 374 | "execution_count": 18, 375 | "metadata": { 376 | "collapsed": false 377 | }, 378 | "outputs": [ 379 | { 380 | "data": { 381 | "text/plain": [ 382 | "'i'" 383 | ] 384 | }, 385 | "execution_count": 18, 386 | "metadata": {}, 387 | "output_type": "execute_result" 388 | } 389 | ], 390 | "source": [ 391 | "s[:1]" 392 | ] 393 | }, 394 | { 395 | "cell_type": "code", 396 | "execution_count": 19, 397 | "metadata": { 398 | "collapsed": false 399 | }, 400 | "outputs": [ 401 | { 402 | "data": { 403 | "text/plain": [ 404 | "'foclock'" 405 | ] 406 | }, 407 | "execution_count": 19, 408 | "metadata": {}, 409 | "output_type": "execute_result" 410 | } 411 | ], 412 | "source": [ 413 | "s[2:]" 414 | ] 415 | }, 416 | { 417 | "cell_type": "code", 418 | "execution_count": 20, 419 | "metadata": { 420 | "collapsed": false 421 | }, 422 | "outputs": [ 423 | { 424 | "data": { 425 | "text/plain": [ 426 | "'in'" 427 | ] 428 | }, 429 | "execution_count": 20, 430 | "metadata": {}, 431 | "output_type": "execute_result" 432 | } 433 | ], 434 | "source": [ 435 | "s[:2]" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 21, 441 | "metadata": { 442 | "collapsed": false 443 | }, 444 | "outputs": [ 445 | { 446 | "data": { 447 | "text/plain": [ 448 | "'infocloc'" 449 | ] 450 | }, 451 | "execution_count": 21, 452 | "metadata": {}, 453 | "output_type": "execute_result" 454 | } 455 | ], 456 | "source": [ 457 | "s[:-1]" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": 22, 463 | "metadata": { 464 | "collapsed": false 465 | }, 466 | "outputs": [ 467 | { 468 | "data": { 469 | "text/plain": [ 470 | "'infoclo'" 471 | ] 472 | }, 473 | "execution_count": 22, 474 | "metadata": {}, 475 | "output_type": "execute_result" 476 | } 477 | ], 478 | "source": [ 479 | "s[:-2]" 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": 23, 485 | "metadata": { 486 | "collapsed": false 487 | }, 488 | "outputs": [ 489 | { 490 | "data": { 491 | "text/plain": [ 492 | "'foclo'" 493 | ] 494 | }, 495 | "execution_count": 23, 496 | "metadata": {}, 497 | "output_type": "execute_result" 498 | } 499 | ], 500 | "source": [ 501 | "s[2 : -2]" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": 24, 507 | "metadata": { 508 | "collapsed": false 509 | }, 510 | "outputs": [ 511 | { 512 | "data": { 513 | "text/plain": [ 514 | "'fco'" 515 | ] 516 | }, 517 | "execution_count": 24, 518 | "metadata": {}, 519 | "output_type": "execute_result" 520 | } 521 | ], 522 | "source": [ 523 | "s[2 : -2 : 2]" 524 | ] 525 | }, 526 | { 527 | "cell_type": "markdown", 528 | "metadata": {}, 529 | "source": [ 530 | "## Lists" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": 25, 536 | "metadata": { 537 | "collapsed": false 538 | }, 539 | "outputs": [], 540 | "source": [ 541 | "l = [2, 3, 5, 7, 11]" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": 26, 547 | "metadata": { 548 | "collapsed": false 549 | }, 550 | "outputs": [ 551 | { 552 | "data": { 553 | "text/plain": [ 554 | "22" 555 | ] 556 | }, 557 | "execution_count": 26, 558 | "metadata": {}, 559 | "output_type": "execute_result" 560 | } 561 | ], 562 | "source": [ 563 | "l[0] * l[-1]" 564 | ] 565 | }, 566 | { 567 | "cell_type": "code", 568 | "execution_count": 27, 569 | "metadata": { 570 | "collapsed": false 571 | }, 572 | "outputs": [ 573 | { 574 | "name": "stdout", 575 | "output_type": "stream", 576 | "text": [ 577 | "[2, 3, 5, 7, 11, 13]\n" 578 | ] 579 | } 580 | ], 581 | "source": [ 582 | "l.append(13)\n", 583 | "print(l)" 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": 28, 589 | "metadata": { 590 | "collapsed": false 591 | }, 592 | "outputs": [ 593 | { 594 | "data": { 595 | "text/plain": [ 596 | "[2, 3, 5, 7, 11, 13, 2, 4, 6, 8]" 597 | ] 598 | }, 599 | "execution_count": 28, 600 | "metadata": {}, 601 | "output_type": "execute_result" 602 | } 603 | ], 604 | "source": [ 605 | "l2 = [2, 4, 6, 8]\n", 606 | "l + l2" 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "execution_count": 29, 612 | "metadata": { 613 | "collapsed": false 614 | }, 615 | "outputs": [ 616 | { 617 | "name": "stdout", 618 | "output_type": "stream", 619 | "text": [ 620 | "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n" 621 | ] 622 | } 623 | ], 624 | "source": [ 625 | "v = [0] * 10\n", 626 | "print(v)" 627 | ] 628 | }, 629 | { 630 | "cell_type": "code", 631 | "execution_count": 30, 632 | "metadata": { 633 | "collapsed": false 634 | }, 635 | "outputs": [ 636 | { 637 | "name": "stdout", 638 | "output_type": "stream", 639 | "text": [ 640 | "[33, 1, 1, 1, 1, 1, 1, 1, 1, 1]\n" 641 | ] 642 | } 643 | ], 644 | "source": [ 645 | "v = [1] * 10\n", 646 | "v[0] = 33\n", 647 | "print(v)" 648 | ] 649 | }, 650 | { 651 | "cell_type": "code", 652 | "execution_count": 31, 653 | "metadata": { 654 | "collapsed": false 655 | }, 656 | "outputs": [], 657 | "source": [ 658 | "# s[0] = 'x' # this will fail" 659 | ] 660 | }, 661 | { 662 | "cell_type": "code", 663 | "execution_count": 32, 664 | "metadata": { 665 | "collapsed": false 666 | }, 667 | "outputs": [ 668 | { 669 | "name": "stdout", 670 | "output_type": "stream", 671 | "text": [ 672 | "['python', 'is', 'awesome']\n" 673 | ] 674 | } 675 | ], 676 | "source": [ 677 | "s = \"python is awesome\"\n", 678 | "v = s.split()\n", 679 | "print(v)" 680 | ] 681 | }, 682 | { 683 | "cell_type": "code", 684 | "execution_count": 33, 685 | "metadata": { 686 | "collapsed": false 687 | }, 688 | "outputs": [ 689 | { 690 | "name": "stdout", 691 | "output_type": "stream", 692 | "text": [ 693 | "['python', 'is', 'awesome', 100]\n" 694 | ] 695 | } 696 | ], 697 | "source": [ 698 | "v.append(100)\n", 699 | "print(v)" 700 | ] 701 | }, 702 | { 703 | "cell_type": "markdown", 704 | "metadata": {}, 705 | "source": [ 706 | "## Dictionaries" 707 | ] 708 | }, 709 | { 710 | "cell_type": "code", 711 | "execution_count": 34, 712 | "metadata": { 713 | "collapsed": false 714 | }, 715 | "outputs": [ 716 | { 717 | "name": "stdout", 718 | "output_type": "stream", 719 | "text": [ 720 | "{3: 42, 'f': 'fmi', 'a': 'alex'}\n" 721 | ] 722 | } 723 | ], 724 | "source": [ 725 | "d = {}\n", 726 | "d['a'] = 'alex'\n", 727 | "d['f'] = 'fmi'\n", 728 | "d[3] = 42\n", 729 | "print(d)" 730 | ] 731 | }, 732 | { 733 | "cell_type": "code", 734 | "execution_count": 35, 735 | "metadata": { 736 | "collapsed": false 737 | }, 738 | "outputs": [ 739 | { 740 | "name": "stdout", 741 | "output_type": "stream", 742 | "text": [ 743 | "Key: 3; Value: 42\n", 744 | "Key: f; Value: fmi\n", 745 | "Key: a; Value: alex\n" 746 | ] 747 | } 748 | ], 749 | "source": [ 750 | "for key in d:\n", 751 | " print ('Key: {0}; Value: {1}'.format(key, d[key]))" 752 | ] 753 | }, 754 | { 755 | "cell_type": "code", 756 | "execution_count": 36, 757 | "metadata": { 758 | "collapsed": false 759 | }, 760 | "outputs": [ 761 | { 762 | "name": "stdout", 763 | "output_type": "stream", 764 | "text": [ 765 | "43\n" 766 | ] 767 | } 768 | ], 769 | "source": [ 770 | "d[3] += 1\n", 771 | "print(d[3])" 772 | ] 773 | }, 774 | { 775 | "cell_type": "markdown", 776 | "metadata": {}, 777 | "source": [ 778 | "## Conditionals and loops" 779 | ] 780 | }, 781 | { 782 | "cell_type": "code", 783 | "execution_count": 37, 784 | "metadata": { 785 | "collapsed": false 786 | }, 787 | "outputs": [ 788 | { 789 | "name": "stdout", 790 | "output_type": "stream", 791 | "text": [ 792 | "I want 1 apples.\n", 793 | "I want 2 apples.\n", 794 | "I want 3 apples.\n", 795 | "I want 4 apples.\n", 796 | "I want 5 apples.\n" 797 | ] 798 | } 799 | ], 800 | "source": [ 801 | "for x in range(5):\n", 802 | " print(\"I want \" + str(x + 1) + \" apples.\")" 803 | ] 804 | }, 805 | { 806 | "cell_type": "code", 807 | "execution_count": 38, 808 | "metadata": { 809 | "collapsed": false 810 | }, 811 | "outputs": [ 812 | { 813 | "name": "stdout", 814 | "output_type": "stream", 815 | "text": [ 816 | "the answer to life, the universe and everything is 42\n" 817 | ] 818 | } 819 | ], 820 | "source": [ 821 | "this_is_true = True\n", 822 | "if this_is_true:\n", 823 | " print(\"the answer to life, the universe and everything is 42\")\n", 824 | "else:\n", 825 | " whatever # change this_is_true to False" 826 | ] 827 | }, 828 | { 829 | "cell_type": "code", 830 | "execution_count": 39, 831 | "metadata": { 832 | "collapsed": false 833 | }, 834 | "outputs": [ 835 | { 836 | "name": "stdout", 837 | "output_type": "stream", 838 | "text": [ 839 | "I am number 2\n", 840 | "I am number 3\n", 841 | "I am number 4\n" 842 | ] 843 | } 844 | ], 845 | "source": [ 846 | "x = 0\n", 847 | "while x <= 5:\n", 848 | " x += 1\n", 849 | " \n", 850 | " if x == 1:\n", 851 | " continue\n", 852 | "\n", 853 | " print(\"I am number\", x)\n", 854 | " \n", 855 | " if x == 4:\n", 856 | " break" 857 | ] 858 | }, 859 | { 860 | "cell_type": "markdown", 861 | "metadata": {}, 862 | "source": [ 863 | "## List Comprehension" 864 | ] 865 | }, 866 | { 867 | "cell_type": "code", 868 | "execution_count": 40, 869 | "metadata": { 870 | "collapsed": false 871 | }, 872 | "outputs": [ 873 | { 874 | "name": "stdout", 875 | "output_type": "stream", 876 | "text": [ 877 | "[4, 6, 10, 14, 22]\n" 878 | ] 879 | } 880 | ], 881 | "source": [ 882 | "v = [2, 3, 5, 7, 11]\n", 883 | "doubles = [x*2 for x in v]\n", 884 | "print(doubles)" 885 | ] 886 | }, 887 | { 888 | "cell_type": "code", 889 | "execution_count": 41, 890 | "metadata": { 891 | "collapsed": false 892 | }, 893 | "outputs": [ 894 | { 895 | "data": { 896 | "text/plain": [ 897 | "[1, 9, 25, 49, 81, 121, 169, 225, 289, 361, 441, 529, 625, 729, 841]" 898 | ] 899 | }, 900 | "execution_count": 41, 901 | "metadata": {}, 902 | "output_type": "execute_result" 903 | } 904 | ], 905 | "source": [ 906 | "[x*x for x in range(30) if x % 2 == 1]" 907 | ] 908 | }, 909 | { 910 | "cell_type": "code", 911 | "execution_count": 42, 912 | "metadata": { 913 | "collapsed": false 914 | }, 915 | "outputs": [ 916 | { 917 | "data": { 918 | "text/plain": [ 919 | "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]" 920 | ] 921 | }, 922 | "execution_count": 42, 923 | "metadata": {}, 924 | "output_type": "execute_result" 925 | } 926 | ], 927 | "source": [ 928 | "N = 10\n", 929 | "[0] * N" 930 | ] 931 | }, 932 | { 933 | "cell_type": "code", 934 | "execution_count": 43, 935 | "metadata": { 936 | "collapsed": false 937 | }, 938 | "outputs": [ 939 | { 940 | "data": { 941 | "text/plain": [ 942 | "[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", 943 | " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", 944 | " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", 945 | " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", 946 | " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", 947 | " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", 948 | " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", 949 | " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", 950 | " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n", 951 | " [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]" 952 | ] 953 | }, 954 | "execution_count": 43, 955 | "metadata": {}, 956 | "output_type": "execute_result" 957 | } 958 | ], 959 | "source": [ 960 | "[[0]*N for i in range(N)]" 961 | ] 962 | }, 963 | { 964 | "cell_type": "markdown", 965 | "metadata": {}, 966 | "source": [ 967 | "## Sexy stuff" 968 | ] 969 | }, 970 | { 971 | "cell_type": "code", 972 | "execution_count": 44, 973 | "metadata": { 974 | "collapsed": false 975 | }, 976 | "outputs": [ 977 | { 978 | "name": "stdout", 979 | "output_type": "stream", 980 | "text": [ 981 | "[25, 63, 96, 81, 90, 39, 23, 54, 8, 86]\n" 982 | ] 983 | } 984 | ], 985 | "source": [ 986 | "# Syntax for importing\n", 987 | "from random import randint\n", 988 | "\n", 989 | "v = [randint(1, 100) for i in range(10)]\n", 990 | "print(v)" 991 | ] 992 | }, 993 | { 994 | "cell_type": "code", 995 | "execution_count": 45, 996 | "metadata": { 997 | "collapsed": false 998 | }, 999 | "outputs": [ 1000 | { 1001 | "name": "stdout", 1002 | "output_type": "stream", 1003 | "text": [ 1004 | "[8, 23, 25, 39, 54, 63, 81, 86, 90, 96]\n" 1005 | ] 1006 | } 1007 | ], 1008 | "source": [ 1009 | "v.sort()\n", 1010 | "print(v)" 1011 | ] 1012 | }, 1013 | { 1014 | "cell_type": "code", 1015 | "execution_count": 46, 1016 | "metadata": { 1017 | "collapsed": false 1018 | }, 1019 | "outputs": [ 1020 | { 1021 | "data": { 1022 | "text/plain": [ 1023 | "'1 3 7 13 2'" 1024 | ] 1025 | }, 1026 | "execution_count": 46, 1027 | "metadata": {}, 1028 | "output_type": "execute_result" 1029 | } 1030 | ], 1031 | "source": [ 1032 | "line = \"1 3 7 13 2\\n\"\n", 1033 | "line.strip()" 1034 | ] 1035 | }, 1036 | { 1037 | "cell_type": "code", 1038 | "execution_count": 47, 1039 | "metadata": { 1040 | "collapsed": false 1041 | }, 1042 | "outputs": [ 1043 | { 1044 | "data": { 1045 | "text/plain": [ 1046 | "['1', '3', '7', '13', '2']" 1047 | ] 1048 | }, 1049 | "execution_count": 47, 1050 | "metadata": {}, 1051 | "output_type": "execute_result" 1052 | } 1053 | ], 1054 | "source": [ 1055 | "line.strip().split()" 1056 | ] 1057 | }, 1058 | { 1059 | "cell_type": "code", 1060 | "execution_count": 48, 1061 | "metadata": { 1062 | "collapsed": false 1063 | }, 1064 | "outputs": [ 1065 | { 1066 | "data": { 1067 | "text/plain": [ 1068 | "[1, 3, 7, 13, 2]" 1069 | ] 1070 | }, 1071 | "execution_count": 48, 1072 | "metadata": {}, 1073 | "output_type": "execute_result" 1074 | } 1075 | ], 1076 | "source": [ 1077 | "[int(x) for x in line.strip().split()]" 1078 | ] 1079 | }, 1080 | { 1081 | "cell_type": "code", 1082 | "execution_count": 49, 1083 | "metadata": { 1084 | "collapsed": false 1085 | }, 1086 | "outputs": [ 1087 | { 1088 | "data": { 1089 | "text/plain": [ 1090 | "[1, 2, 3, 7, 13]" 1091 | ] 1092 | }, 1093 | "execution_count": 49, 1094 | "metadata": {}, 1095 | "output_type": "execute_result" 1096 | } 1097 | ], 1098 | "source": [ 1099 | "sorted([int(x) for x in line.strip().split()])" 1100 | ] 1101 | }, 1102 | { 1103 | "cell_type": "code", 1104 | "execution_count": 50, 1105 | "metadata": { 1106 | "collapsed": false 1107 | }, 1108 | "outputs": [ 1109 | { 1110 | "name": "stdout", 1111 | "output_type": "stream", 1112 | "text": [ 1113 | "1 13\n" 1114 | ] 1115 | } 1116 | ], 1117 | "source": [ 1118 | "[alfa, beta, gamma, epsilon, omega] = sorted([int(x) for x in line.strip().split()])\n", 1119 | "print(alfa, omega)" 1120 | ] 1121 | }, 1122 | { 1123 | "cell_type": "code", 1124 | "execution_count": 51, 1125 | "metadata": { 1126 | "collapsed": false 1127 | }, 1128 | "outputs": [ 1129 | { 1130 | "name": "stdout", 1131 | "output_type": "stream", 1132 | "text": [ 1133 | "7\n" 1134 | ] 1135 | } 1136 | ], 1137 | "source": [ 1138 | "beta, epsilon = epsilon, beta\n", 1139 | "print(beta)" 1140 | ] 1141 | }, 1142 | { 1143 | "cell_type": "markdown", 1144 | "metadata": {}, 1145 | "source": [ 1146 | "## Smart stuff" 1147 | ] 1148 | }, 1149 | { 1150 | "cell_type": "code", 1151 | "execution_count": 52, 1152 | "metadata": { 1153 | "collapsed": false 1154 | }, 1155 | "outputs": [ 1156 | { 1157 | "name": "stdout", 1158 | "output_type": "stream", 1159 | "text": [ 1160 | "1 8\n", 1161 | "2 10\n", 1162 | "3 10\n", 1163 | "4 12\n", 1164 | "5 8\n", 1165 | "6 7\n", 1166 | "7 8\n", 1167 | "8 10\n", 1168 | "9 10\n", 1169 | "10 17\n" 1170 | ] 1171 | } 1172 | ], 1173 | "source": [ 1174 | "# How to count number of appearances\n", 1175 | "from collections import defaultdict\n", 1176 | "\n", 1177 | "v = [randint(1, 10) for i in range(100)]\n", 1178 | "d = defaultdict(int)\n", 1179 | "\n", 1180 | "for x in v:\n", 1181 | " d[x] += 1\n", 1182 | "\n", 1183 | "for key in d:\n", 1184 | " print(key, d[key])" 1185 | ] 1186 | }, 1187 | { 1188 | "cell_type": "markdown", 1189 | "metadata": {}, 1190 | "source": [ 1191 | "### Combinatorics" 1192 | ] 1193 | }, 1194 | { 1195 | "cell_type": "code", 1196 | "execution_count": 53, 1197 | "metadata": { 1198 | "collapsed": false 1199 | }, 1200 | "outputs": [ 1201 | { 1202 | "name": "stdout", 1203 | "output_type": "stream", 1204 | "text": [ 1205 | "A B C D\n", 1206 | "A B D C\n", 1207 | "A C B D\n", 1208 | "A C D B\n", 1209 | "A D B C\n", 1210 | "A D C B\n", 1211 | "B A C D\n", 1212 | "B A D C\n", 1213 | "B C A D\n", 1214 | "B C D A\n", 1215 | "B D A C\n", 1216 | "B D C A\n", 1217 | "C A B D\n", 1218 | "C A D B\n", 1219 | "C B A D\n", 1220 | "C B D A\n", 1221 | "C D A B\n", 1222 | "C D B A\n", 1223 | "D A B C\n", 1224 | "D A C B\n", 1225 | "D B A C\n", 1226 | "D B C A\n", 1227 | "D C A B\n", 1228 | "D C B A\n" 1229 | ] 1230 | } 1231 | ], 1232 | "source": [ 1233 | "v = 'ABCD'\n", 1234 | "\n", 1235 | "from itertools import permutations\n", 1236 | "\n", 1237 | "for p in permutations(v):\n", 1238 | " print(\" \".join(p))" 1239 | ] 1240 | }, 1241 | { 1242 | "cell_type": "code", 1243 | "execution_count": 54, 1244 | "metadata": { 1245 | "collapsed": false 1246 | }, 1247 | "outputs": [ 1248 | { 1249 | "name": "stdout", 1250 | "output_type": "stream", 1251 | "text": [ 1252 | "A B\n", 1253 | "A C\n", 1254 | "A D\n", 1255 | "B C\n", 1256 | "B D\n", 1257 | "C D\n" 1258 | ] 1259 | } 1260 | ], 1261 | "source": [ 1262 | "from itertools import combinations\n", 1263 | "for c in combinations(v, 2):\n", 1264 | " print(\" \".join(c))" 1265 | ] 1266 | }, 1267 | { 1268 | "cell_type": "markdown", 1269 | "metadata": {}, 1270 | "source": [ 1271 | "# The End" 1272 | ] 1273 | }, 1274 | { 1275 | "cell_type": "code", 1276 | "execution_count": 55, 1277 | "metadata": { 1278 | "collapsed": false 1279 | }, 1280 | "outputs": [ 1281 | { 1282 | "name": "stdout", 1283 | "output_type": "stream", 1284 | "text": [ 1285 | "The Zen of Python, by Tim Peters\n", 1286 | "\n", 1287 | "Beautiful is better than ugly.\n", 1288 | "Explicit is better than implicit.\n", 1289 | "Simple is better than complex.\n", 1290 | "Complex is better than complicated.\n", 1291 | "Flat is better than nested.\n", 1292 | "Sparse is better than dense.\n", 1293 | "Readability counts.\n", 1294 | "Special cases aren't special enough to break the rules.\n", 1295 | "Although practicality beats purity.\n", 1296 | "Errors should never pass silently.\n", 1297 | "Unless explicitly silenced.\n", 1298 | "In the face of ambiguity, refuse the temptation to guess.\n", 1299 | "There should be one-- and preferably only one --obvious way to do it.\n", 1300 | "Although that way may not be obvious at first unless you're Dutch.\n", 1301 | "Now is better than never.\n", 1302 | "Although never is often better than *right* now.\n", 1303 | "If the implementation is hard to explain, it's a bad idea.\n", 1304 | "If the implementation is easy to explain, it may be a good idea.\n", 1305 | "Namespaces are one honking great idea -- let's do more of those!\n" 1306 | ] 1307 | } 1308 | ], 1309 | "source": [ 1310 | "import this" 1311 | ] 1312 | } 1313 | ], 1314 | "metadata": { 1315 | "kernelspec": { 1316 | "display_name": "Python 3", 1317 | "language": "python", 1318 | "name": "python3" 1319 | }, 1320 | "language_info": { 1321 | "codemirror_mode": { 1322 | "name": "ipython", 1323 | "version": 3 1324 | }, 1325 | "file_extension": ".py", 1326 | "mimetype": "text/x-python", 1327 | "name": "python", 1328 | "nbconvert_exporter": "python", 1329 | "pygments_lexer": "ipython3", 1330 | "version": "3.5.1" 1331 | } 1332 | }, 1333 | "nbformat": 4, 1334 | "nbformat_minor": 0 1335 | } 1336 | --------------------------------------------------------------------------------