├── .gitignore ├── 01. Python 的安裝及基本畫圖.ipynb ├── 02. Markdown 和 Python 基本語法.ipynb ├── 03. 函式寫法和 Python 程式檔.ipynb ├── 04. Jupyter Notebook 的互動功能.ipynb ├── 05. 自建類別和字串格式化進階.ipynb ├── 06. 檔案的讀取和爬蟲.ipynb ├── 07. APCS 精進計畫 I.ipynb ├── 08. APCS 精進計畫 II.ipynb ├── 09. APCS 精進計畫 III.ipynb ├── 10. MicroBit 等等好玩的程式設計.ipynb ├── APCS 練習區 ├── APCS171028 │ ├── p1.ipynb │ ├── p1.py │ ├── p1t01.txt │ ├── p1t02.txt │ ├── p1t03.txt │ ├── p1t04.txt │ ├── p2.ipynb │ ├── p2.py │ ├── p2t01.txt │ ├── p2t02.txt │ ├── p2t03.txt │ ├── p2t04.txt │ ├── p3.ipynb │ ├── p3.py │ ├── p3t01.txt │ ├── p3t02.txt │ └── p4.ipynb └── APCS1806 │ ├── Untitled.ipynb │ ├── 第 1 題 特殊編碼.ipynb │ └── 第 2 題 完全奇數.ipynb ├── Python 問題集 ├── 我的、你的、我們的紀念日.ipynb ├── 會寫詩的文青機器人.ipynb ├── 猜數字遊戲.ipynb └── 考拉茲猜想(冰雹數列).ipynb ├── README.md ├── YW式APCS專區 ├── 105-10 觀念題翻Python.ipynb ├── 105-106考題 │ ├── 實作題 │ │ ├── 1050305APCSImplementation.pdf │ │ ├── 1051029APCSImplementation.pdf │ │ ├── 1060304APCSImplementation.pdf │ │ ├── 1061028APCSImplementation.pdf │ │ └── 10706APCSImplementation.pdf │ └── 觀念題 │ │ ├── 1050305APCSconcept.pdf │ │ ├── 1051029APCSconcept.pdf │ │ └── 1060304APCSconcept.pdf ├── 105-3 觀念題翻Python.ipynb ├── 106-3 觀念題翻Python.ipynb ├── 常用語法與常用模型.ipynb ├── 觀念解釋7_24.ipynb ├── 觀念解釋7_25.ipynb ├── 觀念解釋7_29.ipynb ├── 觀念解釋7_30.ipynb ├── 觀念解釋7_31.ipynb └── 觀念解釋8_1.ipynb ├── guessing_game.py ├── images ├── PWA.png ├── T_apcs01.png ├── T_apcs02.png ├── T_apcs_env.png ├── T_display.png ├── T_files.png ├── T_for.png ├── T_interact.png ├── T_jupyter_notebook.png ├── T_map_and_filter.png ├── T_microbit.png ├── T_plot101.png ├── T_python_install.png ├── T_range.png ├── T_while.png ├── append.png ├── class.png ├── comic01.png ├── crawler.png ├── ellipse.png ├── figure_axes.png ├── filter.png ├── fist_rock.png ├── flow2.gif ├── format.png ├── function.png ├── function_str.png ├── game_terminal.png ├── hexagram.png ├── html01.png ├── if.png ├── index.png ├── input.png ├── jupyter_notebook.png ├── lambda.png ├── latex_tab.png ├── leetcode01.png ├── leetcode02.png ├── leetcode03.png ├── linspace.png ├── list_operator.png ├── map.png ├── markdown.png ├── matplotlib_inline.png ├── matplotlib_quick_color.png ├── method.png ├── mj01s.png ├── number_formating.png ├── pig.png ├── poem.png ├── polar.png ├── print_to_file.png ├── python_file.png ├── pythontutor.png ├── pythontutor_visual.png ├── randint.png ├── reverse.png ├── run_poem.png ├── scratch.gif ├── slat.png ├── soobi.png ├── special_method.png ├── string_and_list.png ├── type.png ├── web01.png ├── web02.png ├── web03.png └── xkcd.png ├── mydic.csv ├── mydic.pickle ├── p1.py ├── poem.py ├── test01.txt ├── test02.txt └── 投影片 └── Git與GitHub的介紹.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | .ipynb_checkpoints 2 | .DS_Store 3 | slides/Python\ AI\ 課程.key 4 | tmp/ 5 | -------------------------------------------------------------------------------- /03. 函式寫法和 Python 程式檔.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![String and List 的二三事](images/string_and_list.png)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### 問題\n", 15 | "\n", 16 | "假設你要去瑞士玩, 朋友請你帶三個東西, 你查到瑞士法郎的價格是\n", 17 | "\n", 18 | " [3000, 2500, 100]\n", 19 | "\n", 20 | "Google 一下, 發現今天滙率為\n", 21 | "\n", 22 | " 1法郎 = 31.4862596台幣\n", 23 | " \n", 24 | "試著把這三個物品的價格換成台幣, 再存起來。" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": {}, 31 | "outputs": [ 32 | { 33 | "name": "stdout", 34 | "output_type": "stream", 35 | "text": [ 36 | "[94458.7788, 78715.649, 3148.62596]\n" 37 | ] 38 | } 39 | ], 40 | "source": [ 41 | "price = [3000, 2500, 100]\n", 42 | "\n", 43 | "c = 31.4862596\n", 44 | "\n", 45 | "price_tw = []\n", 46 | "\n", 47 | "for p in price:\n", 48 | " ptw = p*c\n", 49 | " price_tw.append(ptw)\n", 50 | " \n", 51 | "print(price_tw)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "markdown", 56 | "metadata": {}, 57 | "source": [ 58 | "![append](images/append.png)" 59 | ] 60 | }, 61 | { 62 | "cell_type": "markdown", 63 | "metadata": {}, 64 | "source": [ 65 | "### 字串分割技巧\n", 66 | "\n", 67 | "Python 的字串處理能力是有名的強, 比方說我們現在有一筆 CSV 形式的字串, 要分割可以用\n", 68 | "\n", 69 | " .split\n", 70 | " \n", 71 | "來做。" 72 | ] 73 | }, 74 | { 75 | "cell_type": "code", 76 | "execution_count": 2, 77 | "metadata": {}, 78 | "outputs": [], 79 | "source": [ 80 | "st = '23,68,99'" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 3, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "data": { 90 | "text/plain": [ 91 | "['23', '68', '99']" 92 | ] 93 | }, 94 | "execution_count": 3, 95 | "metadata": {}, 96 | "output_type": "execute_result" 97 | } 98 | ], 99 | "source": [ 100 | "st.split(',')" 101 | ] 102 | }, 103 | { 104 | "cell_type": "markdown", 105 | "metadata": {}, 106 | "source": [ 107 | "### 問題\n", 108 | "\n", 109 | "這樣子的確把\n", 110 | "\n", 111 | " st = '23,68,99'\n", 112 | " \n", 113 | "三筆資料切開, 但我們希望把這三筆資料轉成整數, 再存到一個串列之中。" 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "execution_count": 7, 119 | "metadata": {}, 120 | "outputs": [], 121 | "source": [ 122 | "s1 = st.split(',')\n", 123 | "s2 = []\n", 124 | "\n", 125 | "for x in s1:\n", 126 | " k = int(x)\n", 127 | " s2.append(k)" 128 | ] 129 | }, 130 | { 131 | "cell_type": "code", 132 | "execution_count": 8, 133 | "metadata": {}, 134 | "outputs": [ 135 | { 136 | "data": { 137 | "text/plain": [ 138 | "[23, 68, 99]" 139 | ] 140 | }, 141 | "execution_count": 8, 142 | "metadata": {}, 143 | "output_type": "execute_result" 144 | } 145 | ], 146 | "source": [ 147 | "s2" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "當然, 如果對程式熟悉, 我們也可能很多變數不設就直接用。" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 9, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "s2 = []\n", 164 | "\n", 165 | "for x in st.split(','):\n", 166 | " s2.append(int(x))" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": 10, 172 | "metadata": {}, 173 | "outputs": [ 174 | { 175 | "data": { 176 | "text/plain": [ 177 | "[23, 68, 99]" 178 | ] 179 | }, 180 | "execution_count": 10, 181 | "metadata": {}, 182 | "output_type": "execute_result" 183 | } 184 | ], 185 | "source": [ 186 | "s2" 187 | ] 188 | }, 189 | { 190 | "cell_type": "markdown", 191 | "metadata": {}, 192 | "source": [ 193 | "### 問題\n", 194 | "\n", 195 | "繼然我們換成數字了, 不如來試著求 [23, 68, 99] 之和。" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 11, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "name": "stdout", 205 | "output_type": "stream", 206 | "text": [ 207 | "總和為 190\n" 208 | ] 209 | } 210 | ], 211 | "source": [ 212 | "s = 0 \n", 213 | "\n", 214 | "for k in s2:\n", 215 | " s = s + k\n", 216 | "\n", 217 | "print('總和為', s)" 218 | ] 219 | }, 220 | { 221 | "cell_type": "markdown", 222 | "metadata": {}, 223 | "source": [ 224 | "### 【小技巧】\n", 225 | "\n", 226 | "其實求一個串列中所有數字的和, Python 有快速指令..." 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 12, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "190" 238 | ] 239 | }, 240 | "execution_count": 12, 241 | "metadata": {}, 242 | "output_type": "execute_result" 243 | } 244 | ], 245 | "source": [ 246 | "sum(s2)" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "### 【小技巧】\n", 254 | "\n", 255 | "不論要看一個串列, 還是一個字串的長度, 我們都可以用 `len` 指令。" 256 | ] 257 | }, 258 | { 259 | "cell_type": "code", 260 | "execution_count": 13, 261 | "metadata": {}, 262 | "outputs": [ 263 | { 264 | "data": { 265 | "text/plain": [ 266 | "3" 267 | ] 268 | }, 269 | "execution_count": 13, 270 | "metadata": {}, 271 | "output_type": "execute_result" 272 | } 273 | ], 274 | "source": [ 275 | "len(s2)" 276 | ] 277 | }, 278 | { 279 | "cell_type": "markdown", 280 | "metadata": {}, 281 | "source": [ 282 | "### 問題\n", 283 | "\n", 284 | "把一個串列倒過來列。比如說我們有個串列是:\n", 285 | "\n", 286 | "['A', 'B', 'C', 'D']\n", 287 | "\n", 288 | "我們希望得到\n", 289 | "\n", 290 | "['D', 'C', 'B', 'A']" 291 | ] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": {}, 296 | "source": [ 297 | "![反過來的串列取法](images/reverse.png)" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 14, 303 | "metadata": {}, 304 | "outputs": [], 305 | "source": [ 306 | "mylist = list('ABCD')" 307 | ] 308 | }, 309 | { 310 | "cell_type": "code", 311 | "execution_count": 15, 312 | "metadata": {}, 313 | "outputs": [ 314 | { 315 | "data": { 316 | "text/plain": [ 317 | "['A', 'B', 'C', 'D']" 318 | ] 319 | }, 320 | "execution_count": 15, 321 | "metadata": {}, 322 | "output_type": "execute_result" 323 | } 324 | ], 325 | "source": [ 326 | "mylist" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "想好策略, 就是由索引 -1, -2, ..., -n 取出 `mylist` 裡的元素, 一一放入新的 `newlist`。" 334 | ] 335 | }, 336 | { 337 | "cell_type": "code", 338 | "execution_count": 16, 339 | "metadata": {}, 340 | "outputs": [], 341 | "source": [ 342 | "n = len(mylist)\n", 343 | "newlist = []\n", 344 | "\n", 345 | "# 需要產生 1, 2, 3, ..., n 的 list -> range(1, n+1)\n", 346 | "\n", 347 | "for i in range(1, n+1):\n", 348 | " newlist.append(mylist[-i])" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": 17, 354 | "metadata": {}, 355 | "outputs": [ 356 | { 357 | "data": { 358 | "text/plain": [ 359 | "['D', 'C', 'B', 'A']" 360 | ] 361 | }, 362 | "execution_count": 17, 363 | "metadata": {}, 364 | "output_type": "execute_result" 365 | } 366 | ], 367 | "source": [ 368 | "newlist" 369 | ] 370 | }, 371 | { 372 | "cell_type": "markdown", 373 | "metadata": {}, 374 | "source": [ 375 | "成功了! \n", 376 | "\n", 377 | "有時我們看到一個問題, 不要馬上開始寫程式, 先想想中間的邏輯, 然後想想我們的策略, 再把程式寫出來!\n", 378 | "\n", 379 | "接著我們來挑戰很類似的問題, 這字把字串倒過來!" 380 | ] 381 | }, 382 | { 383 | "cell_type": "markdown", 384 | "metadata": {}, 385 | "source": [ 386 | "### 問題\n", 387 | "\n", 388 | "給定一個字串, 我們要把字串倒過來!\n", 389 | "\n", 390 | "比如說原本有個字串是\n", 391 | "\n", 392 | "'ABCD'\n", 393 | "\n", 394 | "要變成\n", 395 | "\n", 396 | "'DCBA'" 397 | ] 398 | }, 399 | { 400 | "cell_type": "code", 401 | "execution_count": 18, 402 | "metadata": {}, 403 | "outputs": [], 404 | "source": [ 405 | "mystr = 'ABCD'\n", 406 | "\n", 407 | "n = len(mystr)\n", 408 | "newstr = ''\n", 409 | "\n", 410 | "for i in range(1, n+1):\n", 411 | " newstr = newstr + mystr[-i]" 412 | ] 413 | }, 414 | { 415 | "cell_type": "code", 416 | "execution_count": 19, 417 | "metadata": {}, 418 | "outputs": [ 419 | { 420 | "data": { 421 | "text/plain": [ 422 | "'DCBA'" 423 | ] 424 | }, 425 | "execution_count": 19, 426 | "metadata": {}, 427 | "output_type": "execute_result" 428 | } 429 | ], 430 | "source": [ 431 | "newstr" 432 | ] 433 | }, 434 | { 435 | "cell_type": "markdown", 436 | "metadata": {}, 437 | "source": [ 438 | "所以整個架構基本上是一樣的。" 439 | ] 440 | }, 441 | { 442 | "cell_type": "markdown", 443 | "metadata": {}, 444 | "source": [ 445 | "### 【小技巧】\n", 446 | "\n", 447 | "在 Python 中, 要把一個串列或字串倒過來, 其實不用這麼麻煩..." 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 20, 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "data": { 457 | "text/plain": [ 458 | "['D', 'C', 'B', 'A']" 459 | ] 460 | }, 461 | "execution_count": 20, 462 | "metadata": {}, 463 | "output_type": "execute_result" 464 | } 465 | ], 466 | "source": [ 467 | "mylist[::-1]" 468 | ] 469 | }, 470 | { 471 | "cell_type": "code", 472 | "execution_count": 21, 473 | "metadata": {}, 474 | "outputs": [ 475 | { 476 | "data": { 477 | "text/plain": [ 478 | "'DCBA'" 479 | ] 480 | }, 481 | "execution_count": 21, 482 | "metadata": {}, 483 | "output_type": "execute_result" 484 | } 485 | ], 486 | "source": [ 487 | "mystr[::-1]" 488 | ] 489 | }, 490 | { 491 | "cell_type": "markdown", 492 | "metadata": {}, 493 | "source": [ 494 | "![練腦袋](images/comic01.png)" 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": {}, 500 | "source": [ 501 | "![函式的寫法](images/function.png)" 502 | ] 503 | }, 504 | { 505 | "cell_type": "markdown", 506 | "metadata": {}, 507 | "source": [ 508 | "我們現在要進入 Python 基礎篇最後一個、也是相當重要的主題, 就是函式的寫法。" 509 | ] 510 | }, 511 | { 512 | "cell_type": "markdown", 513 | "metadata": {}, 514 | "source": [ 515 | "![函式結構](images/function_str.png)" 516 | ] 517 | }, 518 | { 519 | "cell_type": "markdown", 520 | "metadata": {}, 521 | "source": [ 522 | "假設我們要做一個簡單的函數, 輸入 $x$, 輸出 $x^2$。" 523 | ] 524 | }, 525 | { 526 | "cell_type": "code", 527 | "execution_count": 23, 528 | "metadata": {}, 529 | "outputs": [], 530 | "source": [ 531 | "def square(x):\n", 532 | " return x**2" 533 | ] 534 | }, 535 | { 536 | "cell_type": "code", 537 | "execution_count": 24, 538 | "metadata": {}, 539 | "outputs": [ 540 | { 541 | "data": { 542 | "text/plain": [ 543 | "9" 544 | ] 545 | }, 546 | "execution_count": 24, 547 | "metadata": {}, 548 | "output_type": "execute_result" 549 | } 550 | ], 551 | "source": [ 552 | "square(3)" 553 | ] 554 | }, 555 | { 556 | "cell_type": "markdown", 557 | "metadata": {}, 558 | "source": [ 559 | "寫成函式的好處是可以一直重覆使用! 剛開始大家的疑問可能是 `return` 是什麼意思, 我們再用另一個幾乎一樣的函式來說明。" 560 | ] 561 | }, 562 | { 563 | "cell_type": "code", 564 | "execution_count": 25, 565 | "metadata": {}, 566 | "outputs": [], 567 | "source": [ 568 | "def 平方(x):\n", 569 | " print(x**2)" 570 | ] 571 | }, 572 | { 573 | "cell_type": "code", 574 | "execution_count": 26, 575 | "metadata": {}, 576 | "outputs": [ 577 | { 578 | "name": "stdout", 579 | "output_type": "stream", 580 | "text": [ 581 | "9\n" 582 | ] 583 | } 584 | ], 585 | "source": [ 586 | "平方(3)" 587 | ] 588 | }, 589 | { 590 | "cell_type": "markdown", 591 | "metadata": {}, 592 | "source": [ 593 | "耶, 這看起來一模一樣! 有什麼差別呢? 答案是有 `return` 函式會回傳一個值回來, 沒有的話, 函式不會回傳任何東西。我們看個例子就更明白。" 594 | ] 595 | }, 596 | { 597 | "cell_type": "code", 598 | "execution_count": 28, 599 | "metadata": {}, 600 | "outputs": [ 601 | { 602 | "name": "stdout", 603 | "output_type": "stream", 604 | "text": [ 605 | "7569\n" 606 | ] 607 | } 608 | ], 609 | "source": [ 610 | "a = square(87)\n", 611 | "b = 平方(87)" 612 | ] 613 | }, 614 | { 615 | "cell_type": "markdown", 616 | "metadata": {}, 617 | "source": [ 618 | "看來 a 和 b 應該是一樣?" 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": 29, 624 | "metadata": {}, 625 | "outputs": [ 626 | { 627 | "data": { 628 | "text/plain": [ 629 | "7569" 630 | ] 631 | }, 632 | "execution_count": 29, 633 | "metadata": {}, 634 | "output_type": "execute_result" 635 | } 636 | ], 637 | "source": [ 638 | "a" 639 | ] 640 | }, 641 | { 642 | "cell_type": "code", 643 | "execution_count": 30, 644 | "metadata": {}, 645 | "outputs": [], 646 | "source": [ 647 | "b" 648 | ] 649 | }, 650 | { 651 | "cell_type": "markdown", 652 | "metadata": {}, 653 | "source": [ 654 | "結果是 a 真的會變成 87 的平方, 但 b 沒有任何值! 因為 \"平方\" 這個函數沒有任何值..." 655 | ] 656 | }, 657 | { 658 | "cell_type": "markdown", 659 | "metadata": {}, 660 | "source": [ 661 | "接著我們用新學會的反轉字串 (串列), 做成一個函數。" 662 | ] 663 | }, 664 | { 665 | "cell_type": "code", 666 | "execution_count": 31, 667 | "metadata": {}, 668 | "outputs": [], 669 | "source": [ 670 | "def reverse(s):\n", 671 | " return s[::-1]" 672 | ] 673 | }, 674 | { 675 | "cell_type": "code", 676 | "execution_count": 32, 677 | "metadata": {}, 678 | "outputs": [ 679 | { 680 | "data": { 681 | "text/plain": [ 682 | "'elppa'" 683 | ] 684 | }, 685 | "execution_count": 32, 686 | "metadata": {}, 687 | "output_type": "execute_result" 688 | } 689 | ], 690 | "source": [ 691 | "reverse('apple')" 692 | ] 693 | }, 694 | { 695 | "cell_type": "code", 696 | "execution_count": 33, 697 | "metadata": {}, 698 | "outputs": [ 699 | { 700 | "data": { 701 | "text/plain": [ 702 | "['d', 'c', 'b', 'a']" 703 | ] 704 | }, 705 | "execution_count": 33, 706 | "metadata": {}, 707 | "output_type": "execute_result" 708 | } 709 | ], 710 | "source": [ 711 | "reverse(['a', 'b', 'c', 'd'])" 712 | ] 713 | }, 714 | { 715 | "cell_type": "markdown", 716 | "metadata": {}, 717 | "source": [ 718 | "很神奇的是, 這個函數不管是對串列或是對字串都是一體適用的!" 719 | ] 720 | }, 721 | { 722 | "cell_type": "markdown", 723 | "metadata": {}, 724 | "source": [ 725 | "接著我們想做一個猜數字遊戲, 並且把它設計成一個函數。為了這個, 我們介紹從 `random` 套件庫來的 `randint` 指令。" 726 | ] 727 | }, 728 | { 729 | "cell_type": "markdown", 730 | "metadata": {}, 731 | "source": [ 732 | "![randint](images/randint.png)" 733 | ] 734 | }, 735 | { 736 | "cell_type": "markdown", 737 | "metadata": {}, 738 | "source": [ 739 | "### 問題: 猜數字遊戲\n", 740 | "\n", 741 | "電腦從 1-100 之間「想」一個數字。玩家開始猜電腦的數字, 電腦回饋「太大」或「太小」, 直到猜對為止。" 742 | ] 743 | }, 744 | { 745 | "cell_type": "code", 746 | "execution_count": 35, 747 | "metadata": {}, 748 | "outputs": [], 749 | "source": [ 750 | "from random import randint" 751 | ] 752 | }, 753 | { 754 | "cell_type": "code", 755 | "execution_count": 36, 756 | "metadata": {}, 757 | "outputs": [], 758 | "source": [ 759 | "def game():\n", 760 | " ans = randint(1, 100)\n", 761 | " guess = -1\n", 762 | "\n", 763 | " while guess != ans:\n", 764 | " guess = int(input(\"請輸入一個數字: \"))\n", 765 | "\n", 766 | " if guess>ans:\n", 767 | " print(\"太大了!\")\n", 768 | " elif guessans:\n", 906 | " print(\"太大了!\")\n", 907 | " elif guess" 120 | ] 121 | }, 122 | "execution_count": 22, 123 | "metadata": {}, 124 | "output_type": "execute_result" 125 | } 126 | ], 127 | "source": [ 128 | "interact(f, x=3)" 129 | ] 130 | }, 131 | { 132 | "cell_type": "markdown", 133 | "metadata": {}, 134 | "source": [ 135 | "這樣數值滑桿就出現了!! 這樣不是浮點數, 有可能變成浮點數數值滑桿嗎?" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 24, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "data": { 145 | "application/vnd.jupyter.widget-view+json": { 146 | "model_id": "93419e4aebc0423e9ff16a3947e2b3c1", 147 | "version_major": 2, 148 | "version_minor": 0 149 | }, 150 | "text/plain": [ 151 | "interactive(children=(FloatSlider(value=3.0, description='x', max=9.0, min=-3.0), Output()), _dom_classes=('wi…" 152 | ] 153 | }, 154 | "metadata": {}, 155 | "output_type": "display_data" 156 | }, 157 | { 158 | "data": { 159 | "text/plain": [ 160 | "" 161 | ] 162 | }, 163 | "execution_count": 24, 164 | "metadata": {}, 165 | "output_type": "execute_result" 166 | } 167 | ], 168 | "source": [ 169 | "interact(f, x=3.0)" 170 | ] 171 | }, 172 | { 173 | "cell_type": "markdown", 174 | "metadata": {}, 175 | "source": [ 176 | "指定範圍。" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 25, 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "data": { 186 | "application/vnd.jupyter.widget-view+json": { 187 | "model_id": "1af99c0f1b32472181f096bba2cf6931", 188 | "version_major": 2, 189 | "version_minor": 0 190 | }, 191 | "text/plain": [ 192 | "interactive(children=(IntSlider(value=5, description='x', max=10, min=1), Output()), _dom_classes=('widget-int…" 193 | ] 194 | }, 195 | "metadata": {}, 196 | "output_type": "display_data" 197 | }, 198 | { 199 | "data": { 200 | "text/plain": [ 201 | "" 202 | ] 203 | }, 204 | "execution_count": 25, 205 | "metadata": {}, 206 | "output_type": "execute_result" 207 | } 208 | ], 209 | "source": [ 210 | "interact(f, x=(1, 10))" 211 | ] 212 | }, 213 | { 214 | "cell_type": "markdown", 215 | "metadata": {}, 216 | "source": [ 217 | "### 文字框" 218 | ] 219 | }, 220 | { 221 | "cell_type": "code", 222 | "execution_count": 26, 223 | "metadata": {}, 224 | "outputs": [ 225 | { 226 | "data": { 227 | "application/vnd.jupyter.widget-view+json": { 228 | "model_id": "9435a62d2c5248b887db30126b70618f", 229 | "version_major": 2, 230 | "version_minor": 0 231 | }, 232 | "text/plain": [ 233 | "interactive(children=(Text(value='輸入你的姓名', description='x'), Output()), _dom_classes=('widget-interact',))" 234 | ] 235 | }, 236 | "metadata": {}, 237 | "output_type": "display_data" 238 | }, 239 | { 240 | "data": { 241 | "text/plain": [ 242 | "" 243 | ] 244 | }, 245 | "execution_count": 26, 246 | "metadata": {}, 247 | "output_type": "execute_result" 248 | } 249 | ], 250 | "source": [ 251 | "interact(f, x = \"輸入你的姓名\")" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "### 下拉式選單 (串列篇)" 259 | ] 260 | }, 261 | { 262 | "cell_type": "code", 263 | "execution_count": 27, 264 | "metadata": {}, 265 | "outputs": [ 266 | { 267 | "data": { 268 | "application/vnd.jupyter.widget-view+json": { 269 | "model_id": "98eb8c941bd2431891fd9c839eff68a8", 270 | "version_major": 2, 271 | "version_minor": 0 272 | }, 273 | "text/plain": [ 274 | "interactive(children=(Dropdown(description='x', options=('台北', '台中', '高雄'), value='台北'), Output()), _dom_class…" 275 | ] 276 | }, 277 | "metadata": {}, 278 | "output_type": "display_data" 279 | }, 280 | { 281 | "data": { 282 | "text/plain": [ 283 | "" 284 | ] 285 | }, 286 | "execution_count": 27, 287 | "metadata": {}, 288 | "output_type": "execute_result" 289 | } 290 | ], 291 | "source": [ 292 | "interact(f, x = [\"台北\", \"台中\", \"高雄\"])" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": {}, 298 | "source": [ 299 | "### 下拉式選單 (字典篇)" 300 | ] 301 | }, 302 | { 303 | "cell_type": "code", 304 | "execution_count": 30, 305 | "metadata": {}, 306 | "outputs": [ 307 | { 308 | "data": { 309 | "application/vnd.jupyter.widget-view+json": { 310 | "model_id": "ec88b870268743d5b5df44d7801fae78", 311 | "version_major": 2, 312 | "version_minor": 0 313 | }, 314 | "text/plain": [ 315 | "interactive(children=(Dropdown(description='x', options={'台北': 1, '台中': 2, '高雄': 3}, value=1), Output()), _dom…" 316 | ] 317 | }, 318 | "metadata": {}, 319 | "output_type": "display_data" 320 | }, 321 | { 322 | "data": { 323 | "text/plain": [ 324 | "" 325 | ] 326 | }, 327 | "execution_count": 30, 328 | "metadata": {}, 329 | "output_type": "execute_result" 330 | } 331 | ], 332 | "source": [ 333 | "interact(f, x = {\"台北\":1, \"台中\":2, \"高雄\":3})" 334 | ] 335 | }, 336 | { 337 | "cell_type": "markdown", 338 | "metadata": {}, 339 | "source": [ 340 | "### 範例" 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": 34, 346 | "metadata": {}, 347 | "outputs": [], 348 | "source": [ 349 | "def move(n=1):\n", 350 | " print(\" \"*n + \"⸜(* ॑꒳ ॑* )⸝\")" 351 | ] 352 | }, 353 | { 354 | "cell_type": "markdown", 355 | "metadata": {}, 356 | "source": [ 357 | "注意 Python 的函式怎麼給預設值。" 358 | ] 359 | }, 360 | { 361 | "cell_type": "code", 362 | "execution_count": 33, 363 | "metadata": {}, 364 | "outputs": [ 365 | { 366 | "data": { 367 | "application/vnd.jupyter.widget-view+json": { 368 | "model_id": "3d79d1a4c9aa40428d6ac093d6c7e397", 369 | "version_major": 2, 370 | "version_minor": 0 371 | }, 372 | "text/plain": [ 373 | "interactive(children=(IntSlider(value=1, description='n', max=80, min=1), Output()), _dom_classes=('widget-int…" 374 | ] 375 | }, 376 | "metadata": {}, 377 | "output_type": "display_data" 378 | } 379 | ], 380 | "source": [ 381 | "interact(move, n=(1, 80));" 382 | ] 383 | }, 384 | { 385 | "cell_type": "markdown", 386 | "metadata": {}, 387 | "source": [ 388 | "### 畫圖的範例" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 36, 394 | "metadata": {}, 395 | "outputs": [], 396 | "source": [ 397 | "x = np.linspace(-10, 10, 500)\n", 398 | "\n", 399 | "def myplot(n=1):\n", 400 | " y = np.sinc(n*x)\n", 401 | " plt.plot(x, y)" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": 37, 407 | "metadata": {}, 408 | "outputs": [ 409 | { 410 | "data": { 411 | "application/vnd.jupyter.widget-view+json": { 412 | "model_id": "b7d0da6b124642ecb930528347f81f69", 413 | "version_major": 2, 414 | "version_minor": 0 415 | }, 416 | "text/plain": [ 417 | "interactive(children=(FloatSlider(value=1.0, description='n', max=10.0, min=1.0), Output()), _dom_classes=('wi…" 418 | ] 419 | }, 420 | "metadata": {}, 421 | "output_type": "display_data" 422 | }, 423 | { 424 | "data": { 425 | "text/plain": [ 426 | "" 427 | ] 428 | }, 429 | "execution_count": 37, 430 | "metadata": {}, 431 | "output_type": "execute_result" 432 | } 433 | ], 434 | "source": [ 435 | "interact(myplot, n=(1., 10.))" 436 | ] 437 | }, 438 | { 439 | "cell_type": "markdown", 440 | "metadata": {}, 441 | "source": [ 442 | "### 按鈕之後才互動\n", 443 | "\n", 444 | "我們也可以不要那麼及時, 而是「按鈕之後」才執行。這時可以用 `interact_manual`, 用法基本上和 `interact` 一樣!" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 38, 450 | "metadata": {}, 451 | "outputs": [], 452 | "source": [ 453 | "from ipywidgets import interact_manual" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 39, 459 | "metadata": {}, 460 | "outputs": [], 461 | "source": [ 462 | "from time import sleep\n", 463 | "from IPython.display import clear_output" 464 | ] 465 | }, 466 | { 467 | "cell_type": "markdown", 468 | "metadata": {}, 469 | "source": [ 470 | "這裡我們用了兩個套件中的兩個函式, 其中:\n", 471 | " \n", 472 | "* `sleep(n)`: 要 Python 暫停 n 秒。\n", 473 | "* `clear_output()`: 清除 Jupyter Notebook 當前 cell 的東西。" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "execution_count": 42, 479 | "metadata": {}, 480 | "outputs": [], 481 | "source": [ 482 | "def pipi(message):\n", 483 | " print(\"拍拍\")\n", 484 | " sleep(2)\n", 485 | " clear_output()" 486 | ] 487 | }, 488 | { 489 | "cell_type": "code", 490 | "execution_count": 43, 491 | "metadata": {}, 492 | "outputs": [ 493 | { 494 | "data": { 495 | "application/vnd.jupyter.widget-view+json": { 496 | "model_id": "c95508f361324edb9b338e531db506de", 497 | "version_major": 2, 498 | "version_minor": 0 499 | }, 500 | "text/plain": [ 501 | "interactive(children=(Text(value='請輸入你要說的話', description='message'), Button(description='Run Interact', style=…" 502 | ] 503 | }, 504 | "metadata": {}, 505 | "output_type": "display_data" 506 | } 507 | ], 508 | "source": [ 509 | "interact_manual(pipi, message=\"請輸入你要說的話\");" 510 | ] 511 | }, 512 | { 513 | "cell_type": "markdown", 514 | "metadata": {}, 515 | "source": [ 516 | "![map 和 filter](images/T_map_and_filter.png)" 517 | ] 518 | }, 519 | { 520 | "cell_type": "markdown", 521 | "metadata": {}, 522 | "source": [ 523 | "## `map` 篇\n", 524 | "\n", 525 | "還記得之前我們要把一個串列裡的法郎, 一次全換算成台幣, 需要用迴圈一個個做嗎? 現在我們學會函式的寫法, 可以先寫一個「匯率換算」的程式, 然後用 `map` 一次把串列中所有的錢換算好!" 526 | ] 527 | }, 528 | { 529 | "cell_type": "markdown", 530 | "metadata": {}, 531 | "source": [ 532 | "### 問題\n", 533 | "\n", 534 | "這次我們到了澳洲, 又是要買三件物品, 價格分別是\n", 535 | "\n", 536 | " 200, 450, 35\n", 537 | " \n", 538 | "澳幣。我們又 Google 了一下, 發現 1 澳幣合台幣 21.8686884 元, 於是我們寫個函數來換算一下。" 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "execution_count": 2, 544 | "metadata": {}, 545 | "outputs": [], 546 | "source": [ 547 | "c = 21.8686884\n", 548 | "\n", 549 | "def aud2twd(m):\n", 550 | " return c*m" 551 | ] 552 | }, 553 | { 554 | "cell_type": "markdown", 555 | "metadata": {}, 556 | "source": [ 557 | "我們來試用一下, 假設我們想知道 100 澳幣合台幣多少。" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 4, 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "data": { 567 | "text/plain": [ 568 | "2186.86884" 569 | ] 570 | }, 571 | "execution_count": 4, 572 | "metadata": {}, 573 | "output_type": "execute_result" 574 | } 575 | ], 576 | "source": [ 577 | "aud2twd(100)" 578 | ] 579 | }, 580 | { 581 | "cell_type": "markdown", 582 | "metadata": {}, 583 | "source": [ 584 | "現在來換算我們三個物品合台幣多少。" 585 | ] 586 | }, 587 | { 588 | "cell_type": "code", 589 | "execution_count": 5, 590 | "metadata": {}, 591 | "outputs": [], 592 | "source": [ 593 | "price = [200, 450, 35]" 594 | ] 595 | }, 596 | { 597 | "cell_type": "markdown", 598 | "metadata": {}, 599 | "source": [ 600 | "![map 使用方式](images/map.png)" 601 | ] 602 | }, 603 | { 604 | "cell_type": "code", 605 | "execution_count": 6, 606 | "metadata": {}, 607 | "outputs": [ 608 | { 609 | "data": { 610 | "text/plain": [ 611 | "" 612 | ] 613 | }, 614 | "execution_count": 6, 615 | "metadata": {}, 616 | "output_type": "execute_result" 617 | } 618 | ], 619 | "source": [ 620 | "map(aud2twd, price)" 621 | ] 622 | }, 623 | { 624 | "cell_type": "markdown", 625 | "metadata": {}, 626 | "source": [ 627 | "耶? 這什麼意思? 原來我們真的要「看到」, 需要把這個 `map` 用串列表現出來。" 628 | ] 629 | }, 630 | { 631 | "cell_type": "code", 632 | "execution_count": 7, 633 | "metadata": {}, 634 | "outputs": [ 635 | { 636 | "data": { 637 | "text/plain": [ 638 | "[4373.73768, 9840.90978, 765.404094]" 639 | ] 640 | }, 641 | "execution_count": 7, 642 | "metadata": {}, 643 | "output_type": "execute_result" 644 | } 645 | ], 646 | "source": [ 647 | "list(map(aud2twd, price))" 648 | ] 649 | }, 650 | { 651 | "cell_type": "markdown", 652 | "metadata": {}, 653 | "source": [ 654 | "## `lambda`: 臨時要使用的函數\n", 655 | "\n", 656 | "`map` 看來挺方便的, 不過還是有一個問題, 需要特別去定義一個函數。這麼簡單的函數, 難道不能臨時定義一個嗎? 答案是肯定的, `lambda` 就是為臨時要用的函數而生。定義方式非常簡單:\n", 657 | "\n", 658 | "![lambda 的使用](images/lambda.png)\n", 659 | "\n", 660 | "\n" 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "execution_count": 8, 666 | "metadata": {}, 667 | "outputs": [ 668 | { 669 | "data": { 670 | "text/plain": [ 671 | "[4373.73768, 9840.90978, 765.404094]" 672 | ] 673 | }, 674 | "execution_count": 8, 675 | "metadata": {}, 676 | "output_type": "execute_result" 677 | } 678 | ], 679 | "source": [ 680 | "list(map(lambda x:c*x, price))" 681 | ] 682 | }, 683 | { 684 | "cell_type": "markdown", 685 | "metadata": {}, 686 | "source": [ 687 | "果然一次成功!" 688 | ] 689 | }, 690 | { 691 | "cell_type": "markdown", 692 | "metadata": {}, 693 | "source": [ 694 | "## `filter` 篇\n", 695 | "\n", 696 | "現在我們來討論 `filter`。要使用 `filter`, 我們需要定義一個輸出為布林值 (Ture/Flase) 的函數。然後用 filter 過濾, 如果一個串列中符合這個要求 (True) 的就留下來, 否則就去掉。\n", 697 | "\n", 698 | "![filter](images/filter.png)" 699 | ] 700 | }, 701 | { 702 | "cell_type": "markdown", 703 | "metadata": {}, 704 | "source": [ 705 | "### 問題\n", 706 | "\n", 707 | "在一個 1-20 的串列中, 我們想找出其中為偶數的數字。" 708 | ] 709 | }, 710 | { 711 | "cell_type": "code", 712 | "execution_count": 10, 713 | "metadata": {}, 714 | "outputs": [], 715 | "source": [ 716 | "def isEven(n):\n", 717 | " if n%2 == 0:\n", 718 | " return True\n", 719 | " else:\n", 720 | " return False" 721 | ] 722 | }, 723 | { 724 | "cell_type": "markdown", 725 | "metadata": {}, 726 | "source": [ 727 | "這樣可以檢查是不是偶數, 我們來試試!" 728 | ] 729 | }, 730 | { 731 | "cell_type": "code", 732 | "execution_count": 11, 733 | "metadata": {}, 734 | "outputs": [ 735 | { 736 | "data": { 737 | "text/plain": [ 738 | "True" 739 | ] 740 | }, 741 | "execution_count": 11, 742 | "metadata": {}, 743 | "output_type": "execute_result" 744 | } 745 | ], 746 | "source": [ 747 | "isEven(94)" 748 | ] 749 | }, 750 | { 751 | "cell_type": "code", 752 | "execution_count": 12, 753 | "metadata": {}, 754 | "outputs": [ 755 | { 756 | "data": { 757 | "text/plain": [ 758 | "False" 759 | ] 760 | }, 761 | "execution_count": 12, 762 | "metadata": {}, 763 | "output_type": "execute_result" 764 | } 765 | ], 766 | "source": [ 767 | "isEven(87)" 768 | ] 769 | }, 770 | { 771 | "cell_type": "markdown", 772 | "metadata": {}, 773 | "source": [ 774 | "濾出偶數!" 775 | ] 776 | }, 777 | { 778 | "cell_type": "code", 779 | "execution_count": 13, 780 | "metadata": {}, 781 | "outputs": [], 782 | "source": [ 783 | "L = range(1, 21)" 784 | ] 785 | }, 786 | { 787 | "cell_type": "code", 788 | "execution_count": 14, 789 | "metadata": {}, 790 | "outputs": [ 791 | { 792 | "data": { 793 | "text/plain": [ 794 | "range(1, 21)" 795 | ] 796 | }, 797 | "execution_count": 14, 798 | "metadata": {}, 799 | "output_type": "execute_result" 800 | } 801 | ], 802 | "source": [ 803 | "L" 804 | ] 805 | }, 806 | { 807 | "cell_type": "code", 808 | "execution_count": 15, 809 | "metadata": {}, 810 | "outputs": [ 811 | { 812 | "data": { 813 | "text/plain": [ 814 | "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]" 815 | ] 816 | }, 817 | "execution_count": 15, 818 | "metadata": {}, 819 | "output_type": "execute_result" 820 | } 821 | ], 822 | "source": [ 823 | "list(filter(isEven, L))" 824 | ] 825 | }, 826 | { 827 | "cell_type": "markdown", 828 | "metadata": {}, 829 | "source": [ 830 | "果然成功了!\n", 831 | "\n", 832 | "我們可能用 `lambda` 做這件事嗎?" 833 | ] 834 | }, 835 | { 836 | "cell_type": "code", 837 | "execution_count": 16, 838 | "metadata": {}, 839 | "outputs": [ 840 | { 841 | "data": { 842 | "text/plain": [ 843 | "[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]" 844 | ] 845 | }, 846 | "execution_count": 16, 847 | "metadata": {}, 848 | "output_type": "execute_result" 849 | } 850 | ], 851 | "source": [ 852 | "list(filter(lambda x:x%2==0, L))" 853 | ] 854 | }, 855 | { 856 | "cell_type": "markdown", 857 | "metadata": {}, 858 | "source": [ 859 | "一樣可以!!" 860 | ] 861 | } 862 | ], 863 | "metadata": { 864 | "kernelspec": { 865 | "display_name": "Python 3", 866 | "language": "python", 867 | "name": "python3" 868 | }, 869 | "language_info": { 870 | "codemirror_mode": { 871 | "name": "ipython", 872 | "version": 3 873 | }, 874 | "file_extension": ".py", 875 | "mimetype": "text/x-python", 876 | "name": "python", 877 | "nbconvert_exporter": "python", 878 | "pygments_lexer": "ipython3", 879 | "version": "3.7.3" 880 | } 881 | }, 882 | "nbformat": 4, 883 | "nbformat_minor": 2 884 | } 885 | -------------------------------------------------------------------------------- /07. APCS 精進計畫 I.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![APCS I](images/T_apcs01.png)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "## APCS 相關介紹\n", 15 | "\n", 16 | "APCS 測驗分成兩個部份:\n", 17 | "\n", 18 | "* 觀念題\n", 19 | "* 實作題\n", 20 | "\n", 21 | "實作題就是真的寫程式! 我們可以看一下歷年來的題目:\n", 22 | "\n", 23 | "[【APCS 歷屆試題】](https://apcs.csie.ntnu.edu.tw/index.php/questionstypes/previousexam/)\n", 24 | "\n", 25 | "然後點「程式設計實作題」。\n", 26 | "\n", 27 | "看完題目, 再開始用 IDLE 等寫程式, 等認為沒問題就依指定檔案名上傳 (如 `P1.py` 等等)。過程中程式可以先存在 backup 資料夾中。詳細流程可參考\n", 28 | "\n", 29 | " [【APCS 檢測作答系統說明】](https://apcs.csie.ntnu.edu.tw/index.php/info/systemdescription/)\n", 30 | " \n", 31 | " 整個 APCS 的問題風格很接近\n", 32 | " \n", 33 | " [UVa Online Judge](https://uva.onlinejudge.org)\n", 34 | " \n", 35 | " 可以自行在 UVa 網站練習 (不過 UVa 的要求比較多)。" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "## 暖身" 43 | ] 44 | }, 45 | { 46 | "cell_type": "markdown", 47 | "metadata": {}, 48 | "source": [ 49 | "### 1. 整理一下字串\n", 50 | "\n", 51 | "這是我們常會碰到的狀況, 原本有個字串\n", 52 | "\n", 53 | " \" abc 23 1 75 33 \"\n", 54 | " \n", 55 | "等等, 我們要整理 (輸出) 成\n", 56 | "\n", 57 | " \"abc 23 1 75 33\"" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "#### 試驗 1" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 2, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "st = \" abc 23 1 75 33 \"" 74 | ] 75 | }, 76 | { 77 | "cell_type": "code", 78 | "execution_count": 5, 79 | "metadata": {}, 80 | "outputs": [], 81 | "source": [ 82 | "L = st.split(' ')" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "多切出來的地方移除。" 90 | ] 91 | }, 92 | { 93 | "cell_type": "code", 94 | "execution_count": 7, 95 | "metadata": {}, 96 | "outputs": [], 97 | "source": [ 98 | "M = []\n", 99 | "for item in L:\n", 100 | " if item != '':\n", 101 | " M.append(item)" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 8, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "['abc', '23', '1', '75', '33']" 113 | ] 114 | }, 115 | "execution_count": 8, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "M" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": 9, 127 | "metadata": {}, 128 | "outputs": [ 129 | { 130 | "data": { 131 | "text/plain": [ 132 | "'abc 23 1 75 33'" 133 | ] 134 | }, 135 | "execution_count": 9, 136 | "metadata": {}, 137 | "output_type": "execute_result" 138 | } 139 | ], 140 | "source": [ 141 | "' '.join(M)" 142 | ] 143 | }, 144 | { 145 | "cell_type": "markdown", 146 | "metadata": {}, 147 | "source": [ 148 | "真的成功了!\n", 149 | "\n", 150 | "#### 試驗 2\n", 151 | "\n", 152 | "`filter` 是過濾出我們要的東西, 中間需要一個函式。我們使用 Python 緊急定義函式的 `lambda`。\n", 153 | "\n", 154 | "如果忘了怎麼使用, 可參考之前上課的內容。" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 12, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "['abc', '23', '1', '75', '33']" 166 | ] 167 | }, 168 | "execution_count": 12, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "list(filter(lambda x:x != '', L))" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "這看來好像很簡潔很嚇人, 不過其實一般正確解是..." 182 | ] 183 | }, 184 | { 185 | "cell_type": "markdown", 186 | "metadata": {}, 187 | "source": [ 188 | "#### 試驗 3 (正解)" 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": 13, 194 | "metadata": {}, 195 | "outputs": [ 196 | { 197 | "data": { 198 | "text/plain": [ 199 | "['abc', '23', '1', '75', '33']" 200 | ] 201 | }, 202 | "execution_count": 13, 203 | "metadata": {}, 204 | "output_type": "execute_result" 205 | } 206 | ], 207 | "source": [ 208 | "st.split()" 209 | ] 210 | }, 211 | { 212 | "cell_type": "markdown", 213 | "metadata": {}, 214 | "source": [ 215 | "### 2. 布林值運算\n", 216 | "\n", 217 | "Python 有兩套系的 `and`, `or`...\n", 218 | "\n", 219 | "* AND: `and`, `&`\n", 220 | "* OR: `or`, `|`\n", 221 | "* XOR: `^`" 222 | ] 223 | }, 224 | { 225 | "cell_type": "markdown", 226 | "metadata": {}, 227 | "source": [ 228 | "#### AND" 229 | ] 230 | }, 231 | { 232 | "cell_type": "code", 233 | "execution_count": 17, 234 | "metadata": {}, 235 | "outputs": [ 236 | { 237 | "data": { 238 | "text/plain": [ 239 | "1" 240 | ] 241 | }, 242 | "execution_count": 17, 243 | "metadata": {}, 244 | "output_type": "execute_result" 245 | } 246 | ], 247 | "source": [ 248 | "1 and 1" 249 | ] 250 | }, 251 | { 252 | "cell_type": "code", 253 | "execution_count": 18, 254 | "metadata": {}, 255 | "outputs": [ 256 | { 257 | "data": { 258 | "text/plain": [ 259 | "1" 260 | ] 261 | }, 262 | "execution_count": 18, 263 | "metadata": {}, 264 | "output_type": "execute_result" 265 | } 266 | ], 267 | "source": [ 268 | "1 & 1" 269 | ] 270 | }, 271 | { 272 | "cell_type": "markdown", 273 | "metadata": {}, 274 | "source": [ 275 | "`and` 和 `&` 看來好像沒有差啊? 到底有什麼差我們後會會說。\n", 276 | "\n", 277 | "#### OR" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 19, 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "data": { 287 | "text/plain": [ 288 | "1" 289 | ] 290 | }, 291 | "execution_count": 19, 292 | "metadata": {}, 293 | "output_type": "execute_result" 294 | } 295 | ], 296 | "source": [ 297 | "1 or 0" 298 | ] 299 | }, 300 | { 301 | "cell_type": "code", 302 | "execution_count": 20, 303 | "metadata": {}, 304 | "outputs": [ 305 | { 306 | "data": { 307 | "text/plain": [ 308 | "1" 309 | ] 310 | }, 311 | "execution_count": 20, 312 | "metadata": {}, 313 | "output_type": "execute_result" 314 | } 315 | ], 316 | "source": [ 317 | "1 | 0" 318 | ] 319 | }, 320 | { 321 | "cell_type": "markdown", 322 | "metadata": {}, 323 | "source": [ 324 | "#### XOR" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 21, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "data": { 334 | "text/plain": [ 335 | "0" 336 | ] 337 | }, 338 | "execution_count": 21, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [ 344 | "1 ^ 1" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 22, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "data": { 354 | "text/plain": [ 355 | "1" 356 | ] 357 | }, 358 | "execution_count": 22, 359 | "metadata": {}, 360 | "output_type": "execute_result" 361 | } 362 | ], 363 | "source": [ 364 | "1 ^ 0" 365 | ] 366 | }, 367 | { 368 | "cell_type": "markdown", 369 | "metadata": {}, 370 | "source": [ 371 | "### 3. 二進位和十進位互轉\n", 372 | "\n", 373 | "* `bin`\n", 374 | "* `int`" 375 | ] 376 | }, 377 | { 378 | "cell_type": "markdown", 379 | "metadata": {}, 380 | "source": [ 381 | "#### 10 進位換 2 進位" 382 | ] 383 | }, 384 | { 385 | "cell_type": "code", 386 | "execution_count": 125, 387 | "metadata": {}, 388 | "outputs": [], 389 | "source": [ 390 | "a = bin(8)" 391 | ] 392 | }, 393 | { 394 | "cell_type": "code", 395 | "execution_count": 126, 396 | "metadata": {}, 397 | "outputs": [], 398 | "source": [ 399 | "b = bin(23)" 400 | ] 401 | }, 402 | { 403 | "cell_type": "code", 404 | "execution_count": 127, 405 | "metadata": {}, 406 | "outputs": [ 407 | { 408 | "data": { 409 | "text/plain": [ 410 | "'0b1000'" 411 | ] 412 | }, 413 | "execution_count": 127, 414 | "metadata": {}, 415 | "output_type": "execute_result" 416 | } 417 | ], 418 | "source": [ 419 | "a" 420 | ] 421 | }, 422 | { 423 | "cell_type": "code", 424 | "execution_count": 128, 425 | "metadata": {}, 426 | "outputs": [ 427 | { 428 | "data": { 429 | "text/plain": [ 430 | "'0b10111'" 431 | ] 432 | }, 433 | "execution_count": 128, 434 | "metadata": {}, 435 | "output_type": "execute_result" 436 | } 437 | ], 438 | "source": [ 439 | "b" 440 | ] 441 | }, 442 | { 443 | "cell_type": "markdown", 444 | "metadata": {}, 445 | "source": [ 446 | "原來 Python 表示一個 2 進位數是前面會加上 `0b`。我們可能會想, 這可以加嗎? 於是..." 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 129, 452 | "metadata": {}, 453 | "outputs": [ 454 | { 455 | "data": { 456 | "text/plain": [ 457 | "'0b10000b10111'" 458 | ] 459 | }, 460 | "execution_count": 129, 461 | "metadata": {}, 462 | "output_type": "execute_result" 463 | } 464 | ], 465 | "source": [ 466 | "a+b" 467 | ] 468 | }, 469 | { 470 | "cell_type": "markdown", 471 | "metadata": {}, 472 | "source": [ 473 | "沒有錯誤耶! 但是, 認真看它是把兩個字串合起來!!\n", 474 | "\n", 475 | "難道, Python 真的只把這兩個當字串?" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": 130, 481 | "metadata": {}, 482 | "outputs": [ 483 | { 484 | "data": { 485 | "text/plain": [ 486 | "str" 487 | ] 488 | }, 489 | "execution_count": 130, 490 | "metadata": {}, 491 | "output_type": "execute_result" 492 | } 493 | ], 494 | "source": [ 495 | "type(a)" 496 | ] 497 | }, 498 | { 499 | "cell_type": "markdown", 500 | "metadata": {}, 501 | "source": [ 502 | "結果還真的... 我們只好自己來...\n", 503 | "\n", 504 | "先把二進位換十進位!" 505 | ] 506 | }, 507 | { 508 | "cell_type": "code", 509 | "execution_count": 35, 510 | "metadata": {}, 511 | "outputs": [ 512 | { 513 | "data": { 514 | "text/plain": [ 515 | "8" 516 | ] 517 | }, 518 | "execution_count": 35, 519 | "metadata": {}, 520 | "output_type": "execute_result" 521 | } 522 | ], 523 | "source": [ 524 | "int(a, 2)" 525 | ] 526 | }, 527 | { 528 | "cell_type": "code", 529 | "execution_count": 36, 530 | "metadata": {}, 531 | "outputs": [ 532 | { 533 | "data": { 534 | "text/plain": [ 535 | "23" 536 | ] 537 | }, 538 | "execution_count": 36, 539 | "metadata": {}, 540 | "output_type": "execute_result" 541 | } 542 | ], 543 | "source": [ 544 | "int(b, 2)" 545 | ] 546 | }, 547 | { 548 | "cell_type": "markdown", 549 | "metadata": {}, 550 | "source": [ 551 | "十進位的數字加起來, 再換成 2 進位就可以了!" 552 | ] 553 | }, 554 | { 555 | "cell_type": "code", 556 | "execution_count": 37, 557 | "metadata": {}, 558 | "outputs": [ 559 | { 560 | "data": { 561 | "text/plain": [ 562 | "'0b11111'" 563 | ] 564 | }, 565 | "execution_count": 37, 566 | "metadata": {}, 567 | "output_type": "execute_result" 568 | } 569 | ], 570 | "source": [ 571 | "bin(int(a,2) + int(b,2))" 572 | ] 573 | }, 574 | { 575 | "cell_type": "markdown", 576 | "metadata": {}, 577 | "source": [ 578 | "16 進位換成 10 進位用法和 2 進位一樣!" 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "execution_count": 131, 584 | "metadata": {}, 585 | "outputs": [ 586 | { 587 | "data": { 588 | "text/plain": [ 589 | "65535" 590 | ] 591 | }, 592 | "execution_count": 131, 593 | "metadata": {}, 594 | "output_type": "execute_result" 595 | } 596 | ], 597 | "source": [ 598 | "int('FFFF', 16)" 599 | ] 600 | }, 601 | { 602 | "cell_type": "markdown", 603 | "metadata": {}, 604 | "source": [ 605 | "10 進位換 16 進位。" 606 | ] 607 | }, 608 | { 609 | "cell_type": "code", 610 | "execution_count": 132, 611 | "metadata": {}, 612 | "outputs": [ 613 | { 614 | "data": { 615 | "text/plain": [ 616 | "'0xffff'" 617 | ] 618 | }, 619 | "execution_count": 132, 620 | "metadata": {}, 621 | "output_type": "execute_result" 622 | } 623 | ], 624 | "source": [ 625 | "hex(65535)" 626 | ] 627 | }, 628 | { 629 | "cell_type": "markdown", 630 | "metadata": {}, 631 | "source": [ 632 | "有 `0b`, `0x` 和沒有的差別是, 在換成 10 進位數時, 我們只要指名是 0, Python 就知道要以 0b, 0x 要求改。" 633 | ] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "execution_count": 42, 638 | "metadata": {}, 639 | "outputs": [ 640 | { 641 | "data": { 642 | "text/plain": [ 643 | "65535" 644 | ] 645 | }, 646 | "execution_count": 42, 647 | "metadata": {}, 648 | "output_type": "execute_result" 649 | } 650 | ], 651 | "source": [ 652 | "int('0xFFFF', 0)" 653 | ] 654 | }, 655 | { 656 | "cell_type": "code", 657 | "execution_count": 43, 658 | "metadata": {}, 659 | "outputs": [ 660 | { 661 | "data": { 662 | "text/plain": [ 663 | "65535" 664 | ] 665 | }, 666 | "execution_count": 43, 667 | "metadata": {}, 668 | "output_type": "execute_result" 669 | } 670 | ], 671 | "source": [ 672 | "int('0xFFFF', 16)" 673 | ] 674 | }, 675 | { 676 | "attachments": {}, 677 | "cell_type": "markdown", 678 | "metadata": {}, 679 | "source": [ 680 | "![APCS 練功系統](images/T_apcs_env.png)\n", 681 | "\n", 682 | "這裡我們來看看怎麼用 Jupyter Notebook、完全不離開來練習。\n", 683 | "\n", 684 | "### 問題: 二進位加法\n", 685 | "\n", 686 | "輸入兩個二進位數\n", 687 | "\n", 688 | " 10011 11100\n", 689 | " \n", 690 | "以二進位表示, 輸出相加的結果。\n", 691 | "\n", 692 | " 111 1101 10100" 693 | ] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "execution_count": 46, 698 | "metadata": {}, 699 | "outputs": [ 700 | { 701 | "name": "stdout", 702 | "output_type": "stream", 703 | "text": [ 704 | "111 1101\n", 705 | "111 1101 10100\n" 706 | ] 707 | } 708 | ], 709 | "source": [ 710 | "line = input()\n", 711 | "a, b = line.split()\n", 712 | "s = int(a, 2) + int(b, 2)\n", 713 | "print(a, b, bin(s)[2:])" 714 | ] 715 | }, 716 | { 717 | "cell_type": "markdown", 718 | "metadata": {}, 719 | "source": [ 720 | "### 問題: 二進位加法\n", 721 | "\n", 722 | "比較接近 APCS 的寫法, 可能會像這樣。\n", 723 | "\n", 724 | "首先, 第一個數字 n 代表一共要做幾組。\n", 725 | "\n", 726 | "接下來 n 行是兩個準備要加的 2 進位數。" 727 | ] 728 | }, 729 | { 730 | "cell_type": "code", 731 | "execution_count": 133, 732 | "metadata": {}, 733 | "outputs": [ 734 | { 735 | "name": "stdout", 736 | "output_type": "stream", 737 | "text": [ 738 | "3\n", 739 | "11111 10111\n", 740 | "11111 10111 110110\n", 741 | "10011 11101\n", 742 | "10011 11101 110000\n", 743 | "111110 1010101\n", 744 | "111110 1010101 10010011\n" 745 | ] 746 | } 747 | ], 748 | "source": [ 749 | "n = int(input())\n", 750 | "\n", 751 | "for i in range(n):\n", 752 | " line = input()\n", 753 | " a, b = line.split()\n", 754 | " s = int(a, 2) + int(b, 2)\n", 755 | " print(a, b, bin(s)[2:])" 756 | ] 757 | }, 758 | { 759 | "cell_type": "markdown", 760 | "metadata": {}, 761 | "source": [ 762 | "看起來很對, 我們就把程式存起來!" 763 | ] 764 | }, 765 | { 766 | "cell_type": "code", 767 | "execution_count": 134, 768 | "metadata": {}, 769 | "outputs": [ 770 | { 771 | "name": "stdout", 772 | "output_type": "stream", 773 | "text": [ 774 | "File `p1.py` exists. Overwrite (y/[N])? y\n", 775 | "The following commands were written to file `p1.py`:\n", 776 | "n = int(input())\n", 777 | "\n", 778 | "for i in range(n):\n", 779 | " line = input()\n", 780 | " a, b = line.split()\n", 781 | " s = int(a, 2) + int(b, 2)\n", 782 | " print(a, b, bin(s)[2:])\n" 783 | ] 784 | } 785 | ], 786 | "source": [ 787 | "%save \"p1.py\" 133" 788 | ] 789 | }, 790 | { 791 | "cell_type": "code", 792 | "execution_count": 49, 793 | "metadata": {}, 794 | "outputs": [ 795 | { 796 | "name": "stdout", 797 | "output_type": "stream", 798 | "text": [ 799 | "Writing test01.txt\n" 800 | ] 801 | } 802 | ], 803 | "source": [ 804 | "%%writefile \"test01.txt\"\n", 805 | "3\n", 806 | "1011 11101\n", 807 | "10111 111010\n", 808 | "1101 110001" 809 | ] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": 50, 814 | "metadata": {}, 815 | "outputs": [ 816 | { 817 | "name": "stdout", 818 | "output_type": "stream", 819 | "text": [ 820 | "1011 11101 101000\r\n", 821 | "10111 111010 1010001\r\n", 822 | "1101 110001 111110\r\n" 823 | ] 824 | } 825 | ], 826 | "source": [ 827 | "!python p1.py < test01.txt" 828 | ] 829 | }, 830 | { 831 | "cell_type": "code", 832 | "execution_count": 135, 833 | "metadata": {}, 834 | "outputs": [ 835 | { 836 | "ename": "SyntaxError", 837 | "evalue": "invalid character in identifier (, line 1)", 838 | "output_type": "error", 839 | "traceback": [ 840 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 我們就很放快再做一個練習的輸入檔。\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character in identifier\n" 841 | ] 842 | } 843 | ], 844 | "source": [ 845 | "我們就很放快再做一個練習的輸入檔。" 846 | ] 847 | }, 848 | { 849 | "cell_type": "code", 850 | "execution_count": 51, 851 | "metadata": {}, 852 | "outputs": [ 853 | { 854 | "name": "stdout", 855 | "output_type": "stream", 856 | "text": [ 857 | "Writing test02.txt\n" 858 | ] 859 | } 860 | ], 861 | "source": [ 862 | "%%writefile \"test02.txt\"\n", 863 | "7\n", 864 | "1100 11011\n", 865 | "11111 010\n", 866 | "1011011 10111\n", 867 | "1101 1110\n", 868 | "100011 111001\n", 869 | "111001 11010\n", 870 | "101010 1101111" 871 | ] 872 | }, 873 | { 874 | "cell_type": "code", 875 | "execution_count": 52, 876 | "metadata": {}, 877 | "outputs": [ 878 | { 879 | "name": "stdout", 880 | "output_type": "stream", 881 | "text": [ 882 | "1100 11011 100111\r\n", 883 | "11111 010 100001\r\n", 884 | "1011011 10111 1110010\r\n", 885 | "1101 1110 11011\r\n", 886 | "100011 111001 1011100\r\n", 887 | "111001 11010 1010011\r\n", 888 | "101010 1101111 10011001\r\n" 889 | ] 890 | } 891 | ], 892 | "source": [ 893 | "!python p1.py < test02.txt" 894 | ] 895 | }, 896 | { 897 | "cell_type": "markdown", 898 | "metadata": {}, 899 | "source": [ 900 | "### 問題: 高級的猜數字" 901 | ] 902 | }, 903 | { 904 | "cell_type": "markdown", 905 | "metadata": {}, 906 | "source": [ 907 | "你想一個 1-31 的數字, 然後電腦給你看五張卡片, 在選出有你想的數字卡片後, 電腦就會猜到你想的數字。" 908 | ] 909 | }, 910 | { 911 | "cell_type": "code", 912 | "execution_count": 136, 913 | "metadata": {}, 914 | "outputs": [ 915 | { 916 | "name": "stdout", 917 | "output_type": "stream", 918 | "text": [ 919 | "1 00001\n", 920 | "2 00010\n", 921 | "3 00011\n", 922 | "4 00100\n", 923 | "5 00101\n", 924 | "6 00110\n", 925 | "7 00111\n", 926 | "8 01000\n", 927 | "9 01001\n", 928 | "10 01010\n", 929 | "11 01011\n", 930 | "12 01100\n", 931 | "13 01101\n", 932 | "14 01110\n", 933 | "15 01111\n", 934 | "16 10000\n", 935 | "17 10001\n", 936 | "18 10010\n", 937 | "19 10011\n", 938 | "20 10100\n", 939 | "21 10101\n", 940 | "22 10110\n", 941 | "23 10111\n", 942 | "24 11000\n", 943 | "25 11001\n", 944 | "26 11010\n", 945 | "27 11011\n", 946 | "28 11100\n", 947 | "29 11101\n", 948 | "30 11110\n", 949 | "31 11111\n" 950 | ] 951 | } 952 | ], 953 | "source": [ 954 | "for n in range(1, 32):\n", 955 | " print(n, bin(n)[2:].zfill(5))" 956 | ] 957 | }, 958 | { 959 | "cell_type": "markdown", 960 | "metadata": {}, 961 | "source": [ 962 | "#### 補 0 篇之一" 963 | ] 964 | }, 965 | { 966 | "cell_type": "code", 967 | "execution_count": 137, 968 | "metadata": {}, 969 | "outputs": [ 970 | { 971 | "name": "stdout", 972 | "output_type": "stream", 973 | "text": [ 974 | "00101\n" 975 | ] 976 | } 977 | ], 978 | "source": [ 979 | "b = '101'\n", 980 | "\n", 981 | "o = '00000' + b\n", 982 | "o = o[-5:]\n", 983 | "print(o)" 984 | ] 985 | }, 986 | { 987 | "cell_type": "markdown", 988 | "metadata": {}, 989 | "source": [ 990 | "#### 補 0 篇之二" 991 | ] 992 | }, 993 | { 994 | "cell_type": "code", 995 | "execution_count": 138, 996 | "metadata": {}, 997 | "outputs": [ 998 | { 999 | "name": "stdout", 1000 | "output_type": "stream", 1001 | "text": [ 1002 | "00101\n" 1003 | ] 1004 | } 1005 | ], 1006 | "source": [ 1007 | "b = '101'\n", 1008 | "\n", 1009 | "o = f\"{b:0>5s}\"\n", 1010 | "print(o)" 1011 | ] 1012 | }, 1013 | { 1014 | "cell_type": "markdown", 1015 | "metadata": {}, 1016 | "source": [ 1017 | "#### 補 0 篇之三" 1018 | ] 1019 | }, 1020 | { 1021 | "cell_type": "code", 1022 | "execution_count": 139, 1023 | "metadata": {}, 1024 | "outputs": [ 1025 | { 1026 | "name": "stdout", 1027 | "output_type": "stream", 1028 | "text": [ 1029 | "00101\n" 1030 | ] 1031 | } 1032 | ], 1033 | "source": [ 1034 | "b = '101'\n", 1035 | "\n", 1036 | "o = b.zfill(5)\n", 1037 | "print(o)" 1038 | ] 1039 | }, 1040 | { 1041 | "cell_type": "code", 1042 | "execution_count": 70, 1043 | "metadata": {}, 1044 | "outputs": [ 1045 | { 1046 | "data": { 1047 | "text/plain": [ 1048 | "31" 1049 | ] 1050 | }, 1051 | "execution_count": 70, 1052 | "metadata": {}, 1053 | "output_type": "execute_result" 1054 | } 1055 | ], 1056 | "source": [ 1057 | "int('11111', 2)" 1058 | ] 1059 | }, 1060 | { 1061 | "cell_type": "code", 1062 | "execution_count": 71, 1063 | "metadata": {}, 1064 | "outputs": [], 1065 | "source": [ 1066 | "b = '10110'" 1067 | ] 1068 | }, 1069 | { 1070 | "cell_type": "code", 1071 | "execution_count": 94, 1072 | "metadata": {}, 1073 | "outputs": [], 1074 | "source": [ 1075 | "card = [[], [], [], [], []]" 1076 | ] 1077 | }, 1078 | { 1079 | "cell_type": "code", 1080 | "execution_count": 99, 1081 | "metadata": {}, 1082 | "outputs": [], 1083 | "source": [ 1084 | "card = [[], [], [], [], []]\n", 1085 | "\n", 1086 | "for n in range(1, 32):\n", 1087 | " b = bin(n)[2:].zfill(5)\n", 1088 | " for i, d in enumerate(b):\n", 1089 | " if int(d):\n", 1090 | " card[4-i].append(int(b, 2))" 1091 | ] 1092 | }, 1093 | { 1094 | "cell_type": "code", 1095 | "execution_count": 117, 1096 | "metadata": {}, 1097 | "outputs": [], 1098 | "source": [ 1099 | "def showcard(k):\n", 1100 | " part = 4\n", 1101 | " for i in range(4):\n", 1102 | " for d in card[k][i*4:(i+1)*4]:\n", 1103 | " print((' '+ str(d))[-2:], end=' ')\n", 1104 | " print()" 1105 | ] 1106 | }, 1107 | { 1108 | "cell_type": "code", 1109 | "execution_count": 140, 1110 | "metadata": {}, 1111 | "outputs": [ 1112 | { 1113 | "ename": "SyntaxError", 1114 | "evalue": "invalid syntax (, line 1)", 1115 | "output_type": "error", 1116 | "traceback": [ 1117 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 顯示 5 張卡片\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" 1118 | ] 1119 | } 1120 | ], 1121 | "source": [ 1122 | "顯示 5 張卡片" 1123 | ] 1124 | }, 1125 | { 1126 | "cell_type": "code", 1127 | "execution_count": 121, 1128 | "metadata": {}, 1129 | "outputs": [ 1130 | { 1131 | "name": "stdout", 1132 | "output_type": "stream", 1133 | "text": [ 1134 | " 1 3 5 7 \n", 1135 | " 9 11 13 15 \n", 1136 | "17 19 21 23 \n", 1137 | "25 27 29 31 \n", 1138 | "---\n", 1139 | " 2 3 6 7 \n", 1140 | "10 11 14 15 \n", 1141 | "18 19 22 23 \n", 1142 | "26 27 30 31 \n", 1143 | "---\n", 1144 | " 4 5 6 7 \n", 1145 | "12 13 14 15 \n", 1146 | "20 21 22 23 \n", 1147 | "28 29 30 31 \n", 1148 | "---\n", 1149 | " 8 9 10 11 \n", 1150 | "12 13 14 15 \n", 1151 | "24 25 26 27 \n", 1152 | "28 29 30 31 \n", 1153 | "---\n", 1154 | "16 17 18 19 \n", 1155 | "20 21 22 23 \n", 1156 | "24 25 26 27 \n", 1157 | "28 29 30 31 \n", 1158 | "---\n" 1159 | ] 1160 | } 1161 | ], 1162 | "source": [ 1163 | "for k in range(5):\n", 1164 | " showcard(k)\n", 1165 | " print(\"---\")" 1166 | ] 1167 | } 1168 | ], 1169 | "metadata": { 1170 | "kernelspec": { 1171 | "display_name": "Python 3", 1172 | "language": "python", 1173 | "name": "python3" 1174 | }, 1175 | "language_info": { 1176 | "codemirror_mode": { 1177 | "name": "ipython", 1178 | "version": 3 1179 | }, 1180 | "file_extension": ".py", 1181 | "mimetype": "text/x-python", 1182 | "name": "python", 1183 | "nbconvert_exporter": "python", 1184 | "pygments_lexer": "ipython3", 1185 | "version": "3.7.3" 1186 | } 1187 | }, 1188 | "nbformat": 4, 1189 | "nbformat_minor": 2 1190 | } 1191 | -------------------------------------------------------------------------------- /08. APCS 精進計畫 II.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![APCS 2](images/T_apcs02.png)" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### `map` 的複習\n", 15 | "\n", 16 | "要把一個函式用到一個 list 上每一個元素時, 我們可以用 `map` 指令。\n", 17 | "\n", 18 | "比如說, 下面的 list, 我們想要都變成整數型態。" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 24, 24 | "metadata": {}, 25 | "outputs": [], 26 | "source": [ 27 | "mylist = list('94871234')" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 25, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "data": { 37 | "text/plain": [ 38 | "['9', '4', '8', '7', '1', '2', '3', '4']" 39 | ] 40 | }, 41 | "execution_count": 25, 42 | "metadata": {}, 43 | "output_type": "execute_result" 44 | } 45 | ], 46 | "source": [ 47 | "mylist" 48 | ] 49 | }, 50 | { 51 | "cell_type": "markdown", 52 | "metadata": {}, 53 | "source": [ 54 | "就把 `int` 用到整個 list。" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 26, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "data": { 64 | "text/plain": [ 65 | "" 66 | ] 67 | }, 68 | "execution_count": 26, 69 | "metadata": {}, 70 | "output_type": "execute_result" 71 | } 72 | ], 73 | "source": [ 74 | "map(int, mylist)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "但這樣是抽象的 `map`, 想具體看到是用 `list` 換成具體的 list。" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 27, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "data": { 91 | "text/plain": [ 92 | "[9, 4, 8, 7, 1, 2, 3, 4]" 93 | ] 94 | }, 95 | "execution_count": 27, 96 | "metadata": {}, 97 | "output_type": "execute_result" 98 | } 99 | ], 100 | "source": [ 101 | "list(map(int, mylist))" 102 | ] 103 | }, 104 | { 105 | "cell_type": "markdown", 106 | "metadata": {}, 107 | "source": [ 108 | "### 複習: 救急函數 `lambda`\n", 109 | "\n", 110 | "`map` 可以用任何可作用到一個 list 上每個元素的函數。我們當然可以定義自己的函數。\n", 111 | "\n", 112 | "比方說我們想把一個 list 中的數字都平方..." 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 28, 118 | "metadata": {}, 119 | "outputs": [], 120 | "source": [ 121 | "mylist = [9, 4, 8, 7, 3, 5, 10]" 122 | ] 123 | }, 124 | { 125 | "cell_type": "markdown", 126 | "metadata": {}, 127 | "source": [ 128 | "我們要先訂一個平方函式。" 129 | ] 130 | }, 131 | { 132 | "cell_type": "code", 133 | "execution_count": 29, 134 | "metadata": {}, 135 | "outputs": [], 136 | "source": [ 137 | "def square(x):\n", 138 | " return x**2" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 32, 144 | "metadata": {}, 145 | "outputs": [], 146 | "source": [ 147 | "squared = map(square, mylist)" 148 | ] 149 | }, 150 | { 151 | "cell_type": "markdown", 152 | "metadata": {}, 153 | "source": [ 154 | "用 list 看一下。" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 33, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "[81, 16, 64, 49, 9, 25, 100]" 166 | ] 167 | }, 168 | "execution_count": 33, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "list(squared)" 175 | ] 176 | }, 177 | { 178 | "cell_type": "markdown", 179 | "metadata": {}, 180 | "source": [ 181 | "但這也太麻煩了!\n", 182 | "\n", 183 | "這種臨時要用的, 可以用 `lambda` 來定義這個函數。語法是:\n", 184 | "\n", 185 | " lambda 輸入:輸出\n", 186 | " \n", 187 | "比方說剛剛的平方動作..." 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 34, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "data": { 197 | "text/plain": [ 198 | "[81, 16, 64, 49, 9, 25, 100]" 199 | ] 200 | }, 201 | "execution_count": 34, 202 | "metadata": {}, 203 | "output_type": "execute_result" 204 | } 205 | ], 206 | "source": [ 207 | "list(map(lambda x:x**2, mylist))" 208 | ] 209 | }, 210 | { 211 | "cell_type": "markdown", 212 | "metadata": {}, 213 | "source": [ 214 | "### Python 超炫的 list comprehension\n", 215 | "\n", 216 | "Python 有個很炫的快速 list 產成方式, 叫 list comprehension。\n", 217 | "\n", 218 | "簡單的說, 很像我們數學集合表示法。比方說, 有個集合是:\n", 219 | "\n", 220 | "$$\\{ 2k+1 \\mid k \\in \\{0, 1, 2, 3, 4, 5\\} \\}$$\n", 221 | "\n", 222 | "就是前 6 個奇數:\n", 223 | "\n", 224 | "$$\\{1, 3, 5, 7, 9, 11 \\}$$\n", 225 | "\n", 226 | "在 Python 可以這麼做..." 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": 35, 232 | "metadata": {}, 233 | "outputs": [ 234 | { 235 | "data": { 236 | "text/plain": [ 237 | "[1, 3, 5, 7, 9]" 238 | ] 239 | }, 240 | "execution_count": 35, 241 | "metadata": {}, 242 | "output_type": "execute_result" 243 | } 244 | ], 245 | "source": [ 246 | "[2*k+1 for k in range(5)]" 247 | ] 248 | }, 249 | { 250 | "cell_type": "markdown", 251 | "metadata": {}, 252 | "source": [ 253 | "是不是超級酷!\n", 254 | "\n", 255 | "我們也可做相當複雜的事情。比如說現在我們想把一個字串:\n", 256 | "\n", 257 | " st = 'abxCDefGHLmk'\n", 258 | "\n", 259 | "轉成\n", 260 | "\n", 261 | " binary_str = '000110011100'\n", 262 | " \n", 263 | "也就是小寫就記為 0, 大寫記為 1。" 264 | ] 265 | }, 266 | { 267 | "cell_type": "code", 268 | "execution_count": 36, 269 | "metadata": {}, 270 | "outputs": [], 271 | "source": [ 272 | "st = 'abxCDefGHLmk'" 273 | ] 274 | }, 275 | { 276 | "cell_type": "code", 277 | "execution_count": 37, 278 | "metadata": {}, 279 | "outputs": [ 280 | { 281 | "data": { 282 | "text/plain": [ 283 | "[False, False, False, True, True, False, False, True, True, True, False, False]" 284 | ] 285 | }, 286 | "execution_count": 37, 287 | "metadata": {}, 288 | "output_type": "execute_result" 289 | } 290 | ], 291 | "source": [ 292 | "[x.isupper() for x in st]" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": {}, 298 | "source": [ 299 | "有... 一點... 酷, 那就是... 出現一個 list, 小寫是 Flase, 大寫是 True...\n", 300 | "\n", 301 | "我們是要 0 和 1...\n", 302 | "\n", 303 | "但我們看一下兩個判斷式。" 304 | ] 305 | }, 306 | { 307 | "cell_type": "code", 308 | "execution_count": 43, 309 | "metadata": {}, 310 | "outputs": [ 311 | { 312 | "data": { 313 | "text/plain": [ 314 | "True" 315 | ] 316 | }, 317 | "execution_count": 43, 318 | "metadata": {}, 319 | "output_type": "execute_result" 320 | } 321 | ], 322 | "source": [ 323 | "True == 1" 324 | ] 325 | }, 326 | { 327 | "cell_type": "code", 328 | "execution_count": 44, 329 | "metadata": {}, 330 | "outputs": [ 331 | { 332 | "data": { 333 | "text/plain": [ 334 | "True" 335 | ] 336 | }, 337 | "execution_count": 44, 338 | "metadata": {}, 339 | "output_type": "execute_result" 340 | } 341 | ], 342 | "source": [ 343 | "False == 0" 344 | ] 345 | }, 346 | { 347 | "cell_type": "markdown", 348 | "metadata": {}, 349 | "source": [ 350 | "這其實就是 0, 1 耶, 這時只要把這 True, False 用 `int` 轉換就可以!" 351 | ] 352 | }, 353 | { 354 | "cell_type": "code", 355 | "execution_count": 45, 356 | "metadata": {}, 357 | "outputs": [ 358 | { 359 | "data": { 360 | "text/plain": [ 361 | "[0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0]" 362 | ] 363 | }, 364 | "execution_count": 45, 365 | "metadata": {}, 366 | "output_type": "execute_result" 367 | } 368 | ], 369 | "source": [ 370 | "[int(x.isupper()) for x in st]" 371 | ] 372 | }, 373 | { 374 | "cell_type": "markdown", 375 | "metadata": {}, 376 | "source": [ 377 | "是不是很炫? 但我們要的是字串, 不要緊、再來..." 378 | ] 379 | }, 380 | { 381 | "cell_type": "code", 382 | "execution_count": 46, 383 | "metadata": {}, 384 | "outputs": [ 385 | { 386 | "data": { 387 | "text/plain": [ 388 | "['0', '0', '0', '1', '1', '0', '0', '1', '1', '1', '0', '0']" 389 | ] 390 | }, 391 | "execution_count": 46, 392 | "metadata": {}, 393 | "output_type": "execute_result" 394 | } 395 | ], 396 | "source": [ 397 | "[str(int(x.isupper())) for x in st]" 398 | ] 399 | }, 400 | { 401 | "cell_type": "markdown", 402 | "metadata": {}, 403 | "source": [ 404 | "把這個 list 再合成字串可以嗎? 可以哦, 用 `join`..." 405 | ] 406 | }, 407 | { 408 | "cell_type": "code", 409 | "execution_count": 47, 410 | "metadata": {}, 411 | "outputs": [ 412 | { 413 | "data": { 414 | "text/plain": [ 415 | "'000110011100'" 416 | ] 417 | }, 418 | "execution_count": 47, 419 | "metadata": {}, 420 | "output_type": "execute_result" 421 | } 422 | ], 423 | "source": [ 424 | "''.join([str(int(x.isupper())) for x in st])" 425 | ] 426 | }, 427 | { 428 | "cell_type": "markdown", 429 | "metadata": {}, 430 | "source": [ 431 | "是不是很炫?\n", 432 | "\n", 433 | "不過說實在這不太易讀, 我們也不一定非要這樣耍寶。只是說 Python 可以這麼做。" 434 | ] 435 | }, 436 | { 437 | "cell_type": "markdown", 438 | "metadata": {}, 439 | "source": [ 440 | "### `enumerate`\n", 441 | "\n", 442 | "我們要把一個 list 的元素, 一一列出來的時候...\n", 443 | "\n", 444 | "就是用 `for` 迴圈。" 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 48, 450 | "metadata": {}, 451 | "outputs": [], 452 | "source": [ 453 | "L = ['python', 'matplotlib', 'pandas', 'scikit-learn']" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 52, 459 | "metadata": {}, 460 | "outputs": [ 461 | { 462 | "name": "stdout", 463 | "output_type": "stream", 464 | "text": [ 465 | "python\n", 466 | "matplotlib\n", 467 | "pandas\n", 468 | "scikit-learn\n" 469 | ] 470 | } 471 | ], 472 | "source": [ 473 | "for i in L:\n", 474 | " print(i)" 475 | ] 476 | }, 477 | { 478 | "cell_type": "markdown", 479 | "metadata": {}, 480 | "source": [ 481 | "有時, 我們需要知道它的 `index`, 也就是第幾個元素。" 482 | ] 483 | }, 484 | { 485 | "cell_type": "code", 486 | "execution_count": 54, 487 | "metadata": {}, 488 | "outputs": [ 489 | { 490 | "name": "stdout", 491 | "output_type": "stream", 492 | "text": [ 493 | "0 python\n", 494 | "1 matplotlib\n", 495 | "2 pandas\n", 496 | "3 scikit-learn\n" 497 | ] 498 | } 499 | ], 500 | "source": [ 501 | "for i in range(len(L)):\n", 502 | " print(i, L[i])" 503 | ] 504 | }, 505 | { 506 | "cell_type": "markdown", 507 | "metadata": {}, 508 | "source": [ 509 | "但這好像麻煩了點。可以有更簡單的方式嗎? 有的, 就是用 `enumerate`。" 510 | ] 511 | }, 512 | { 513 | "cell_type": "code", 514 | "execution_count": 56, 515 | "metadata": {}, 516 | "outputs": [ 517 | { 518 | "name": "stdout", 519 | "output_type": "stream", 520 | "text": [ 521 | "(0, 'python')\n", 522 | "(1, 'matplotlib')\n", 523 | "(2, 'pandas')\n", 524 | "(3, 'scikit-learn')\n" 525 | ] 526 | } 527 | ], 528 | "source": [ 529 | "for x in enumerate(L):\n", 530 | " print(x)" 531 | ] 532 | }, 533 | { 534 | "cell_type": "markdown", 535 | "metadata": {}, 536 | "source": [ 537 | "#### unpack 技巧\n", 538 | "\n", 539 | "我們其實用了好幾次, 但沒認真說的是, Python 很容易從一個 tuple (或 list) 取出元素。" 540 | ] 541 | }, 542 | { 543 | "cell_type": "code", 544 | "execution_count": 58, 545 | "metadata": {}, 546 | "outputs": [], 547 | "source": [ 548 | "x = (8, 7)" 549 | ] 550 | }, 551 | { 552 | "cell_type": "code", 553 | "execution_count": 59, 554 | "metadata": {}, 555 | "outputs": [], 556 | "source": [ 557 | "a, b = x" 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 60, 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "data": { 567 | "text/plain": [ 568 | "8" 569 | ] 570 | }, 571 | "execution_count": 60, 572 | "metadata": {}, 573 | "output_type": "execute_result" 574 | } 575 | ], 576 | "source": [ 577 | "a" 578 | ] 579 | }, 580 | { 581 | "cell_type": "code", 582 | "execution_count": 61, 583 | "metadata": {}, 584 | "outputs": [ 585 | { 586 | "data": { 587 | "text/plain": [ 588 | "7" 589 | ] 590 | }, 591 | "execution_count": 61, 592 | "metadata": {}, 593 | "output_type": "execute_result" 594 | } 595 | ], 596 | "source": [ 597 | "b" 598 | ] 599 | }, 600 | { 601 | "cell_type": "markdown", 602 | "metadata": {}, 603 | "source": [ 604 | "所以, 用在 `enumerate` 上就會是..." 605 | ] 606 | }, 607 | { 608 | "cell_type": "code", 609 | "execution_count": 62, 610 | "metadata": {}, 611 | "outputs": [ 612 | { 613 | "name": "stdout", 614 | "output_type": "stream", 615 | "text": [ 616 | "0 python\n", 617 | "1 matplotlib\n", 618 | "2 pandas\n", 619 | "3 scikit-learn\n" 620 | ] 621 | } 622 | ], 623 | "source": [ 624 | "for i, x in enumerate(L):\n", 625 | " print(i, x)" 626 | ] 627 | }, 628 | { 629 | "cell_type": "markdown", 630 | "metadata": {}, 631 | "source": [ 632 | "更炫的是, 從 1 (或任何數字) 開始都可以!" 633 | ] 634 | }, 635 | { 636 | "cell_type": "code", 637 | "execution_count": 63, 638 | "metadata": {}, 639 | "outputs": [ 640 | { 641 | "name": "stdout", 642 | "output_type": "stream", 643 | "text": [ 644 | "1 python\n", 645 | "2 matplotlib\n", 646 | "3 pandas\n", 647 | "4 scikit-learn\n" 648 | ] 649 | } 650 | ], 651 | "source": [ 652 | "for i, x in enumerate(L, 1):\n", 653 | " print(i, x)" 654 | ] 655 | }, 656 | { 657 | "cell_type": "markdown", 658 | "metadata": {}, 659 | "source": [ 660 | "### 程式技巧\n", 661 | "\n", 662 | "先想好自己的策略, 分析需要什麼東西, 再想辦法寫成程式。\n", 663 | "\n", 664 | "例如, 我們想確認某字串最長的 $k$-交錯子字串是多長。\n", 665 | "\n", 666 | "#### step 1. 字串轉換\n", 667 | "\n", 668 | " st = 'abxCDefGHLmk'\n", 669 | " \n", 670 | "轉成\n", 671 | "\n", 672 | " binary_str = '000110011100'\n", 673 | " \n", 674 | "也就是小寫就記為 0, 大寫記為 1。\n", 675 | "\n", 676 | "#### step 2. 檢查交錯字串\n", 677 | "\n", 678 | "假設我們要看的是 2-交錯字串。 (k=2)\n", 679 | "\n", 680 | "##### 第 1 回合\n", 681 | "我們至少要看到其中一種模式:\n", 682 | "\n", 683 | " p0 = \"00\"\n", 684 | " p1 = \"11\"\n", 685 | " \n", 686 | "也就是\n", 687 | "\n", 688 | " p0 in binary_str\n", 689 | " p1 in binary_str\n", 690 | " \n", 691 | "至少一個成立, 那就進到第 2 回合, 否則就停止。\n", 692 | "\n", 693 | "##### 第 2 回合\n", 694 | "我們至少要看到其中一種模式:\n", 695 | "\n", 696 | " p0 = \"0011\"\n", 697 | " p1 = \"1100\"\n", 698 | " \n", 699 | "也就是\n", 700 | "\n", 701 | " p0 in binary_str\n", 702 | " p1 in binary_str\n", 703 | " \n", 704 | "至少一個成立, 那就進到第 3 回合, 否則就停止。\n", 705 | "\n", 706 | "##### 第 3 回合\n", 707 | "我們至少要看到其中一種模式:\n", 708 | "\n", 709 | " p0 = \"001100\"\n", 710 | " p1 = \"110011\"\n", 711 | " \n", 712 | "也就是\n", 713 | "\n", 714 | " p0 in binary_str\n", 715 | " p1 in binary_str\n", 716 | " \n", 717 | "至少一個成立, 那就進到第 3 回合, 否則就停止。\n", 718 | "\n", 719 | "我們要看最後第幾回合是成功的 (假設是 round 回), 那最後答案就是:\n", 720 | "\n", 721 | " round*k" 722 | ] 723 | } 724 | ], 725 | "metadata": { 726 | "kernelspec": { 727 | "display_name": "Python 3", 728 | "language": "python", 729 | "name": "python3" 730 | }, 731 | "language_info": { 732 | "codemirror_mode": { 733 | "name": "ipython", 734 | "version": 3 735 | }, 736 | "file_extension": ".py", 737 | "mimetype": "text/x-python", 738 | "name": "python", 739 | "nbconvert_exporter": "python", 740 | "pygments_lexer": "ipython3", 741 | "version": "3.7.3" 742 | } 743 | }, 744 | "nbformat": 4, 745 | "nbformat_minor": 2 746 | } 747 | -------------------------------------------------------------------------------- /09. APCS 精進計畫 III.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## Tree 的表示法\n", 8 | "\n", 9 | "這裡我們要考慮一個 tree 的圖。\n", 10 | "\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [] 19 | }, 20 | { 21 | "cell_type": "markdown", 22 | "metadata": {}, 23 | "source": [ 24 | "### 全域/局部變數的問題" 25 | ] 26 | }, 27 | { 28 | "cell_type": "markdown", 29 | "metadata": {}, 30 | "source": [ 31 | "```c\n", 32 | "#include\n", 33 | "\n", 34 | "int s = 1;\n", 35 | "\n", 36 | "void foo(){\n", 37 | "\tint s = 8;\n", 38 | "}\n", 39 | "\t\n", 40 | "int main(){\n", 41 | "\tprintf(\"原始的 s=%d\\n\", s);\n", 42 | "\tfoo();\n", 43 | "\tprintf(\"經函數 foo 後的 s=%d\\n\", s);\n", 44 | "\treturn 0;\n", 45 | "}```\n", 46 | "\n", 47 | "#### 執行結果\n", 48 | "\n", 49 | " 原始的 s=1\n", 50 | " 經函數 foo 後的 s=1\n", 51 | "\n", 52 | "這裡會發現雖然 `foo()` 函數會把 `s` 改成 8, 但是回來之後沒有改! 也就是外面世界是看不到 `foo` 函式裡那個 `s` 的。但 `s` 明明是全域變數, 為什麼沒有改到呢? 原因是我們在 `foo` 這個函數又「定義」一次 `s`, 於是會用新定義版本。\n", 53 | "\n", 54 | "在 Python 等價程式如下。" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": 7, 60 | "metadata": {}, 61 | "outputs": [ 62 | { 63 | "name": "stdout", 64 | "output_type": "stream", 65 | "text": [ 66 | "原始的 s=1\n", 67 | "經函數 foo 後的 s=1\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "s = 1\n", 73 | "\n", 74 | "def foo():\n", 75 | " s = 8\n", 76 | "\n", 77 | "print(f\"原始的 s={s}\")\n", 78 | "foo()\n", 79 | "print(f\"經函數 foo 後的 s={s}\")" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "再看只改一點點的程式。\n", 87 | "\n", 88 | "```c\n", 89 | "#include\n", 90 | "\n", 91 | "int s = 1;\n", 92 | "\n", 93 | "void foo(){\n", 94 | "\ts = 8;\n", 95 | "}\n", 96 | "\t\n", 97 | "int main(){\n", 98 | "\tprintf(\"原始的 s=%d\\n\", s);\n", 99 | "\tfoo();\n", 100 | "\tprintf(\"經函數 foo 後的 s=%d\\n\", s);\n", 101 | "\treturn 0;\n", 102 | "}```\n", 103 | "\n", 104 | "#### 執行結果\n", 105 | " 原始的 s=1\n", 106 | " 經函數 foo 後的 s=8\n", 107 | " \n", 108 | "這裡只把\n", 109 | "\n", 110 | "```c\n", 111 | "int s = 8;```\n", 112 | "\n", 113 | "改成\n", 114 | "\n", 115 | "```c\n", 116 | "s = 8;```\n", 117 | "\n", 118 | "這寫法和 Python 比較像, 可是怎麼會出來結果不同呢? 原因是 C 沒有重新定義的變數, 就會用原來的外面全域定義的變數, 一改就會改動全域變數的 `s`。\n", 119 | "\n", 120 | "Python 要做這作事的話, 要設 `foo` 裡的 `s` 為 `global` 變數。" 121 | ] 122 | }, 123 | { 124 | "cell_type": "code", 125 | "execution_count": 9, 126 | "metadata": {}, 127 | "outputs": [ 128 | { 129 | "name": "stdout", 130 | "output_type": "stream", 131 | "text": [ 132 | "原始的 s=1\n", 133 | "經函數 foo 後的 s=8\n" 134 | ] 135 | } 136 | ], 137 | "source": [ 138 | "s = 1\n", 139 | "\n", 140 | "def foo():\n", 141 | " global s \n", 142 | " s = 8\n", 143 | "\n", 144 | "print(f\"原始的 s={s}\")\n", 145 | "foo()\n", 146 | "print(f\"經函數 foo 後的 s={s}\")" 147 | ] 148 | }, 149 | { 150 | "cell_type": "markdown", 151 | "metadata": {}, 152 | "source": [ 153 | "### 錯誤程式的修正 (`break` 使用)" 154 | ] 155 | }, 156 | { 157 | "cell_type": "markdown", 158 | "metadata": {}, 159 | "source": [ 160 | " 11 12 13 14 15 3\n", 161 | " 11 12 13 14 25 20\n", 162 | " 23 15 18 20 11 12\n", 163 | " 18 17 19 24 15 16" 164 | ] 165 | }, 166 | { 167 | "cell_type": "code", 168 | "execution_count": 6, 169 | "metadata": {}, 170 | "outputs": [ 171 | { 172 | "name": "stdout", 173 | "output_type": "stream", 174 | "text": [ 175 | "11 12 13 14 25 20\n", 176 | "20 is not the smallest.\n" 177 | ] 178 | } 179 | ], 180 | "source": [ 181 | "line = input().split()\n", 182 | "n_line = [int(x) for x in line]\n", 183 | "\n", 184 | "d = n_line[:-1]\n", 185 | "val = n_line[-1]\n", 186 | "allBig = True\n", 187 | "for i in range(5):\n", 188 | " if (d[i]>val):\n", 189 | " allBig = True\n", 190 | " else:\n", 191 | " allBig = False\n", 192 | " \n", 193 | "if (allBig == True):\n", 194 | " print(f\"{val} is the smallest.\")\n", 195 | "else:\n", 196 | " print(f\"{val} is not the smallest.\")" 197 | ] 198 | }, 199 | { 200 | "cell_type": "code", 201 | "execution_count": null, 202 | "metadata": {}, 203 | "outputs": [], 204 | "source": [] 205 | }, 206 | { 207 | "cell_type": "markdown", 208 | "metadata": {}, 209 | "source": [ 210 | "## LeetCode 練習程式技巧\n", 211 | "\n", 212 | "[LeetCode](https://leetcode.com) 是非常有名的程式練習網站, 很多人在練習程式功力、尤其是想要找程式設計師相關工作的, 常常會「刷 LeetCode」練習。\n", 213 | "\n", 214 | "我們開始可以專選 \"Easy\" 等級的題目, 如果我們程式正確回答 (submit 成功) 之後, 題目前面就會打個勾。而一些「上鎖」的題目, 就是要付費後才可以看。不過沒有上鎖的題目已非常多, 相信很足夠開始的練習。\n", 215 | "\n", 216 | "![LeetCode 問題列表](images/leetcode01.png)" 217 | ] 218 | }, 219 | { 220 | "cell_type": "markdown", 221 | "metadata": {}, 222 | "source": [ 223 | "#### LeetCode 回答的準備\n", 224 | "\n", 225 | "選好一個問題進去, 我們要選擇回答的語言是 \"Python 3\", 這時會出來一個看來有點可怕的 class 定義。但不用擔心! 每次都只改這 class 裡的 (唯一的) 方法 (函式), 記得答案都是用 `return` 回傳回來就可以。\n", 226 | "\n", 227 | "![LeetCode 問題例子](images/leetcode02.png)\n", 228 | "\n", 229 | "我們要注意的是這段要略略的修改:\n", 230 | "\n", 231 | "```python\n", 232 | "class Solution:\n", 233 | " def fib(self, N: int) -> int:```\n", 234 | " \n", 235 | "要改成\n", 236 | "```python\n", 237 | "class Solution:\n", 238 | " def fib(self, N):```\n", 239 | " \n", 240 | "記得回傳需要是一個整數, 假設叫做 `M`, 那整個程式架構會是這樣:\n", 241 | "\n", 242 | "```python\n", 243 | "class Solution:\n", 244 | " def fib(self, N):\n", 245 | " \n", 246 | " (你的程式)\n", 247 | " \n", 248 | " return M```" 249 | ] 250 | }, 251 | { 252 | "cell_type": "markdown", 253 | "metadata": {}, 254 | "source": [ 255 | "#### Submit 你的程式\n", 256 | "\n", 257 | "我們在真正提交自己答案前, 可以先 Run 我們的程式看看, 如果和預期答案一樣, 我們就可以 Submit 了! 這裡要注意的是, 因為是寫在 Class 裡面, 要呼叫 `fib` 這個函數, 要用 `self.fib`。\n", 258 | "\n", 259 | "![Run LeetCode](images/leetcode03.png)" 260 | ] 261 | }, 262 | { 263 | "cell_type": "markdown", 264 | "metadata": {}, 265 | "source": [ 266 | "成功就會在左上角出現 Success! 失敗的話再回來改就是了。" 267 | ] 268 | } 269 | ], 270 | "metadata": { 271 | "kernelspec": { 272 | "display_name": "Python 3", 273 | "language": "python", 274 | "name": "python3" 275 | }, 276 | "language_info": { 277 | "codemirror_mode": { 278 | "name": "ipython", 279 | "version": 3 280 | }, 281 | "file_extension": ".py", 282 | "mimetype": "text/x-python", 283 | "name": "python", 284 | "nbconvert_exporter": "python", 285 | "pygments_lexer": "ipython3", 286 | "version": "3.7.3" 287 | } 288 | }, 289 | "nbformat": 4, 290 | "nbformat_minor": 2 291 | } 292 | -------------------------------------------------------------------------------- /10. MicroBit 等等好玩的程式設計.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![Micro:bit](images/T_microbit.png)\n", 8 | "\n", 9 | "就是要好玩, 才學得會!\n", 10 | "\n", 11 | "我寫程式是國中時代開始, 程式都是英文指令, 所以我一直覺得至少國中開始, 學 Python 這樣的程式語言是可以的。\n", 12 | "\n", 13 | "就覺得好玩, 有時會去參加每個月雜誌出題的回答, 還被編輯說「小小年紀就能寫...」之類的評語。高中寫了幾篇電腦的文章, 出了兩個遊戲、教學 (?) 程式。\n", 14 | "\n", 15 | "![拳形岩](images/fist_rock.png)" 16 | ] 17 | }, 18 | { 19 | "cell_type": "markdown", 20 | "metadata": {}, 21 | "source": [ 22 | "## 一些程式教學計畫" 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "### Programming the World\n", 30 | "\n", 31 | "成功大學蘇文鈺老師, 著有《做孩子的重要他人》, 長期投入偏鄉小朋友的程式教育。\n", 32 | "\n", 33 | "![Programming the World](images/PWA.png)" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "### Soobi 計畫\n", 41 | "\n", 42 | "由政大應數系曾正男老師主持, 現在大力推廣使用 Micro:bit 來帶偏鄉中小學程式設計。\n", 43 | "\n", 44 | "![Soobi](images/soobi.png)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "### 不插電/偷插電的資訊教育\n", 52 | "\n", 53 | "[軟體自由協會書城](https://sites.google.com/view/slat-bookstore/首頁)可以訂購這兩本書, 而且也有免費的電子版!\n", 54 | "\n", 55 | "![SLAT](images/slat.png)" 56 | ] 57 | }, 58 | { 59 | "cell_type": "markdown", 60 | "metadata": {}, 61 | "source": [ 62 | "## 工具\n", 63 | "\n", 64 | "### Scratch\n", 65 | "\n", 66 | "MIT 開發的 [Scratch](https://scratch.mit.edu) 簡單、好上手, 常常被當成一個教小朋友程式設計的工具。\n", 67 | "\n", 68 | "![Scratch](images/scratch.gif)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "markdown", 73 | "metadata": {}, 74 | "source": [ 75 | "### Micro:bit\n", 76 | "\n", 77 | "[Micro:bit (香港)繁體網站](https://microbit.org/hk/code/)\n", 78 | "這是 Micro:bit 的官網, 很方便查有什麼指令可以使用、怎麼用。\n", 79 | "\n", 80 | "[好用的 Mu Editor](https://codewith.mu/)\n", 81 | "Mu 編輯器甚至比 Micro:bit 官網還好用! 上傳程式按個鍵就上傳, 不需要先下載檔案再手動上傳。\n", 82 | "\n", 83 | "[Micro:bit 線上模擬器](https://create.withcode.uk)\n", 84 | "這裡可以看到 Micro:bit 執行的狀況, 還可以模擬各種動作, 非常酷!" 85 | ] 86 | }, 87 | { 88 | "cell_type": "markdown", 89 | "metadata": {}, 90 | "source": [ 91 | "#### 點的漸亮漸暗\n", 92 | "\n", 93 | "讓一個點漸漸亮、再漸暗, 一直反覆。\n", 94 | "\n", 95 | "```python\n", 96 | "from microbit import *\n", 97 | "from random import randint\n", 98 | "\n", 99 | "while True:\n", 100 | " for i in range(10):\n", 101 | " display.set_pixel(2, 2, i)\n", 102 | " sleep(100)\n", 103 | " for i in range(8,0,-1):\n", 104 | " display.set_pixel(2, 2, i)\n", 105 | " sleep(100)```" 106 | ] 107 | }, 108 | { 109 | "cell_type": "markdown", 110 | "metadata": {}, 111 | "source": [ 112 | "#### 一個光條、一直流動\n", 113 | "\n", 114 | "設一條光條, 一直往下移動。翻成程式的意思是, 我們亂數產生一個長度為 5, 每個數字是 0-9 的 list, 比如說\n", 115 | "\n", 116 | " [3, 0, 2, 5, 9]\n", 117 | " \n", 118 | "我們這串 list 為亮度, 畫出一條光條, 最左邊的在最下方。接著, 每次都加入一個數字, 並且把最左的數字刪去。比如說, 我們的例子可能變成:\n", 119 | "\n", 120 | " [0, 2, 5, 9, 8]\n", 121 | " [2, 5, 9, 8, 7]\n", 122 | " [5, 9, 8, 7, 3]\n", 123 | " :\n", 124 | " :\n", 125 | "\n", 126 | "等等。\n", 127 | "\n", 128 | "```python\n", 129 | "from microbit import *\n", 130 | "from random import randint\n", 131 | "\n", 132 | "line = []\n", 133 | "\n", 134 | "for i in range(5):\n", 135 | " line.append(randint(0,9))\n", 136 | "\n", 137 | "while True:\n", 138 | " for i in range(5):\n", 139 | " display.set_pixel(2, 4-i, line[i])\n", 140 | " sleep(200)\n", 141 | " line.append(randint(0,9))\n", 142 | " line.pop(0)```" 143 | ] 144 | }, 145 | { 146 | "cell_type": "markdown", 147 | "metadata": {}, 148 | "source": [ 149 | "#### 完整版\n", 150 | "\n", 151 | "![flow](images/flow.gif)" 152 | ] 153 | }, 154 | { 155 | "cell_type": "markdown", 156 | "metadata": {}, 157 | "source": [ 158 | "#### 產生一個卦\n", 159 | "\n", 160 | "我們來寫一個種式, 讓 Micro:bit 搖兩次之後, 就出現一個八卦的卦象。\n", 161 | "\n", 162 | "![八卦](images/hexagram.png)" 163 | ] 164 | }, 165 | { 166 | "cell_type": "markdown", 167 | "metadata": {}, 168 | "source": [ 169 | "```python\n", 170 | "from microbit import *\n", 171 | "from random import choice\n", 172 | "\n", 173 | "yao = [\"99999\", \"99099\"]\n", 174 | "\n", 175 | "c = 0\n", 176 | "while True:\n", 177 | " if accelerometer.was_gesture(\"shake\"):\n", 178 | " c = c + 1\n", 179 | " if c == 2:\n", 180 | " yao_list = []\n", 181 | " for i in range(3):\n", 182 | " yao_list.append(choice(yao))\n", 183 | "\n", 184 | " hex_str = \":00000:\".join(yao_list)\n", 185 | " hexagram = Image(hex_str)\n", 186 | " display.show(hexagram)\n", 187 | " c = 0```" 188 | ] 189 | } 190 | ], 191 | "metadata": { 192 | "kernelspec": { 193 | "display_name": "Python 3", 194 | "language": "python", 195 | "name": "python3" 196 | }, 197 | "language_info": { 198 | "codemirror_mode": { 199 | "name": "ipython", 200 | "version": 3 201 | }, 202 | "file_extension": ".py", 203 | "mimetype": "text/x-python", 204 | "name": "python", 205 | "nbconvert_exporter": "python", 206 | "pygments_lexer": "ipython3", 207 | "version": "3.7.3" 208 | } 209 | }, 210 | "nbformat": 4, 211 | "nbformat_minor": 2 212 | } 213 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## APCS 2017 年 10 月 \n", 8 | "## 第 1 題: 邏輯運算子 (Logic Operators)\n", 9 | "\n", 10 | "### 問題描述\n", 11 | "\n", 12 | "小蘇最近在學三種邏輯運算子 `AND`、`OR` 和 `XOR`。這三種運算子都是二元運算子,\n", 13 | "\n", 14 | "也就是說在運算時需要兩個運算元,例如 `a AND b`。對於整數a與b,以下三個二元運算\n", 15 | "子的運算結果定義如下列三個表格:\n", 16 | "\n", 17 | "##### a `AND` b\n", 18 | "\n", 19 | "| * | b 為 0 | b 不為 0 |\n", 20 | "|---|--------|------------|\n", 21 | "| a 為 0 | 0 | 0 |\n", 22 | "| a 不為 0 | 0 | 1 |\n", 23 | "\n", 24 | "##### a `OR` b\n", 25 | "| * | b 為 0 | b 不為 0 |\n", 26 | "|---|--------|------------|\n", 27 | "| a 為 0 | 0 | 1 |\n", 28 | "| a 不為 0 | 1 | 1 |\n", 29 | "\n", 30 | "##### a `XOR` b\n", 31 | "| * | b 為 0 | b 不為 0 |\n", 32 | "|---|--------|------------|\n", 33 | "| a 為 0 | 0 | 1 |\n", 34 | "| a 不為 0 | 1 | 0 |\n", 35 | "\n", 36 | "\n", 37 | "### 舉例來說:\n", 38 | "\n", 39 | "1. 0 `AND` 0 的結果為 0 , 0 `OR` 0 以及 0 `XOR` 0 的結果也為 0 。\n", 40 | "2. 0 `AND` 3 的結果為 0 , 0 `OR` 3 以及 0 `XOR` 3 的結果則為 1 。\n", 41 | "3. 4 `AND` 9 的結果為 1 , 4 `OR` 9 的結果也為 1 ,但 4 `XOR` 9 的結果為 0 。\n", 42 | "\n", 43 | "\n", 44 | "請撰寫一個程式,讀入 a、b 以及邏輯運算的結果,輸出可能的邏輯運算為何。\n", 45 | "\n", 46 | "### 輸入格式\n", 47 | "輸入只有一行,共三個整數值,整數間以一個空白隔開。第一個整數代表 a,第二\n", 48 | "個整數代表 b,這兩數均為非負的整數。第三個整數代表邏輯運算的結果,只會是 0 或 1 。\n", 49 | "\n", 50 | "### 輸出格式\n", 51 | "輸出可能得到指定結果的運算,若有多個,輸出順序為 `AND`、`OR`、`XOR`,每個可能\n", 52 | "的運算單獨輸出一行,每行結尾皆有換行。若不可能得到指定結果,輸出 `IMPOSSIBLE`。\n", 53 | "(注意輸出時所有英文字母均為大寫字母。)\n", 54 | "\n", 55 | "\n", 56 | "#### 範例一:輸入\n", 57 | " 0 0 0\n", 58 | "\n", 59 | "#### 範例一:正確輸出\n", 60 | " AND\n", 61 | " OR\n", 62 | " XOR\n", 63 | "\n", 64 | "#### 範例二:輸入\n", 65 | " 1 1 1\n", 66 | "\n", 67 | "#### 範例二:正確輸出\n", 68 | " AND\n", 69 | " OR\n", 70 | "\n", 71 | "#### 範例三:輸入\n", 72 | " 3 0 1\n", 73 | "\n", 74 | "#### 範例三:正確輸出\n", 75 | " OR\n", 76 | " XOR\n", 77 | "\n", 78 | "#### 範例四:輸入\n", 79 | " 0 0 1\n", 80 | "\n", 81 | "#### 範例四:正確輸出\n", 82 | " IMPOSSIBLE\n", 83 | "\n", 84 | "### 評分說明\n", 85 | "\n", 86 | "輸入包含若干筆測試資料,每一筆測試資料的執行時間限制(time limit)均為 1 秒,依\n", 87 | "正確通過測資筆數給分。其中:\n", 88 | "\n", 89 | "第 1 子題組 80 分,a 和 b 的值只會是 0 或 1 。\n", 90 | "\n", 91 | "第 2 子題組 20 分, $0\\leq a, b < 10,000$。" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 14, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "0 0 1\n", 104 | "IMPOSSIBLE\n" 105 | ] 106 | } 107 | ], 108 | "source": [ 109 | "line = input().split()\n", 110 | "\n", 111 | "def conv(x):\n", 112 | " k = int(x)\n", 113 | " if k:\n", 114 | " k = 1\n", 115 | " return k\n", 116 | "a, b, c = map(conv, line)\n", 117 | "\n", 118 | "\n", 119 | "check = [a & b == c, a | b == c, a ^ b == c]\n", 120 | "operator = ['AND', 'OR', 'XOR']\n", 121 | "\n", 122 | "f = 0\n", 123 | "\n", 124 | "for i in range(3):\n", 125 | " if check[i]:\n", 126 | " print(operator[i])\n", 127 | " f = 1\n", 128 | "\n", 129 | "if f == 0:\n", 130 | " print(\"IMPOSSIBLE\")\n" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": 1, 136 | "metadata": {}, 137 | "outputs": [ 138 | { 139 | "data": { 140 | "text/plain": [ 141 | "0" 142 | ] 143 | }, 144 | "execution_count": 1, 145 | "metadata": {}, 146 | "output_type": "execute_result" 147 | } 148 | ], 149 | "source": [ 150 | "3 & 0" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 3, 156 | "metadata": {}, 157 | "outputs": [ 158 | { 159 | "data": { 160 | "text/plain": [ 161 | "False" 162 | ] 163 | }, 164 | "execution_count": 3, 165 | "metadata": {}, 166 | "output_type": "execute_result" 167 | } 168 | ], 169 | "source": [ 170 | "3^0 == 1" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 4, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "True" 182 | ] 183 | }, 184 | "execution_count": 4, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "1^0 == 1" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 6, 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "name": "stdout", 200 | "output_type": "stream", 201 | "text": [ 202 | "1 0 0\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "line = input().split()" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 7, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "data": { 217 | "text/plain": [ 218 | "['1', '0', '0']" 219 | ] 220 | }, 221 | "execution_count": 7, 222 | "metadata": {}, 223 | "output_type": "execute_result" 224 | } 225 | ], 226 | "source": [ 227 | "line" 228 | ] 229 | }, 230 | { 231 | "cell_type": "code", 232 | "execution_count": 8, 233 | "metadata": {}, 234 | "outputs": [], 235 | "source": [ 236 | "a = line[0]\n", 237 | "b = line[1]\n", 238 | "c = line[2]" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 9, 244 | "metadata": {}, 245 | "outputs": [], 246 | "source": [ 247 | "a, b, c = line" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 10, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "data": { 257 | "text/plain": [ 258 | "'1'" 259 | ] 260 | }, 261 | "execution_count": 10, 262 | "metadata": {}, 263 | "output_type": "execute_result" 264 | } 265 | ], 266 | "source": [ 267 | "a" 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": 11, 273 | "metadata": {}, 274 | "outputs": [ 275 | { 276 | "data": { 277 | "text/plain": [ 278 | "'0'" 279 | ] 280 | }, 281 | "execution_count": 11, 282 | "metadata": {}, 283 | "output_type": "execute_result" 284 | } 285 | ], 286 | "source": [ 287 | "b" 288 | ] 289 | }, 290 | { 291 | "cell_type": "code", 292 | "execution_count": 12, 293 | "metadata": {}, 294 | "outputs": [ 295 | { 296 | "data": { 297 | "text/plain": [ 298 | "'0'" 299 | ] 300 | }, 301 | "execution_count": 12, 302 | "metadata": {}, 303 | "output_type": "execute_result" 304 | } 305 | ], 306 | "source": [ 307 | "c" 308 | ] 309 | }, 310 | { 311 | "cell_type": "code", 312 | "execution_count": 17, 313 | "metadata": {}, 314 | "outputs": [ 315 | { 316 | "name": "stdout", 317 | "output_type": "stream", 318 | "text": [ 319 | "3 0 1\n" 320 | ] 321 | } 322 | ], 323 | "source": [ 324 | "a, b, c = input().split()" 325 | ] 326 | }, 327 | { 328 | "cell_type": "code", 329 | "execution_count": 18, 330 | "metadata": {}, 331 | "outputs": [ 332 | { 333 | "data": { 334 | "text/plain": [ 335 | "'3'" 336 | ] 337 | }, 338 | "execution_count": 18, 339 | "metadata": {}, 340 | "output_type": "execute_result" 341 | } 342 | ], 343 | "source": [ 344 | "a" 345 | ] 346 | }, 347 | { 348 | "cell_type": "code", 349 | "execution_count": 19, 350 | "metadata": {}, 351 | "outputs": [ 352 | { 353 | "data": { 354 | "text/plain": [ 355 | "'0'" 356 | ] 357 | }, 358 | "execution_count": 19, 359 | "metadata": {}, 360 | "output_type": "execute_result" 361 | } 362 | ], 363 | "source": [ 364 | "b" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": 20, 370 | "metadata": {}, 371 | "outputs": [ 372 | { 373 | "data": { 374 | "text/plain": [ 375 | "'1'" 376 | ] 377 | }, 378 | "execution_count": 20, 379 | "metadata": {}, 380 | "output_type": "execute_result" 381 | } 382 | ], 383 | "source": [ 384 | "c" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": 21, 390 | "metadata": {}, 391 | "outputs": [], 392 | "source": [ 393 | "if int(a) != 0:\n", 394 | " a = 1\n", 395 | "else:\n", 396 | " a = 0\n", 397 | " " 398 | ] 399 | }, 400 | { 401 | "cell_type": "code", 402 | "execution_count": 22, 403 | "metadata": {}, 404 | "outputs": [ 405 | { 406 | "data": { 407 | "text/plain": [ 408 | "1" 409 | ] 410 | }, 411 | "execution_count": 22, 412 | "metadata": {}, 413 | "output_type": "execute_result" 414 | } 415 | ], 416 | "source": [ 417 | "a" 418 | ] 419 | }, 420 | { 421 | "cell_type": "code", 422 | "execution_count": 30, 423 | "metadata": {}, 424 | "outputs": [ 425 | { 426 | "data": { 427 | "text/plain": [ 428 | "['3', '1', '2']" 429 | ] 430 | }, 431 | "execution_count": 30, 432 | "metadata": {}, 433 | "output_type": "execute_result" 434 | } 435 | ], 436 | "source": [ 437 | "line" 438 | ] 439 | }, 440 | { 441 | "cell_type": "code", 442 | "execution_count": null, 443 | "metadata": {}, 444 | "outputs": [], 445 | "source": [] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "execution_count": 28, 450 | "metadata": {}, 451 | "outputs": [], 452 | "source": [ 453 | "# a, b, c = int(a), int(b), int(c)" 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": 31, 459 | "metadata": {}, 460 | "outputs": [], 461 | "source": [ 462 | "a, b, c = map(int, line)" 463 | ] 464 | }, 465 | { 466 | "cell_type": "code", 467 | "execution_count": 32, 468 | "metadata": {}, 469 | "outputs": [ 470 | { 471 | "data": { 472 | "text/plain": [ 473 | "(3, 1, 2)" 474 | ] 475 | }, 476 | "execution_count": 32, 477 | "metadata": {}, 478 | "output_type": "execute_result" 479 | } 480 | ], 481 | "source": [ 482 | "a, b, c" 483 | ] 484 | }, 485 | { 486 | "cell_type": "code", 487 | "execution_count": null, 488 | "metadata": {}, 489 | "outputs": [], 490 | "source": [] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": 25, 495 | "metadata": {}, 496 | "outputs": [ 497 | { 498 | "data": { 499 | "text/plain": [ 500 | "(3, 0, 1)" 501 | ] 502 | }, 503 | "execution_count": 25, 504 | "metadata": {}, 505 | "output_type": "execute_result" 506 | } 507 | ], 508 | "source": [ 509 | "a, b, c" 510 | ] 511 | }, 512 | { 513 | "cell_type": "code", 514 | "execution_count": 27, 515 | "metadata": {}, 516 | "outputs": [ 517 | { 518 | "ename": "TypeError", 519 | "evalue": "int() argument must be a string, a bytes-like object or a number, not 'list'", 520 | "output_type": "error", 521 | "traceback": [ 522 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 523 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 524 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'3'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'2'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'5'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 525 | "\u001b[0;31mTypeError\u001b[0m: int() argument must be a string, a bytes-like object or a number, not 'list'" 526 | ] 527 | } 528 | ], 529 | "source": [ 530 | "int(['3', '2', '5'])" 531 | ] 532 | }, 533 | { 534 | "cell_type": "code", 535 | "execution_count": 34, 536 | "metadata": {}, 537 | "outputs": [ 538 | { 539 | "data": { 540 | "text/plain": [ 541 | "True" 542 | ] 543 | }, 544 | "execution_count": 34, 545 | "metadata": {}, 546 | "output_type": "execute_result" 547 | } 548 | ], 549 | "source": [ 550 | "bool(3)" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "execution_count": 35, 556 | "metadata": {}, 557 | "outputs": [ 558 | { 559 | "data": { 560 | "text/plain": [ 561 | "False" 562 | ] 563 | }, 564 | "execution_count": 35, 565 | "metadata": {}, 566 | "output_type": "execute_result" 567 | } 568 | ], 569 | "source": [ 570 | "bool(0)" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": 37, 576 | "metadata": {}, 577 | "outputs": [ 578 | { 579 | "data": { 580 | "text/plain": [ 581 | "True" 582 | ] 583 | }, 584 | "execution_count": 37, 585 | "metadata": {}, 586 | "output_type": "execute_result" 587 | } 588 | ], 589 | "source": [ 590 | "bool(-3)" 591 | ] 592 | }, 593 | { 594 | "cell_type": "code", 595 | "execution_count": 54, 596 | "metadata": {}, 597 | "outputs": [], 598 | "source": [ 599 | "a = 7" 600 | ] 601 | }, 602 | { 603 | "cell_type": "code", 604 | "execution_count": 55, 605 | "metadata": {}, 606 | "outputs": [], 607 | "source": [ 608 | "if a != 0:\n", 609 | " a = 1" 610 | ] 611 | }, 612 | { 613 | "cell_type": "code", 614 | "execution_count": 56, 615 | "metadata": {}, 616 | "outputs": [ 617 | { 618 | "data": { 619 | "text/plain": [ 620 | "1" 621 | ] 622 | }, 623 | "execution_count": 56, 624 | "metadata": {}, 625 | "output_type": "execute_result" 626 | } 627 | ], 628 | "source": [ 629 | "a" 630 | ] 631 | }, 632 | { 633 | "cell_type": "code", 634 | "execution_count": null, 635 | "metadata": {}, 636 | "outputs": [], 637 | "source": [] 638 | }, 639 | { 640 | "cell_type": "code", 641 | "execution_count": null, 642 | "metadata": {}, 643 | "outputs": [], 644 | "source": [] 645 | }, 646 | { 647 | "cell_type": "markdown", 648 | "metadata": {}, 649 | "source": [ 650 | "### 我的程式區" 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": 71, 656 | "metadata": {}, 657 | "outputs": [ 658 | { 659 | "name": "stdout", 660 | "output_type": "stream", 661 | "text": [ 662 | "1 2 0\n", 663 | "XOR\n" 664 | ] 665 | } 666 | ], 667 | "source": [ 668 | "line = input().split()\n", 669 | "a, b, c = map(int, line)\n", 670 | "if a != 0:\n", 671 | " a = 1\n", 672 | "if b != 0:\n", 673 | " b = 1\n", 674 | "if c != 0:\n", 675 | " c = 1\n", 676 | "\n", 677 | "fail = True\n", 678 | "if (a & b == c):\n", 679 | " print(\"AND\")\n", 680 | " fail = False\n", 681 | "if (a | b == c):\n", 682 | " print(\"OR\")\n", 683 | " fail = False\n", 684 | "if (a ^ b == c):\n", 685 | " print(\"XOR\")\n", 686 | " fail = False\n", 687 | "if fail:\n", 688 | " print(\"IMPOSSIBLE\")" 689 | ] 690 | }, 691 | { 692 | "cell_type": "code", 693 | "execution_count": 72, 694 | "metadata": {}, 695 | "outputs": [ 696 | { 697 | "name": "stdout", 698 | "output_type": "stream", 699 | "text": [ 700 | "File `p1.py` exists. Overwrite (y/[N])? y\n", 701 | "The following commands were written to file `p1.py`:\n", 702 | "line = input().split()\n", 703 | "a, b, c = map(int, line)\n", 704 | "if a != 0:\n", 705 | " a = 1\n", 706 | "if b != 0:\n", 707 | " b = 1\n", 708 | "if c != 0:\n", 709 | " c = 1\n", 710 | "\n", 711 | "fail = True\n", 712 | "if (a & b == c):\n", 713 | " print(\"AND\")\n", 714 | " fail = False\n", 715 | "if (a | b == c):\n", 716 | " print(\"OR\")\n", 717 | " fail = False\n", 718 | "if (a ^ b == c):\n", 719 | " print(\"XOR\")\n", 720 | " fail = False\n", 721 | "if fail:\n", 722 | " print(\"IMPOSSIBLE\")\n" 723 | ] 724 | } 725 | ], 726 | "source": [ 727 | "%save \"p1.py\" 71" 728 | ] 729 | }, 730 | { 731 | "cell_type": "code", 732 | "execution_count": 73, 733 | "metadata": {}, 734 | "outputs": [ 735 | { 736 | "name": "stdout", 737 | "output_type": "stream", 738 | "text": [ 739 | "Writing p1t01.txt\n" 740 | ] 741 | } 742 | ], 743 | "source": [ 744 | "%%writefile \"p1t01.txt\"\n", 745 | "0 0 0" 746 | ] 747 | }, 748 | { 749 | "cell_type": "code", 750 | "execution_count": 74, 751 | "metadata": {}, 752 | "outputs": [ 753 | { 754 | "name": "stdout", 755 | "output_type": "stream", 756 | "text": [ 757 | "Writing p1t02.txt\n" 758 | ] 759 | } 760 | ], 761 | "source": [ 762 | "%%writefile \"p1t02.txt\"\n", 763 | "1 1 1" 764 | ] 765 | }, 766 | { 767 | "cell_type": "code", 768 | "execution_count": 75, 769 | "metadata": {}, 770 | "outputs": [ 771 | { 772 | "name": "stdout", 773 | "output_type": "stream", 774 | "text": [ 775 | "Writing p1t03.txt\n" 776 | ] 777 | } 778 | ], 779 | "source": [ 780 | "%%writefile \"p1t03.txt\"\n", 781 | "3 0 1" 782 | ] 783 | }, 784 | { 785 | "cell_type": "code", 786 | "execution_count": 76, 787 | "metadata": {}, 788 | "outputs": [ 789 | { 790 | "name": "stdout", 791 | "output_type": "stream", 792 | "text": [ 793 | "Writing p1t04.txt\n" 794 | ] 795 | } 796 | ], 797 | "source": [ 798 | "%%writefile \"p1t04.txt\"\n", 799 | "0 0 1" 800 | ] 801 | }, 802 | { 803 | "cell_type": "code", 804 | "execution_count": 77, 805 | "metadata": {}, 806 | "outputs": [ 807 | { 808 | "name": "stdout", 809 | "output_type": "stream", 810 | "text": [ 811 | "AND\r\n", 812 | "OR\r\n", 813 | "XOR\r\n" 814 | ] 815 | } 816 | ], 817 | "source": [ 818 | "!python p1.py < p1t01.txt" 819 | ] 820 | }, 821 | { 822 | "cell_type": "code", 823 | "execution_count": 78, 824 | "metadata": {}, 825 | "outputs": [ 826 | { 827 | "name": "stdout", 828 | "output_type": "stream", 829 | "text": [ 830 | "AND\r\n", 831 | "OR\r\n" 832 | ] 833 | } 834 | ], 835 | "source": [ 836 | "!python p1.py < p1t02.txt" 837 | ] 838 | }, 839 | { 840 | "cell_type": "code", 841 | "execution_count": 79, 842 | "metadata": {}, 843 | "outputs": [ 844 | { 845 | "name": "stdout", 846 | "output_type": "stream", 847 | "text": [ 848 | "OR\r\n", 849 | "XOR\r\n" 850 | ] 851 | } 852 | ], 853 | "source": [ 854 | "!python p1.py < p1t03.txt" 855 | ] 856 | }, 857 | { 858 | "cell_type": "code", 859 | "execution_count": 80, 860 | "metadata": {}, 861 | "outputs": [ 862 | { 863 | "name": "stdout", 864 | "output_type": "stream", 865 | "text": [ 866 | "IMPOSSIBLE\r\n" 867 | ] 868 | } 869 | ], 870 | "source": [ 871 | "!python p1.py < p1t04.txt" 872 | ] 873 | }, 874 | { 875 | "cell_type": "code", 876 | "execution_count": null, 877 | "metadata": {}, 878 | "outputs": [], 879 | "source": [] 880 | } 881 | ], 882 | "metadata": { 883 | "kernelspec": { 884 | "display_name": "Python 3", 885 | "language": "python", 886 | "name": "python3" 887 | }, 888 | "language_info": { 889 | "codemirror_mode": { 890 | "name": "ipython", 891 | "version": 3 892 | }, 893 | "file_extension": ".py", 894 | "mimetype": "text/x-python", 895 | "name": "python", 896 | "nbconvert_exporter": "python", 897 | "pygments_lexer": "ipython3", 898 | "version": "3.7.3" 899 | } 900 | }, 901 | "nbformat": 4, 902 | "nbformat_minor": 2 903 | } 904 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p1.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | line = input().split() 3 | a, b, c = map(int, line) 4 | if a != 0: 5 | a = 1 6 | if b != 0: 7 | b = 1 8 | if c != 0: 9 | c = 1 10 | 11 | fail = True 12 | if (a & b == c): 13 | print("AND") 14 | fail = False 15 | if (a | b == c): 16 | print("OR") 17 | fail = False 18 | if (a ^ b == c): 19 | print("XOR") 20 | fail = False 21 | if fail: 22 | print("IMPOSSIBLE") 23 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p1t01.txt: -------------------------------------------------------------------------------- 1 | 0 0 0 2 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p1t02.txt: -------------------------------------------------------------------------------- 1 | 1 1 1 2 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p1t03.txt: -------------------------------------------------------------------------------- 1 | 3 0 1 2 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p1t04.txt: -------------------------------------------------------------------------------- 1 | 0 0 1 2 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p2.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | k = int(input()) 3 | line = input() 4 | 5 | st = ''.join([str(int(x.isupper())) for x in line]) 6 | 7 | pattern = ['0'*k, '1'*k] 8 | p0 = '' 9 | p1 = '' 10 | 11 | c = True 12 | m = 0 13 | 14 | 15 | while c: 16 | p0 = p0 + pattern[m%2] 17 | p1 = p1 + pattern[(m+1)%2] 18 | 19 | if (p0 in st) or (p1 in st): 20 | m = m + 1 21 | else: 22 | c = False 23 | 24 | print(m*k) 25 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p2t01.txt: -------------------------------------------------------------------------------- 1 | 1 2 | aBBdaaa 3 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p2t02.txt: -------------------------------------------------------------------------------- 1 | 2 2 | aafAXbbCDCCC 3 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p2t03.txt: -------------------------------------------------------------------------------- 1 | 3 2 | DDaasAAbbCC 3 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p2t04.txt: -------------------------------------------------------------------------------- 1 | 3 2 | DDaaAAbbCC 3 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "Writing p3t01.txt\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "%%writefile \"p3t01.txt\"\n", 18 | "7\n", 19 | "0\n", 20 | "2 6 7\n", 21 | "2 1 4\n", 22 | "0\n", 23 | "2 3 2\n", 24 | "0\n", 25 | "0" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 2, 31 | "metadata": {}, 32 | "outputs": [ 33 | { 34 | "name": "stdout", 35 | "output_type": "stream", 36 | "text": [ 37 | "Writing p3t02.txt\n" 38 | ] 39 | } 40 | ], 41 | "source": [ 42 | "%%writefile \"p3t02.txt\"\n", 43 | "9\n", 44 | "1 6\n", 45 | "3 5 3 8\n", 46 | "0\n", 47 | "2 1 7\n", 48 | "1 9\n", 49 | "0\n", 50 | "1 2\n", 51 | "0\n", 52 | "0" 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 39, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "9\n", 65 | "1 6\n", 66 | "3 5 3 8\n", 67 | "0\n", 68 | "2 1 7\n", 69 | "1 9\n", 70 | "0\n", 71 | "1 2\n", 72 | "0\n", 73 | "0\n", 74 | "4\n", 75 | "11\n" 76 | ] 77 | } 78 | ], 79 | "source": [ 80 | "n = int(input())\n", 81 | "\n", 82 | "G = {}\n", 83 | "V = set(range(1, n+1))\n", 84 | "\n", 85 | "for i in range(n):\n", 86 | " line = input().split()\n", 87 | " if int(line[0]):\n", 88 | " nodes = set(map(int, line[1:]))\n", 89 | " G[i+1] = nodes\n", 90 | " V = V - nodes\n", 91 | " else:\n", 92 | " G[i+1] = []\n", 93 | " V = V - {i+1}\n", 94 | "\n", 95 | "def h(v):\n", 96 | " if G[v] == []:\n", 97 | " return 0\n", 98 | " return max(map(h, G[v])) + 1\n", 99 | "\n", 100 | "H = sum(map(h, G.keys()))\n", 101 | "print(V.pop())\n", 102 | "print(H)" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 38, 108 | "metadata": {}, 109 | "outputs": [ 110 | { 111 | "data": { 112 | "text/plain": [ 113 | "4" 114 | ] 115 | }, 116 | "execution_count": 38, 117 | "metadata": {}, 118 | "output_type": "execute_result" 119 | } 120 | ], 121 | "source": [ 122 | "V.pop()" 123 | ] 124 | }, 125 | { 126 | "cell_type": "code", 127 | "execution_count": 15, 128 | "metadata": {}, 129 | "outputs": [ 130 | { 131 | "data": { 132 | "text/plain": [ 133 | "{1: {6}, 2: {3, 5, 8}, 3: [], 4: {1, 7}, 5: {9}, 6: [], 7: {2}, 8: [], 9: []}" 134 | ] 135 | }, 136 | "execution_count": 15, 137 | "metadata": {}, 138 | "output_type": "execute_result" 139 | } 140 | ], 141 | "source": [ 142 | "G" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 30, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "def h(v):\n", 152 | " if G[v] == []:\n", 153 | " return 0\n", 154 | " return max(map(h, G[v])) + 1\n", 155 | " \n", 156 | " " 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 35, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "data": { 166 | "text/plain": [ 167 | "11" 168 | ] 169 | }, 170 | "execution_count": 35, 171 | "metadata": {}, 172 | "output_type": "execute_result" 173 | } 174 | ], 175 | "source": [ 176 | "sum(map(h, G.keys()))" 177 | ] 178 | }, 179 | { 180 | "cell_type": "code", 181 | "execution_count": 40, 182 | "metadata": {}, 183 | "outputs": [ 184 | { 185 | "name": "stdout", 186 | "output_type": "stream", 187 | "text": [ 188 | "The following commands were written to file `p3.py`:\n", 189 | "n = int(input())\n", 190 | "\n", 191 | "G = {}\n", 192 | "V = set(range(1, n+1))\n", 193 | "\n", 194 | "for i in range(n):\n", 195 | " line = input().split()\n", 196 | " if int(line[0]):\n", 197 | " nodes = set(map(int, line[1:]))\n", 198 | " G[i+1] = nodes\n", 199 | " V = V - nodes\n", 200 | " else:\n", 201 | " G[i+1] = []\n", 202 | " V = V - {i+1}\n", 203 | "\n", 204 | "def h(v):\n", 205 | " if G[v] == []:\n", 206 | " return 0\n", 207 | " return max(map(h, G[v])) + 1\n", 208 | "\n", 209 | "H = sum(map(h, G.keys()))\n", 210 | "print(V.pop())\n", 211 | "print(H)\n" 212 | ] 213 | } 214 | ], 215 | "source": [ 216 | "%save \"p3.py\" 39" 217 | ] 218 | }, 219 | { 220 | "cell_type": "code", 221 | "execution_count": 41, 222 | "metadata": {}, 223 | "outputs": [ 224 | { 225 | "name": "stdout", 226 | "output_type": "stream", 227 | "text": [ 228 | "5\r\n", 229 | "4\r\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "!python p3.py < p3t01.txt" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": 42, 240 | "metadata": {}, 241 | "outputs": [ 242 | { 243 | "name": "stdout", 244 | "output_type": "stream", 245 | "text": [ 246 | "4\r\n", 247 | "11\r\n" 248 | ] 249 | } 250 | ], 251 | "source": [ 252 | "!python p3.py < p3t02.txt" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": null, 258 | "metadata": {}, 259 | "outputs": [], 260 | "source": [] 261 | }, 262 | { 263 | "cell_type": "code", 264 | "execution_count": null, 265 | "metadata": {}, 266 | "outputs": [], 267 | "source": [] 268 | }, 269 | { 270 | "cell_type": "code", 271 | "execution_count": null, 272 | "metadata": {}, 273 | "outputs": [], 274 | "source": [] 275 | }, 276 | { 277 | "cell_type": "code", 278 | "execution_count": null, 279 | "metadata": {}, 280 | "outputs": [], 281 | "source": [] 282 | }, 283 | { 284 | "cell_type": "code", 285 | "execution_count": null, 286 | "metadata": {}, 287 | "outputs": [], 288 | "source": [] 289 | } 290 | ], 291 | "metadata": { 292 | "kernelspec": { 293 | "display_name": "Python 3", 294 | "language": "python", 295 | "name": "python3" 296 | }, 297 | "language_info": { 298 | "codemirror_mode": { 299 | "name": "ipython", 300 | "version": 3 301 | }, 302 | "file_extension": ".py", 303 | "mimetype": "text/x-python", 304 | "name": "python", 305 | "nbconvert_exporter": "python", 306 | "pygments_lexer": "ipython3", 307 | "version": "3.7.3" 308 | } 309 | }, 310 | "nbformat": 4, 311 | "nbformat_minor": 2 312 | } 313 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p3.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | n = int(input()) 3 | 4 | G = {} 5 | V = set(range(1, n+1)) 6 | 7 | for i in range(n): 8 | line = input().split() 9 | if int(line[0]): 10 | nodes = set(map(int, line[1:])) 11 | G[i+1] = nodes 12 | V = V - nodes 13 | else: 14 | G[i+1] = [] 15 | V = V - {i+1} 16 | 17 | def h(v): 18 | if G[v] == []: 19 | return 0 20 | return max(map(h, G[v])) + 1 21 | 22 | H = sum(map(h, G.keys())) 23 | print(V.pop()) 24 | print(H) 25 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p3t01.txt: -------------------------------------------------------------------------------- 1 | 7 2 | 0 3 | 2 6 7 4 | 2 1 4 5 | 0 6 | 2 3 2 7 | 0 8 | 0 9 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p3t02.txt: -------------------------------------------------------------------------------- 1 | 9 2 | 1 6 3 | 3 5 3 8 4 | 0 5 | 2 1 7 6 | 1 9 7 | 0 8 | 1 2 9 | 0 10 | 0 11 | -------------------------------------------------------------------------------- /APCS 練習區/APCS171028/p4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "from itertools import permutations" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 4, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "data": { 19 | "text/plain": [ 20 | "24" 21 | ] 22 | }, 23 | "execution_count": 4, 24 | "metadata": {}, 25 | "output_type": "execute_result" 26 | } 27 | ], 28 | "source": [ 29 | "len(list(permutations([1,2,3,4], 4)))" 30 | ] 31 | }, 32 | { 33 | "cell_type": "code", 34 | "execution_count": 5, 35 | "metadata": {}, 36 | "outputs": [ 37 | { 38 | "data": { 39 | "text/plain": [ 40 | "24" 41 | ] 42 | }, 43 | "execution_count": 5, 44 | "metadata": {}, 45 | "output_type": "execute_result" 46 | } 47 | ], 48 | "source": [ 49 | "4*3*2*1" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 51, 55 | "metadata": {}, 56 | "outputs": [ 57 | { 58 | "name": "stdout", 59 | "output_type": "stream", 60 | "text": [ 61 | "3\n", 62 | "3 4 5\n", 63 | "1 2 3\n", 64 | "19\n" 65 | ] 66 | } 67 | ], 68 | "source": [ 69 | "n = int(input())\n", 70 | "wline = input().split()\n", 71 | "fline = input().split()\n", 72 | "\n", 73 | "w = lambda x:int(wline[x-1])\n", 74 | "f = lambda x:int(fline[x-1])\n", 75 | "\n", 76 | "Sn = list(permutations(range(1, n+1)))\n", 77 | "\n", 78 | "def energy(p):\n", 79 | " total = 0\n", 80 | " for i in range(1, n):\n", 81 | " weight = sum(map(w, p[:i]))\n", 82 | " total = total + weight * f(p[i])\n", 83 | " return total\n", 84 | "\n", 85 | "print(min(map(energy, Sn)))" 86 | ] 87 | }, 88 | { 89 | "cell_type": "code", 90 | "execution_count": 41, 91 | "metadata": {}, 92 | "outputs": [], 93 | "source": [ 94 | "p = (3,2,1)" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": 44, 100 | "metadata": {}, 101 | "outputs": [ 102 | { 103 | "data": { 104 | "text/plain": [ 105 | "37" 106 | ] 107 | }, 108 | "execution_count": 44, 109 | "metadata": {}, 110 | "output_type": "execute_result" 111 | } 112 | ], 113 | "source": [ 114 | "energy((3,2,1))" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 48, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "data": { 124 | "text/plain": [ 125 | "2" 126 | ] 127 | }, 128 | "execution_count": 48, 129 | "metadata": {}, 130 | "output_type": "execute_result" 131 | } 132 | ], 133 | "source": [ 134 | "f(p[1])" 135 | ] 136 | }, 137 | { 138 | "cell_type": "code", 139 | "execution_count": 46, 140 | "metadata": {}, 141 | "outputs": [ 142 | { 143 | "data": { 144 | "text/plain": [ 145 | "2" 146 | ] 147 | }, 148 | "execution_count": 46, 149 | "metadata": {}, 150 | "output_type": "execute_result" 151 | } 152 | ], 153 | "source": [ 154 | "f(2)" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": 27, 160 | "metadata": {}, 161 | "outputs": [ 162 | { 163 | "data": { 164 | "text/plain": [ 165 | "(1, 2)" 166 | ] 167 | }, 168 | "execution_count": 27, 169 | "metadata": {}, 170 | "output_type": "execute_result" 171 | } 172 | ], 173 | "source": [ 174 | "p[:2]" 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": null, 180 | "metadata": {}, 181 | "outputs": [], 182 | "source": [] 183 | } 184 | ], 185 | "metadata": { 186 | "kernelspec": { 187 | "display_name": "Python 3", 188 | "language": "python", 189 | "name": "python3" 190 | }, 191 | "language_info": { 192 | "codemirror_mode": { 193 | "name": "ipython", 194 | "version": 3 195 | }, 196 | "file_extension": ".py", 197 | "mimetype": "text/x-python", 198 | "name": "python", 199 | "nbconvert_exporter": "python", 200 | "pygments_lexer": "ipython3", 201 | "version": "3.7.3" 202 | } 203 | }, 204 | "nbformat": 4, 205 | "nbformat_minor": 2 206 | } 207 | -------------------------------------------------------------------------------- /APCS 練習區/APCS1806/Untitled.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.7.3" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 2 32 | } 33 | -------------------------------------------------------------------------------- /APCS 練習區/APCS1806/第 1 題 特殊編碼.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## APCS 2018 年 6 月 \n", 8 | "## 第 1 題: 特殊編碼\n", 9 | "\n", 10 | "### 問題描述\n", 11 | "任何文字與數字在電腦中儲存時都是使用二元編碼,而所謂二元編碼也就是一段由 0與 1 構成的序列。在本題中,A~F這六個字元由一種特殊方式來編碼,在這種編碼方式中,這六個字元的編碼都是一個長度為 4 的二元序列,對照表如下:\n", 12 | "\n", 13 | "| 字元 | A | B | C | D | E | F |\n", 14 | "| --- | --- | --- | --- | --- | --- | --- |\n", 15 | "| 編碼 | 0101 | 0111 | 0010 | 1101 | 1000 | 1100 | \n", 16 | "\n", 17 | "請你寫一個程式從編碼辨識這六個字元。\n", 18 | "\n", 19 | "### 輸入格式\n", 20 | "第一行是一個正整數 $N$ , $1 \\leq N \\leq 4$ ,以下有 $N$ 行,每行有 4 個 0 或 1 的數字,數字間彼此以空白隔開,每一行必定是上述六個字元其中之一的編碼。\n", 21 | "\n", 22 | "\n", 23 | "### 輸出格式\n", 24 | "輸出編碼所代表的 $N$ 個字元,字元之間不需要空白或換行間格。\n", 25 | "\n", 26 | "#### 範例一:輸入\n", 27 | " 1\n", 28 | " 0 1 0 1\n", 29 | "\n", 30 | "#### 範例一:正確輸出\n", 31 | " A\n", 32 | "\n", 33 | "#### 範例二:輸入\n", 34 | " 1\n", 35 | " 0 0 1 0\n", 36 | "\n", 37 | "#### 範例二:正確輸出\n", 38 | " C\n", 39 | "\n", 40 | "#### 範例三:輸入\n", 41 | " 2\n", 42 | " 1 0 0 0\n", 43 | " 1 1 0 0\n", 44 | "\n", 45 | "#### 範例三:正確輸出\n", 46 | " EF\n", 47 | "\n", 48 | "#### 範例四:輸入\n", 49 | " 4\n", 50 | " 1 1 0 1\n", 51 | " 1 0 0 0\n", 52 | " 0 1 1 1\n", 53 | " 1 1 0 1\n", 54 | "\n", 55 | "#### 範例四:正確輸出\n", 56 | " DEBD\n", 57 | "\n", 58 | "### 評分說明\n", 59 | "\n", 60 | "輸入包含若干筆測試資料,每一筆測試資料的執行時間限制均為 1 秒,依正確通過測資筆數給分。其中:\n", 61 | "\n", 62 | "第 1 子題組 50 分:$N = 1$。\n", 63 | "\n", 64 | "第 2 子題組 50 分: $N \\leq 4$ 。\n" 65 | ] 66 | } 67 | ], 68 | "metadata": { 69 | "kernelspec": { 70 | "display_name": "Python 3", 71 | "language": "python", 72 | "name": "python3" 73 | }, 74 | "language_info": { 75 | "codemirror_mode": { 76 | "name": "ipython", 77 | "version": 3 78 | }, 79 | "file_extension": ".py", 80 | "mimetype": "text/x-python", 81 | "name": "python", 82 | "nbconvert_exporter": "python", 83 | "pygments_lexer": "ipython3", 84 | "version": "3.7.3" 85 | } 86 | }, 87 | "nbformat": 4, 88 | "nbformat_minor": 2 89 | } 90 | -------------------------------------------------------------------------------- /APCS 練習區/APCS1806/第 2 題 完全奇數.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "## APCS 2018 年 6 月 \n", 8 | "## 第 2 題: 完全奇數\n", 9 | "\n", 10 | "### 問題描述\n", 11 | "如果一個正整數的每ㄧ位數都是奇數時,例如: 7 、 19 、 1759977 等,我們稱這種數字為完全奇數。對於輸入的一正整數 $N$ ,如果 $K$ 是最靠近 $N$ 的完全奇數,請寫一程式找出 $K$ 與 $N$ 之間差距的絕對值,也就是說,請計算並輸出 $| K – N |$。\n", 12 | "\n", 13 | "以 $N = 13256$ 為例,比 13256 大的最小完全奇數是 13311 ,比它小的最大完全奇數是13199,因為 $| 13311 – 13256 | = 55 < | 13256 – 13199 | = 57$,因此輸出 55 。\n", 14 | "\n", 15 | "### 輸入格式\n", 16 | "一個正整數 $N$ , $N < 1018$ 。\n", 17 | "\n", 18 | "### 輸出格式\n", 19 | "輸出 $N$ 與其最近的完全奇數的差距。\n", 20 | "\n", 21 | "#### 範例一:輸入\n", 22 | " 135\n", 23 | "\n", 24 | "#### 範例一:正確輸出\n", 25 | " 0\n", 26 | "\n", 27 | "#### 範例三:輸入\n", 28 | " 35001\n", 29 | "\n", 30 | "#### 範例三:正確輸出\n", 31 | " 110\n", 32 | "\n", 33 | "#### 範例二:輸入\n", 34 | " 13256\n", 35 | "\n", 36 | "#### 範例二:正確輸出\n", 37 | " 55\n", 38 | "\n", 39 | "#### 範例四:輸入\n", 40 | " 1001\n", 41 | "\n", 42 | "#### 範例四:正確輸出\n", 43 | " 2\n", 44 | "\n", 45 | "### 評分說明\n", 46 | "輸入包含若干筆測試資料,每一筆測試資料的執行時間限制均為 1 秒,依正確通過測資筆數給分。其中:\n", 47 | "\n", 48 | "第 1 子題組 20 分:$N < 100$ 。\n", 49 | "\n", 50 | "第 2 子題組 30 分:$N < 106$ 。\n", 51 | "\n", 52 | "第 3 子題組 50 分:$N < 1018$ 。" 53 | ] 54 | } 55 | ], 56 | "metadata": { 57 | "kernelspec": { 58 | "display_name": "Python 3", 59 | "language": "python", 60 | "name": "python3" 61 | }, 62 | "language_info": { 63 | "codemirror_mode": { 64 | "name": "ipython", 65 | "version": 3 66 | }, 67 | "file_extension": ".py", 68 | "mimetype": "text/x-python", 69 | "name": "python", 70 | "nbconvert_exporter": "python", 71 | "pygments_lexer": "ipython3", 72 | "version": "3.7.3" 73 | } 74 | }, 75 | "nbformat": 4, 76 | "nbformat_minor": 2 77 | } 78 | -------------------------------------------------------------------------------- /Python 問題集/我的、你的、我們的紀念日.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "我們來寫個程式, 算算日子。發揮你的創意, 可以寫出很多有趣的程式。\n", 8 | "\n", 9 | "【提示】\n", 10 | "在 Python 中有個標準的 `datetime` 模組, 專門處理時間相關的。我們可以知道今天是幾月幾號、可以做日子的加減... " 11 | ] 12 | }, 13 | { 14 | "cell_type": "markdown", 15 | "metadata": {}, 16 | "source": [ 17 | "### 問題 1: 我們到今天交往多久?\n", 18 | "\n", 19 | "想想, 如果在任何一天, 你可以告訴你親愛的, 到今天交往 (結婚) 第幾天, 那是多麼浪漫?\n", 20 | "\n", 21 | "【輸入】\n", 22 | "你們開始交往 (或結婚) 的日子。\n", 23 | "\n", 24 | "【輸出】\n", 25 | "到今天是第幾天。" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "### 問題 2: 我的、你的、我們的第一萬天\n", 33 | "\n", 34 | "你想知道, 你生命中的第一萬天是哪一天? 或是, 你的某個重要日子的第一萬天?\n", 35 | "\n", 36 | "【輸入】\n", 37 | "某個人的生日。\n", 38 | "\n", 39 | "【輸出】\n", 40 | "那個人生命的第一萬天。" 41 | ] 42 | } 43 | ], 44 | "metadata": { 45 | "kernelspec": { 46 | "display_name": "Python 3", 47 | "language": "python", 48 | "name": "python3" 49 | }, 50 | "language_info": { 51 | "codemirror_mode": { 52 | "name": "ipython", 53 | "version": 3 54 | }, 55 | "file_extension": ".py", 56 | "mimetype": "text/x-python", 57 | "name": "python", 58 | "nbconvert_exporter": "python", 59 | "pygments_lexer": "ipython3", 60 | "version": "3.7.3" 61 | } 62 | }, 63 | "nbformat": 4, 64 | "nbformat_minor": 2 65 | } 66 | -------------------------------------------------------------------------------- /Python 問題集/會寫詩的文青機器人.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "我們讓 Python 寫首詩吧。\n", 8 | "\n", 9 | "去收集一些你覺得容易在詩裡出現的字眼, 比如\n", 10 | "\n", 11 | " words='''\n", 12 | " 我\n", 13 | " 我的\n", 14 | " 妳\n", 15 | " 妳的\n", 16 | " 心\n", 17 | " 溫柔\n", 18 | " 日子\n", 19 | " 雨\n", 20 | " 風\n", 21 | " 天空\n", 22 | " 雲\n", 23 | " 等待\n", 24 | " 哭泣\n", 25 | " 戀愛\n", 26 | " 相遇\n", 27 | " 分離\n", 28 | " 忘記\n", 29 | " 心醉\n", 30 | " 驀然\n", 31 | " 吹過\n", 32 | " 思念\n", 33 | " 靈魂\n", 34 | " 停止'''\n", 35 | " \n", 36 | "接著, 你可以亂數決定幾句的詩, 比如說 2-7 句。同時也可以亂數決定每句各用多少詞, 再隨機選取你選的字..." 37 | ] 38 | }, 39 | { 40 | "cell_type": "markdown", 41 | "metadata": {}, 42 | "source": [ 43 | "【Python 的作詩範例】\n", 44 | "\n", 45 | " ※\n", 46 | " 我的 戀愛 \n", 47 | " 忘記 心醉 哭泣 \n", 48 | "\n", 49 | " ※\n", 50 | " 妳 忘記 我 思念 \n", 51 | " 雨 風 我的 等待\n", 52 | "\n", 53 | " ※\n", 54 | " 妳的 天空 驀然 \n", 55 | " 忘記 相遇 哭泣\n", 56 | "\n", 57 | " ※\n", 58 | " 相遇 吹過 靈魂 \n", 59 | " 哭泣 思念 \n", 60 | " 心 停止 " 61 | ] 62 | } 63 | ], 64 | "metadata": { 65 | "kernelspec": { 66 | "display_name": "Python 3", 67 | "language": "python", 68 | "name": "python3" 69 | }, 70 | "language_info": { 71 | "codemirror_mode": { 72 | "name": "ipython", 73 | "version": 3 74 | }, 75 | "file_extension": ".py", 76 | "mimetype": "text/x-python", 77 | "name": "python", 78 | "nbconvert_exporter": "python", 79 | "pygments_lexer": "ipython3", 80 | "version": "3.7.3" 81 | } 82 | }, 83 | "nbformat": 4, 84 | "nbformat_minor": 2 85 | } 86 | -------------------------------------------------------------------------------- /Python 問題集/猜數字遊戲.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "我們來和電腦玩玩猜數字遊戲。" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### 問題 1. 基本款\n", 15 | "\n", 16 | "電腦先「想」一個 1~100 間的一個數字。然後你來猜電腦的數字, 太大或太小電腦會告訴你, 直到你猜對為止。\n", 17 | "\n", 18 | "【變化型】\n", 19 | "假設電腦心中想的是 87。\n", 20 | "\n", 21 | "* 第一次你猜 50, 電腦會告訴你 51-100\n", 22 | "* 第二次猜 75, 電腦會告訴你 76-100\n", 23 | "* 第三次猜 90, 電腦會告訴你 76-89\n", 24 | "\n", 25 | "等等。\n" 26 | ] 27 | }, 28 | { 29 | "cell_type": "markdown", 30 | "metadata": {}, 31 | "source": [ 32 | "### 問題 2. 幾A幾B\n", 33 | "\n", 34 | "這是比較困難的問題, 就是和電腦玩「幾A幾B」這種猜數字。\n", 35 | "\n", 36 | "【提示】\n", 37 | "我們可以先用「各個擊破」的方式, 先寫一些小程式。\n", 38 | "\n", 39 | "* 寫一個程式, 能讓電腦產生四個 0-9 不重覆數字。\n", 40 | "* 寫一個程式, 設 x 是使用者猜的答案, y 是正確答案, 然後可以判斷這個 x 是幾 A 幾 B。\n", 41 | "* 最後, 可以讓電腦也猜你的數字!" 42 | ] 43 | } 44 | ], 45 | "metadata": { 46 | "kernelspec": { 47 | "display_name": "Python 3", 48 | "language": "python", 49 | "name": "python3" 50 | }, 51 | "language_info": { 52 | "codemirror_mode": { 53 | "name": "ipython", 54 | "version": 3 55 | }, 56 | "file_extension": ".py", 57 | "mimetype": "text/x-python", 58 | "name": "python", 59 | "nbconvert_exporter": "python", 60 | "pygments_lexer": "ipython3", 61 | "version": "3.7.3" 62 | } 63 | }, 64 | "nbformat": 4, 65 | "nbformat_minor": 2 66 | } 67 | -------------------------------------------------------------------------------- /Python 問題集/考拉茲猜想(冰雹數列).ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "所謂考拉茲猜想 (Collatz conjecture), 又稱為 $3n+1$ 猜想、冰雹數列猜想等等, 是指給了一個起始的正整數 $a_1$, 用以下的規則產生下一個數。假設我們已經有 $a_n$ 了, 那麼\n", 8 | "\n", 9 | "$$a_{n+1} = \\begin{cases}a_n/2, & \\mbox{若 $a_n$ 為偶數}\\\\\n", 10 | "3a_n + 1, & \\mbox{若 $a_n$ 為奇數}\\end{cases} \\mbox{。}$$\n", 11 | "\n", 12 | "考拉茲猜想就是說, 這個數字最終都會得到 $1$。" 13 | ] 14 | }, 15 | { 16 | "cell_type": "markdown", 17 | "metadata": {}, 18 | "source": [ 19 | "### 問題: 寫個程式來計算冰雹數列\n", 20 | "\n", 21 | "【輸入】\n", 22 | "一個正整數。\n", 23 | "\n", 24 | "【輸出】\n", 25 | "冰雹數列, 到 1 的時候停止。\n", 26 | "\n", 27 | "【變型】\n", 28 | "1. 可以用一些創意方式表示冰雹數列, 如把圖畫出來!\n", 29 | "2. 也可以輸入 $i$, $j$, 找出從 $i$ 到 $j$, 哪個數字為起始值時, 冰雹數列的長度最長!" 30 | ] 31 | } 32 | ], 33 | "metadata": { 34 | "kernelspec": { 35 | "display_name": "Python 3", 36 | "language": "python", 37 | "name": "python3" 38 | }, 39 | "language_info": { 40 | "codemirror_mode": { 41 | "name": "ipython", 42 | "version": 3 43 | }, 44 | "file_extension": ".py", 45 | "mimetype": "text/x-python", 46 | "name": "python", 47 | "nbconvert_exporter": "python", 48 | "pygments_lexer": "ipython3", 49 | "version": "3.7.3" 50 | } 51 | }, 52 | "nbformat": 4, 53 | "nbformat_minor": 2 54 | } 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## 政治大學 2019 資訊第二專長班 2 | ## 「程式設計課程」課程大綱 3 | 4 | ### 課程大綱 5 | 課程的目標是使同學具備程式設計能力, 我們主要使用當今流行的 Python 程式語言, 但課程會介紹如何看懂 C 語言。寫程式需要有應用才能引發興趣, 我們以當紅的「數據分析」做為我們主要的應用。課程內容主要包括三個部份: 6 | 7 | 1. Python 基本資料型態介紹、流程控制、輸出/輸入、迴圈、函式、檔案處理、基礎類別等等。 8 | 2. Python 數據分析相關內容, 包括 numpy, sympy, matplotlib, pandas, scikit learn 等套件介紹。 9 | 3. 有趣的程式教學, 如 Micro:bit 等等。 10 | 11 | ### 教師與助教 12 | 13 | #### 蔡炎龍老師 14 | 美國爾灣加州大學 (UC Irvine) 數學博士, 曾任政治大學教發中心數位學習組組長、副學務長, 現任新生書院總導師、數理資訊學程召集人。推廣 Python 程式語言超過 10 年, 目前專注在人工智慧相關應用及教學。 15 | 16 | * email: yenlung@nccu.edu.tw 17 | * 辦公室: 果夫樓二樓上樓右轉第一間 18 | * Facebook: fb.me/yenlung 19 | 20 | #### 賴彥文助教 21 | 政治大學應用數學系學士、中興大學應用數學系碩士, 資深 iOS Web 開發工程師, 現在專研 Python 在人工智慧的應用及 Python 教學, 發明 YW 程式語言學習法。 22 | 23 | * email: owl3032@gmail.com 24 | * Facebook: fb.me/yanwen.lai 25 | 26 | ### 課程內容 27 | 28 | 1. Python 程式的安裝 29 | 2. Jupyter Notebook 主流數據分析平台介絡 30 | 3. Python 當成電子計算機! 31 | 4. 和使用者互動的輸入輸出 32 | 5. 電腦判斷的基礎 33 | 6. 迴圈的兩種型式 34 | 7. 函式的寫法 35 | 8. 串列再深度一點的討論 36 | 9. 基本的物件導向 37 | 10. Python 畫圖套件 matplotlib 38 | 11. Sympy 來做學校的數學 39 | 12. 像是 Excel 的 pandas 40 | 13. 基本機器學習概念 41 | 14. 網頁 HTML + CSS 基本概念 42 | 15. Git/GitHub 基本概念 43 | 44 | ### 課程評分 45 | 1. 出席狀況: 40% 46 | 2. 程式分數: 60% 47 | 48 | ### 程式分數說明 49 | 1. 每週至少要交 4 個作業 (每天一程式的概念) 50 | 2. 分數計點法: 指定網頁問題 x 1, 自己出題 x2, 分享概念 x 3 51 | 52 | ### 重要: 程式設計期末考試說明 53 | 本課程需通過大學程式設計先修測驗 (APCS)。8 月 26 日開放報名, 10 月 26 日考試。 54 | 55 | https://apcs.csie.ntnu.edu.tw/ 56 | 57 | APCS統一考試, 包含C、C++、Java、Python四種語言, 總級分10級。 58 | 59 | 1. 通過:6級分~10級分,可修習暑期課程及(線上)資料結構。 60 | 2. 有條件通過:4級分~5級分或單科達3級分,需重複參加測驗,直至通過為止,可修習暑期課程及(線上)資料結構。 61 | 3. 不通過:1級分~3級分,需重修(線上)程式設計課程並重複參加測驗,直至通過為止,尚不可修習暑期課程及(線上)資料結構。 62 | 63 | ### 學習 Python 64 | 65 | #### 選一本書 66 | 學習 Python 可以在書店找一本你看來順眼的 Python 書就可以了。 67 | 68 | #### https://www.w3schools.com/python 69 | W3Schools 做了很多網頁相關 HTML, CSS, JavaSctipt 教學等等, 做得非常好。現在也有 Python 教學了! 70 | 71 | #### 線上課程 72 | 可參考老師在 moocs.nccu.edu.tw 的「成為 Python 數據分析達人的第一門課」。 73 | 74 | ### 練習資源 75 | 可以在以下練習資源中尋找練習的問題,有些資料有提供 online judge 的服務。 76 | 77 | 78 | #### ITSA 自學平台 79 | http://e-tutor.itsa.org.tw/e-Tutor/ 80 | 81 | 這裡用學校的 email 就可以註冊, 由許多老師提供問題集。 82 | 83 | #### UVa Online Judge 84 | https://onlinejudge.org/ 85 | 86 | 老牌的 online judge, 有很多試合挑戰學習的問題。 87 | 88 | 高雄中學李青育老師有個收集了 UVa 問題的中文翻譯。 89 | 90 | http://luckycat.kshs.kh.edu.tw/ 91 | 92 | 93 | #### LeetCode 94 | 95 | https://leetcode.com/problemset/all/ 96 | 97 | 開可以專注在 Easy 的問題, 這是寫程式的人很喜歡練習的地方。 -------------------------------------------------------------------------------- /YW式APCS專區/105-106考題/實作題/1050305APCSImplementation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yenlung/Python-Programming/885d2615f565fa55af2246613fe7fba8f2f83f9e/YW式APCS專區/105-106考題/實作題/1050305APCSImplementation.pdf -------------------------------------------------------------------------------- /YW式APCS專區/105-106考題/實作題/1051029APCSImplementation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yenlung/Python-Programming/885d2615f565fa55af2246613fe7fba8f2f83f9e/YW式APCS專區/105-106考題/實作題/1051029APCSImplementation.pdf -------------------------------------------------------------------------------- /YW式APCS專區/105-106考題/實作題/1060304APCSImplementation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yenlung/Python-Programming/885d2615f565fa55af2246613fe7fba8f2f83f9e/YW式APCS專區/105-106考題/實作題/1060304APCSImplementation.pdf -------------------------------------------------------------------------------- /YW式APCS專區/105-106考題/實作題/1061028APCSImplementation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yenlung/Python-Programming/885d2615f565fa55af2246613fe7fba8f2f83f9e/YW式APCS專區/105-106考題/實作題/1061028APCSImplementation.pdf -------------------------------------------------------------------------------- /YW式APCS專區/105-106考題/實作題/10706APCSImplementation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yenlung/Python-Programming/885d2615f565fa55af2246613fe7fba8f2f83f9e/YW式APCS專區/105-106考題/實作題/10706APCSImplementation.pdf -------------------------------------------------------------------------------- /YW式APCS專區/105-106考題/觀念題/1050305APCSconcept.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yenlung/Python-Programming/885d2615f565fa55af2246613fe7fba8f2f83f9e/YW式APCS專區/105-106考題/觀念題/1050305APCSconcept.pdf -------------------------------------------------------------------------------- /YW式APCS專區/105-106考題/觀念題/1051029APCSconcept.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yenlung/Python-Programming/885d2615f565fa55af2246613fe7fba8f2f83f9e/YW式APCS專區/105-106考題/觀念題/1051029APCSconcept.pdf -------------------------------------------------------------------------------- /YW式APCS專區/105-106考題/觀念題/1060304APCSconcept.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yenlung/Python-Programming/885d2615f565fa55af2246613fe7fba8f2f83f9e/YW式APCS專區/105-106考題/觀念題/1060304APCSconcept.pdf -------------------------------------------------------------------------------- /YW式APCS專區/常用語法與常用模型.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "先筆記一些看到的點,待補上。\n", 8 | "\n", 9 | "* 空list或temp list\n", 10 | "* input直接轉list\n" 11 | ] 12 | }, 13 | { 14 | "cell_type": "code", 15 | "execution_count": null, 16 | "metadata": {}, 17 | "outputs": [], 18 | "source": [] 19 | } 20 | ], 21 | "metadata": { 22 | "kernelspec": { 23 | "display_name": "Python 3", 24 | "language": "python", 25 | "name": "python3" 26 | }, 27 | "language_info": { 28 | "codemirror_mode": { 29 | "name": "ipython", 30 | "version": 3 31 | }, 32 | "file_extension": ".py", 33 | "mimetype": "text/x-python", 34 | "name": "python", 35 | "nbconvert_exporter": "python", 36 | "pygments_lexer": "ipython3", 37 | "version": "3.6.4" 38 | } 39 | }, 40 | "nbformat": 4, 41 | "nbformat_minor": 2 42 | } 43 | -------------------------------------------------------------------------------- /YW式APCS專區/觀念解釋7_24.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 105-3-1\n", 8 | " ## 關於變數宣告\n", 9 | " * c 語言需要先宣變內容,例如 int k = 4\n", 10 | " * python不需要,因此直接令變數 k = 4\n", 11 | " ## 關於 for 迴圈\n", 12 | " * c 的for 需要說明迴圈變數啟始值、結束條件、迴圈變數變化規則\n", 13 | " * python 可以for in來代換\n", 14 | " ## 關於print換行\n", 15 | " * c 在進行print的時候不會自動換行,所以在需要換行的時候需要執行 printf(\"\\n\")\n", 16 | " * python 預設會換行,所以不想換行的時候需要告知結尾(end=''), 取代\"\\n\",因此python中不換行的print會如下\n", 17 | " print(\"輸出內容\", end='')" 18 | ] 19 | }, 20 | { 21 | "cell_type": "code", 22 | "execution_count": 4, 23 | "metadata": {}, 24 | "outputs": [ 25 | { 26 | "name": "stdout", 27 | "output_type": "stream", 28 | "text": [ 29 | "----*\n", 30 | "---***\n", 31 | "--*****\n", 32 | "-*******\n", 33 | "*********\n" 34 | ] 35 | } 36 | ], 37 | "source": [ 38 | "k = 4\n", 39 | "m = 1\n", 40 | "\n", 41 | "for i in range(1, 5+1):\n", 42 | " for j in range(1, k+1):\n", 43 | " print(\"-\", end='') #為了可以更清楚印出來的內容,故以短線取代一個空格\n", 44 | " \n", 45 | " for j in range(1, m+1):\n", 46 | " print('*', end='')\n", 47 | " \n", 48 | " print()\n", 49 | " k = k-1\n", 50 | "# m = m+1 #原本題目的寫法\n", 51 | " m = m+2 #修正後可以正確印出的寫法\n", 52 | " " 53 | ] 54 | }, 55 | { 56 | "cell_type": "code", 57 | "execution_count": 1, 58 | "metadata": {}, 59 | "outputs": [ 60 | { 61 | "name": "stdout", 62 | "output_type": "stream", 63 | "text": [ 64 | "0\n", 65 | "1\n", 66 | "2\n", 67 | "2\n" 68 | ] 69 | } 70 | ], 71 | "source": [ 72 | "for i in range(3):\n", 73 | " print(i)\n", 74 | " \n", 75 | "print(i)" 76 | ] 77 | }, 78 | { 79 | "cell_type": "code", 80 | "execution_count": 7, 81 | "metadata": {}, 82 | "outputs": [], 83 | "source": [ 84 | "def f(a = [], n = 1):\n", 85 | " index = 0\n", 86 | " for i in range(1, n):\n", 87 | " if a[i] >= a[index]:\n", 88 | " index = i\n", 89 | " \n", 90 | " return index" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 8, 96 | "metadata": {}, 97 | "outputs": [ 98 | { 99 | "data": { 100 | "text/plain": [ 101 | "7" 102 | ] 103 | }, 104 | "execution_count": 8, 105 | "metadata": {}, 106 | "output_type": "execute_result" 107 | } 108 | ], 109 | "source": [ 110 | "L = [1, 3, 9, 2, 5, 8, 4, 9, 6, 7]\n", 111 | "f(L, 10)" 112 | ] 113 | }, 114 | { 115 | "cell_type": "markdown", 116 | "metadata": {}, 117 | "source": [ 118 | "# 105-3_3" 119 | ] 120 | }, 121 | { 122 | "cell_type": "code", 123 | "execution_count": 1, 124 | "metadata": {}, 125 | "outputs": [], 126 | "source": [ 127 | "def f1(a, value):\n", 128 | " r_value = -1\n", 129 | " i = 0\n", 130 | "\n", 131 | " ###\n", 132 | " counter = 0\n", 133 | " ###\n", 134 | "\n", 135 | " while i < 100 :\n", 136 | " ###\n", 137 | " counter += 1\n", 138 | " ### \n", 139 | " if a[i] == value:\n", 140 | " r_value = i\n", 141 | " \n", 142 | " ###\n", 143 | " print(f'n1 = {counter}')\n", 144 | " ###\n", 145 | " \n", 146 | " break\n", 147 | " \n", 148 | " i += 1\n", 149 | " \n", 150 | " return r_value" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": 2, 156 | "metadata": {}, 157 | "outputs": [], 158 | "source": [ 159 | "def f2(a, value):\n", 160 | " r_value = -1\n", 161 | " low = 0\n", 162 | " high = 99\n", 163 | " mid = 0\n", 164 | " \n", 165 | " ###\n", 166 | " counter = 0\n", 167 | " ###\n", 168 | " \n", 169 | " while (low <= high) :\n", 170 | " mid = (low + high)//2\n", 171 | " \n", 172 | " ###\n", 173 | " counter += 1\n", 174 | " ###\n", 175 | " \n", 176 | " if (a[mid] == value) :\n", 177 | " r_value = mid\n", 178 | " \n", 179 | " ###\n", 180 | " print(f'n2 = {counter}')\n", 181 | " ###\n", 182 | "\n", 183 | " break\n", 184 | " elif (a[mid] < value) :\n", 185 | " low = mid + 1\n", 186 | " else :\n", 187 | " high = mid - 1\n", 188 | " \n", 189 | " return r_value " 190 | ] 191 | }, 192 | { 193 | "cell_type": "code", 194 | "execution_count": 3, 195 | "metadata": {}, 196 | "outputs": [ 197 | { 198 | "data": { 199 | "text/plain": [ 200 | "100" 201 | ] 202 | }, 203 | "execution_count": 3, 204 | "metadata": {}, 205 | "output_type": "execute_result" 206 | } 207 | ], 208 | "source": [ 209 | "a = range(1, 300, 3)\n", 210 | "len(a)\n", 211 | "# for i in a:\n", 212 | "# print(i)" 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": 4, 218 | "metadata": {}, 219 | "outputs": [ 220 | { 221 | "name": "stdout", 222 | "output_type": "stream", 223 | "text": [ 224 | "n1 = 100\n", 225 | "n2 = 7\n", 226 | "r1 = 99, r2 = 99\n" 227 | ] 228 | } 229 | ], 230 | "source": [ 231 | "r1 = f1(a, 298)\n", 232 | "r2 = f2(a, 298)\n", 233 | "print(f'r1 = {r1}, r2 = {r2}')" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": null, 239 | "metadata": {}, 240 | "outputs": [], 241 | "source": [] 242 | }, 243 | { 244 | "cell_type": "markdown", 245 | "metadata": {}, 246 | "source": [ 247 | "# 4" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": 36, 253 | "metadata": {}, 254 | "outputs": [ 255 | { 256 | "name": "stdout", 257 | "output_type": "stream", 258 | "text": [ 259 | "810\n" 260 | ] 261 | } 262 | ], 263 | "source": [ 264 | "b = list(range(101))\n", 265 | "\n", 266 | "a = [0]\n", 267 | "for i in range(1, 100+1):\n", 268 | " a.append(b[i] + a[i-1])\n", 269 | "\n", 270 | "print(a[50]- a[30])" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 37, 276 | "metadata": {}, 277 | "outputs": [ 278 | { 279 | "name": "stdout", 280 | "output_type": "stream", 281 | "text": [ 282 | "810\n" 283 | ] 284 | } 285 | ], 286 | "source": [ 287 | "b = list(range(101))\n", 288 | "a = list(range(101))\n", 289 | "for i in range(1, 100+1):\n", 290 | " a[i] = b[i] + a[i-1]\n", 291 | "\n", 292 | "print(a[50]- a[30])" 293 | ] 294 | }, 295 | { 296 | "cell_type": "code", 297 | "execution_count": 38, 298 | "metadata": {}, 299 | "outputs": [ 300 | { 301 | "name": "stdout", 302 | "output_type": "stream", 303 | "text": [ 304 | "[]\n", 305 | "[0]\n", 306 | "[0, 1]\n", 307 | "[0, 1, 2]\n", 308 | "[0, 1, 2, 3]\n", 309 | "[0, 1, 2, 3, 4]\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "a = []\n", 315 | "print(a)\n", 316 | "\n", 317 | "for i in range(5):\n", 318 | " a.append(i)\n", 319 | " print(a)" 320 | ] 321 | }, 322 | { 323 | "cell_type": "code", 324 | "execution_count": 39, 325 | "metadata": {}, 326 | "outputs": [ 327 | { 328 | "name": "stdout", 329 | "output_type": "stream", 330 | "text": [ 331 | "[0, 0, 0, 0, 0]\n", 332 | "[0, 0, 0, 0, 0]\n", 333 | "[0, 1, 0, 0, 0]\n", 334 | "[0, 1, 2, 0, 0]\n", 335 | "[0, 1, 2, 3, 0]\n", 336 | "[0, 1, 2, 3, 4]\n" 337 | ] 338 | } 339 | ], 340 | "source": [ 341 | "a = [0, 0, 0, 0, 0]\n", 342 | "print(a)\n", 343 | "for i in range(5):\n", 344 | " a[i] = i\n", 345 | " print(a)" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 40, 351 | "metadata": {}, 352 | "outputs": [ 353 | { 354 | "data": { 355 | "text/plain": [ 356 | "[0, 0, 0, 0, 0]" 357 | ] 358 | }, 359 | "execution_count": 40, 360 | "metadata": {}, 361 | "output_type": "execute_result" 362 | } 363 | ], 364 | "source": [ 365 | "[0] * 5" 366 | ] 367 | }, 368 | { 369 | "cell_type": "code", 370 | "execution_count": null, 371 | "metadata": {}, 372 | "outputs": [], 373 | "source": [] 374 | } 375 | ], 376 | "metadata": { 377 | "kernelspec": { 378 | "display_name": "Python 3", 379 | "language": "python", 380 | "name": "python3" 381 | }, 382 | "language_info": { 383 | "codemirror_mode": { 384 | "name": "ipython", 385 | "version": 3 386 | }, 387 | "file_extension": ".py", 388 | "mimetype": "text/x-python", 389 | "name": "python", 390 | "nbconvert_exporter": "python", 391 | "pygments_lexer": "ipython3", 392 | "version": "3.6.4" 393 | } 394 | }, 395 | "nbformat": 4, 396 | "nbformat_minor": 2 397 | } 398 | -------------------------------------------------------------------------------- /YW式APCS專區/觀念解釋7_25.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 5\n", 8 | "\n", 9 | "呼叫 $f(1000)$ 時 $sum = sum + i$ 被執行的次數最接近何者" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 10, 15 | "metadata": {}, 16 | "outputs": [ 17 | { 18 | "name": "stdout", 19 | "output_type": "stream", 20 | "text": [ 21 | "f(1000) = 900336, count = 2980\n" 22 | ] 23 | } 24 | ], 25 | "source": [ 26 | "count = 0\n", 27 | "def f(n):\n", 28 | " global count #使用global來宣告此變數為外部變數\n", 29 | " if n < 2 :\n", 30 | " return 0\n", 31 | " \n", 32 | " sum=0\n", 33 | " for i in range(1, n+1):\n", 34 | " count = count+ 1\n", 35 | " sum = sum + i\n", 36 | " \n", 37 | " newN = (2*n)//3\n", 38 | " sum = sum + f(newN)\n", 39 | " \n", 40 | " return sum\n", 41 | "\n", 42 | "n = 1000\n", 43 | "result = f(n)\n", 44 | "print(f'f({n}) = {result}, count = {count}')" 45 | ] 46 | }, 47 | { 48 | "cell_type": "markdown", 49 | "metadata": {}, 50 | "source": [ 51 | "# 7\n", 52 | "\n", 53 | "$a(13, 15)$ 的回傳值會是多少?" 54 | ] 55 | }, 56 | { 57 | "cell_type": "code", 58 | "execution_count": 11, 59 | "metadata": {}, 60 | "outputs": [ 61 | { 62 | "data": { 63 | "text/plain": [ 64 | "103" 65 | ] 66 | }, 67 | "execution_count": 11, 68 | "metadata": {}, 69 | "output_type": "execute_result" 70 | } 71 | ], 72 | "source": [ 73 | "def a(n=0, m=0):\n", 74 | " if n < 10:\n", 75 | " if m < 10 :\n", 76 | " return n + m\n", 77 | " else :\n", 78 | " return a(n, m - 2) + m\n", 79 | " else :\n", 80 | " return a(n - 1, m) + n\n", 81 | " \n", 82 | "a(13, 15)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "# 8\n", 90 | " \n", 91 | " 計算費式數列,空格該填入什麼?" 92 | ] 93 | }, 94 | { 95 | "cell_type": "code", 96 | "execution_count": 13, 97 | "metadata": {}, 98 | "outputs": [ 99 | { 100 | "name": "stdout", 101 | "output_type": "stream", 102 | "text": [ 103 | "1\n", 104 | "2\n", 105 | "3\n", 106 | "5\n", 107 | "8\n", 108 | "13\n", 109 | "21\n", 110 | "34\n", 111 | "55\n" 112 | ] 113 | } 114 | ], 115 | "source": [ 116 | "a = 0\n", 117 | "b = 1\n", 118 | "i = 0\n", 119 | "temp = 0\n", 120 | "N = 10\n", 121 | "\n", 122 | "\n", 123 | "for i in range(2, N+1):\n", 124 | " temp = b\n", 125 | " b = b + a\n", 126 | " a = temp\n", 127 | " \n", 128 | "# a, b = b, a+b #此行可以完全取代上面四行\n", 129 | " print(b)" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "# 9\n", 137 | " 輸出為何?\n", 138 | " 以下為分解寫法" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": 1, 144 | "metadata": {}, 145 | "outputs": [ 146 | { 147 | "data": { 148 | "text/plain": [ 149 | "[0, 0, 0, 0, 0]" 150 | ] 151 | }, 152 | "execution_count": 1, 153 | "metadata": {}, 154 | "output_type": "execute_result" 155 | } 156 | ], 157 | "source": [ 158 | "A =[0] *5\n", 159 | "A" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 2, 165 | "metadata": {}, 166 | "outputs": [ 167 | { 168 | "data": { 169 | "text/plain": [ 170 | "[0]" 171 | ] 172 | }, 173 | "execution_count": 2, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "B = [0]\n", 180 | "B" 181 | ] 182 | }, 183 | { 184 | "cell_type": "code", 185 | "execution_count": 4, 186 | "metadata": {}, 187 | "outputs": [], 188 | "source": [ 189 | "for i in range(1, 4+1):\n", 190 | " A[i] = 2 + i*4\n", 191 | " B.append(i*5)" 192 | ] 193 | }, 194 | { 195 | "cell_type": "code", 196 | "execution_count": 5, 197 | "metadata": {}, 198 | "outputs": [ 199 | { 200 | "data": { 201 | "text/plain": [ 202 | "[0, 6, 10, 14, 18]" 203 | ] 204 | }, 205 | "execution_count": 5, 206 | "metadata": {}, 207 | "output_type": "execute_result" 208 | } 209 | ], 210 | "source": [ 211 | "A" 212 | ] 213 | }, 214 | { 215 | "cell_type": "code", 216 | "execution_count": 6, 217 | "metadata": {}, 218 | "outputs": [ 219 | { 220 | "data": { 221 | "text/plain": [ 222 | "[0, 5, 10, 15, 20]" 223 | ] 224 | }, 225 | "execution_count": 6, 226 | "metadata": {}, 227 | "output_type": "execute_result" 228 | } 229 | ], 230 | "source": [ 231 | "B" 232 | ] 233 | }, 234 | { 235 | "cell_type": "code", 236 | "execution_count": 7, 237 | "metadata": {}, 238 | "outputs": [ 239 | { 240 | "name": "stdout", 241 | "output_type": "stream", 242 | "text": [ 243 | "4\n" 244 | ] 245 | } 246 | ], 247 | "source": [ 248 | "c =0\n", 249 | "for i in range(1, 5):\n", 250 | " if B[i] > A[i]:\n", 251 | " c = c+ (B[i] % A[i])\n", 252 | " else:\n", 253 | " c = 1\n", 254 | "print(c)" 255 | ] 256 | }, 257 | { 258 | "cell_type": "markdown", 259 | "metadata": {}, 260 | "source": [ 261 | " 以下為合併同cell寫法" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": 14, 267 | "metadata": {}, 268 | "outputs": [ 269 | { 270 | "name": "stdout", 271 | "output_type": "stream", 272 | "text": [ 273 | "4\n" 274 | ] 275 | } 276 | ], 277 | "source": [ 278 | "A =[0] *5\n", 279 | "B = [0]\n", 280 | "i = 0\n", 281 | "c = 0\n", 282 | "\n", 283 | "for i in range(1, 4+1):\n", 284 | " A[i] = 2 + i*4\n", 285 | " B.append(i*5)\n", 286 | " \n", 287 | "for i in range(1, 5):\n", 288 | " if B[i] > A[i]:\n", 289 | " c = c+ (B[i] % A[i])\n", 290 | " else:\n", 291 | " c = 1\n", 292 | "print(c)" 293 | ] 294 | }, 295 | { 296 | "cell_type": "markdown", 297 | "metadata": {}, 298 | "source": [ 299 | "# 10\n", 300 | "\n", 301 | "請問 $g(13)$ 回傳值為何?" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": 15, 307 | "metadata": {}, 308 | "outputs": [ 309 | { 310 | "data": { 311 | "text/plain": [ 312 | "19" 313 | ] 314 | }, 315 | "execution_count": 15, 316 | "metadata": {}, 317 | "output_type": "execute_result" 318 | } 319 | ], 320 | "source": [ 321 | "def g(a = 0):\n", 322 | " if a > 1:\n", 323 | " return g(a - 2) + 3\n", 324 | " else :\n", 325 | " return a\n", 326 | " \n", 327 | "g(13)" 328 | ] 329 | }, 330 | { 331 | "cell_type": "markdown", 332 | "metadata": {}, 333 | "source": [ 334 | "# 11\n", 335 | "\n", 336 | "$a[n]$ 為一陣列,若要將 $a[0]$ 移到 $a[n-1]$, $i$ 的範圍是多少? " 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": 19, 342 | "metadata": {}, 343 | "outputs": [ 344 | { 345 | "name": "stdout", 346 | "output_type": "stream", 347 | "text": [ 348 | "i=0, a = [1, 0, 2, 3, 4, 5, 6, 7, 8, 9]\n", 349 | "i=1, a = [1, 2, 0, 3, 4, 5, 6, 7, 8, 9]\n", 350 | "i=2, a = [1, 2, 3, 0, 4, 5, 6, 7, 8, 9]\n", 351 | "i=3, a = [1, 2, 3, 4, 0, 5, 6, 7, 8, 9]\n", 352 | "i=4, a = [1, 2, 3, 4, 5, 0, 6, 7, 8, 9]\n", 353 | "i=5, a = [1, 2, 3, 4, 5, 6, 0, 7, 8, 9]\n", 354 | "i=6, a = [1, 2, 3, 4, 5, 6, 7, 0, 8, 9]\n", 355 | "i=7, a = [1, 2, 3, 4, 5, 6, 7, 8, 0, 9]\n", 356 | "i=8, a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]\n" 357 | ] 358 | } 359 | ], 360 | "source": [ 361 | "i = 0\n", 362 | "hold = 0\n", 363 | "n = 0\n", 364 | "\n", 365 | "a = list(range(10))# 為了測試,隨意造了一個串列 a\n", 366 | "n = len(a) #先取得串列長度,以利後續決定for 的次數。\n", 367 | "\n", 368 | "for i in range(n - 2 + 1): \n", 369 | " hold = a[i]\n", 370 | " a[i] = a[i+1]\n", 371 | " a[i+1] = hold\n", 372 | " \n", 373 | " print(f'i={i}, a = {a}')#此行原文當中沒有,但印出a可以看到最前面的元素不斷後移動過程" 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "execution_count": null, 379 | "metadata": {}, 380 | "outputs": [], 381 | "source": [] 382 | } 383 | ], 384 | "metadata": { 385 | "kernelspec": { 386 | "display_name": "Python 3", 387 | "language": "python", 388 | "name": "python3" 389 | }, 390 | "language_info": { 391 | "codemirror_mode": { 392 | "name": "ipython", 393 | "version": 3 394 | }, 395 | "file_extension": ".py", 396 | "mimetype": "text/x-python", 397 | "name": "python", 398 | "nbconvert_exporter": "python", 399 | "pygments_lexer": "ipython3", 400 | "version": "3.6.4" 401 | } 402 | }, 403 | "nbformat": 4, 404 | "nbformat_minor": 2 405 | } 406 | -------------------------------------------------------------------------------- /YW式APCS專區/觀念解釋7_29.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "# 106_3-6\n", 8 | " 原題直譯" 9 | ] 10 | }, 11 | { 12 | "cell_type": "code", 13 | "execution_count": 1, 14 | "metadata": {}, 15 | "outputs": [], 16 | "source": [ 17 | "def main(A = [[]]):\n", 18 | " M = len(A)\n", 19 | " N = len(A[0])\n", 20 | " \n", 21 | " rowsum = 0\n", 22 | " for i in range(M):\n", 23 | " rowsum = 0\n", 24 | " for j in range(N):\n", 25 | " rowsum = rowsum + A[i][j] \n", 26 | "\n", 27 | " print(f\"The sum of row {i} is {rowsum}.\")" 28 | ] 29 | }, 30 | { 31 | "cell_type": "code", 32 | "execution_count": 2, 33 | "metadata": {}, 34 | "outputs": [ 35 | { 36 | "name": "stdout", 37 | "output_type": "stream", 38 | "text": [ 39 | "The sum of row 2 is 11.\n" 40 | ] 41 | } 42 | ], 43 | "source": [ 44 | "# 造一個雙層的串列來表示矩陣,以下範例為3x2\n", 45 | "B = [[1, 2], [3, 4], [5, 6]]\n", 46 | "\n", 47 | "# 將此矩陣輸入到main裡計算,可以看出rowsome無法正確印出每一列的和。\n", 48 | "main(B)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "markdown", 53 | "metadata": {}, 54 | "source": [ 55 | " 以下修正成符合成題器描述的需求,印出每一列的和。\n", 56 | " 同時直接將main當成主程式進入點(不再特地定義一個main)\n", 57 | " 因此,需要被計算的矩陣A需要在前面先定義好,執行到for的時候才有內容可以計算" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": 3, 63 | "metadata": {}, 64 | "outputs": [ 65 | { 66 | "name": "stdout", 67 | "output_type": "stream", 68 | "text": [ 69 | "The sum of row 0 is 3.\n", 70 | "The sum of row 1 is 7.\n", 71 | "The sum of row 2 is 11.\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "A = [[1, 2], [3, 4], [5, 6]]\n", 77 | "\n", 78 | "M = len(A)\n", 79 | "N = len(A[0])\n", 80 | "\n", 81 | "rowsum = 0 #此行移不移除都不影響\n", 82 | "for i in range(M):\n", 83 | " rowsum = 0 #原程式無法正確執行主要是沒有這一行\n", 84 | " for j in range(N):\n", 85 | " rowsum = rowsum + A[i][j]\n", 86 | " print(f\"The sum of row {i} is {rowsum}.\")" 87 | ] 88 | }, 89 | { 90 | "cell_type": "markdown", 91 | "metadata": {}, 92 | "source": [ 93 | "## 105-3_17\n", 94 | "* #define TRUE 1 :表示定義一個新“常數”, TRUE 代表 1\n", 95 | "* #define FALSE 1 :表示定義一個新“常數”, FALSE 代表 0" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": 4, 101 | "metadata": {}, 102 | "outputs": [ 103 | { 104 | "data": { 105 | "text/plain": [ 106 | "True" 107 | ] 108 | }, 109 | "execution_count": 4, 110 | "metadata": {}, 111 | "output_type": "execute_result" 112 | } 113 | ], 114 | "source": [ 115 | "True == 1" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 5, 121 | "metadata": {}, 122 | "outputs": [ 123 | { 124 | "data": { 125 | "text/plain": [ 126 | "True" 127 | ] 128 | }, 129 | "execution_count": 5, 130 | "metadata": {}, 131 | "output_type": "execute_result" 132 | } 133 | ], 134 | "source": [ 135 | "False == 0" 136 | ] 137 | }, 138 | { 139 | "cell_type": "markdown", 140 | "metadata": {}, 141 | "source": [ 142 | " 以上邏輯中可以看出來,在python裡的 True 本身等於1, False等於 0,故毋須另外定義。" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": null, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "d = list(range(6))\n", 152 | "val = 0\n", 153 | "allBig = 0\n", 154 | "\n", 155 | "for i in range(1, 5+1):\n", 156 | " d[i] = int(input(\"please key some integer:\"))\n", 157 | "val = int(input('please key some integer:'))\n", 158 | "\n", 159 | "allBig = True\n", 160 | "\n", 161 | "for i in range(1, 5+1):\n", 162 | " if d[i] > val:\n", 163 | " allBig = True\n", 164 | " else:\n", 165 | " allBig = False\n", 166 | " \n", 167 | "if allBig == True:\n", 168 | " print(f'{val} is the smallest.')\n", 169 | "else:\n", 170 | " print(f'{val} is not the smallest.')" 171 | ] 172 | }, 173 | { 174 | "cell_type": "markdown", 175 | "metadata": {}, 176 | "source": [ 177 | " 上面翻譯完成後,會發現原題目第二組答案會不符點,該如何修正呢?" 178 | ] 179 | }, 180 | { 181 | "cell_type": "code", 182 | "execution_count": 8, 183 | "metadata": {}, 184 | "outputs": [ 185 | { 186 | "name": "stdout", 187 | "output_type": "stream", 188 | "text": [ 189 | "First, default d = [0, 1, 2, 3, 4, 5]\n", 190 | "please key some integer:11\n", 191 | "please key some integer:12\n", 192 | "please key some integer:13\n", 193 | "please key some integer:14\n", 194 | "please key some integer:20\n", 195 | "please key some integer:3\n", 196 | "When input finished, d = [0, 11, 12, 13, 14, 20], val = 3\n", 197 | "d[1] = 11, val = 3, allBig = True\n", 198 | "d[2] = 12, val = 3, allBig = True\n", 199 | "d[3] = 13, val = 3, allBig = True\n", 200 | "d[4] = 14, val = 3, allBig = True\n", 201 | "d[5] = 20, val = 3, allBig = True\n", 202 | "3 is the smallest.\n" 203 | ] 204 | } 205 | ], 206 | "source": [ 207 | "d = list(range(6))\n", 208 | "val = 0\n", 209 | "allBig = 0\n", 210 | "\n", 211 | "print(f\"First, default d = {d}\")\n", 212 | "for i in range(1, 5+1):\n", 213 | " d[i] = int(input(\"please key some integer:\"))\n", 214 | "val = int(input('please key some integer:'))\n", 215 | "\n", 216 | "print(f\"When input finished, d = {d}, val = {val}\")\n", 217 | "\n", 218 | "allBig = True\n", 219 | "for i in range(1, 5+1):\n", 220 | " if d[i] < val: \n", 221 | " allBig = False\n", 222 | " break #只要有一個d[i] < val,那麼val就不是最小的,那麼就可以跳迴圈了\n", 223 | " print(f'd[{i}] = {d[i]}, val = {val}, allBig = {allBig}')\n", 224 | "\n", 225 | "if allBig == True:\n", 226 | " print(f'{val} is the smallest.')\n", 227 | "else:\n", 228 | " print(f'{val} is not the smallest.')" 229 | ] 230 | }, 231 | { 232 | "cell_type": "markdown", 233 | "metadata": {}, 234 | "source": [ 235 | "原題目的輸入是否太麻煩?改成我們熟悉一次輸入一串(每一個用空格格開),\n", 236 | "(而且想要的話,還可以不限長度,在此不示範,但是想一下要多改些什麼就可以由程式連動。)\n", 237 | "\n", 238 | "為了配合原題目後續變數與index範圍,我們只更改輸入方式,但會依然保留d裡頭有6格,最後一個輸入的放入val。" 239 | ] 240 | }, 241 | { 242 | "cell_type": "code", 243 | "execution_count": 10, 244 | "metadata": {}, 245 | "outputs": [ 246 | { 247 | "name": "stdout", 248 | "output_type": "stream", 249 | "text": [ 250 | "請輸入6個不重覆整數,每個整數以空格分開11 12 13 14 30 20\n", 251 | "d = [0, 11, 12, 13, 14, 30], val = 20\n" 252 | ] 253 | } 254 | ], 255 | "source": [ 256 | "d = [0] + list(map(int, input(\"請輸入6個不重覆整數,每個整數以空格分開\").split()))\n", 257 | "val = d.pop()\n", 258 | "allBig = 0\n", 259 | "\n", 260 | "print(f\"d = {d}, val = {val}\")" 261 | ] 262 | }, 263 | { 264 | "cell_type": "markdown", 265 | "metadata": {}, 266 | "source": [ 267 | "以上少少幾行改掉了原先的輸入程式" 268 | ] 269 | }, 270 | { 271 | "cell_type": "markdown", 272 | "metadata": {}, 273 | "source": [ 274 | "# 106-3_8\n", 275 | " 善用print的方式來追蹤自己程式的進行方式" 276 | ] 277 | }, 278 | { 279 | "cell_type": "code", 280 | "execution_count": 5, 281 | "metadata": {}, 282 | "outputs": [ 283 | { 284 | "name": "stdout", 285 | "output_type": "stream", 286 | "text": [ 287 | "1 before fucntion add,\n", 288 | "6 a = 1\n", 289 | "7 a = 1 after s=s+1\n", 290 | "7 a = 0\n", 291 | "8 a = 0 after s=s+1\n", 292 | "1 after function add,\n", 293 | "9 after s = 9,\n" 294 | ] 295 | } 296 | ], 297 | "source": [ 298 | "s = 1\n", 299 | "\n", 300 | "def add(a = 0):\n", 301 | " s = 6\n", 302 | " \n", 303 | " for a in range(a, -1, -1):\n", 304 | " print(f\"{s} a = {a}\")\n", 305 | "# print(f\"{s},\", end='')\n", 306 | " s = s+1\n", 307 | " print(f\"{s} a = {a} after s=s+1\")\n", 308 | "# print(f'{s},', end='')\n", 309 | " \n", 310 | "print(f'{s} before fucntion add,')\n", 311 | "# print(f'{s},', end='')\n", 312 | "add(s)\n", 313 | "print(f'{s} after function add,')\n", 314 | "# print(f'{s},', end='')\n", 315 | "s = 9\n", 316 | "print(f'{s} after s = 9,')\n", 317 | "# print(f'{s},', end='')" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": null, 323 | "metadata": {}, 324 | "outputs": [], 325 | "source": [] 326 | } 327 | ], 328 | "metadata": { 329 | "kernelspec": { 330 | "display_name": "Python 3", 331 | "language": "python", 332 | "name": "python3" 333 | }, 334 | "language_info": { 335 | "codemirror_mode": { 336 | "name": "ipython", 337 | "version": 3 338 | }, 339 | "file_extension": ".py", 340 | "mimetype": "text/x-python", 341 | "name": "python", 342 | "nbconvert_exporter": "python", 343 | "pygments_lexer": "ipython3", 344 | "version": "3.6.4" 345 | } 346 | }, 347 | "nbformat": 4, 348 | "nbformat_minor": 2 349 | } 350 | -------------------------------------------------------------------------------- /YW式APCS專區/觀念解釋7_30.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 3, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "5\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "b = randint(3, 10)\n", 18 | "print(b)\n" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 1, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "4\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "from random import randint\n", 36 | "\n", 37 | "a = randint(3, 10)\n", 38 | "print(a)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": null, 44 | "metadata": {}, 45 | "outputs": [], 46 | "source": [ 47 | "try:\n", 48 | "\n", 49 | "except:" 50 | ] 51 | }, 52 | { 53 | "cell_type": "code", 54 | "execution_count": 36, 55 | "metadata": {}, 56 | "outputs": [], 57 | "source": [ 58 | "def divided(a, b):\n", 59 | " print(f\"a/b = {a/b}\")\n", 60 | " print('謝謝使用')" 61 | ] 62 | }, 63 | { 64 | "cell_type": "code", 65 | "execution_count": 37, 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "ename": "ZeroDivisionError", 70 | "evalue": "division by zero", 71 | "output_type": "error", 72 | "traceback": [ 73 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 74 | "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", 75 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdivided\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 76 | "\u001b[0;32m\u001b[0m in \u001b[0;36mdivided\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mdivided\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"a/b = {a/b}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'謝謝使用'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 77 | "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" 78 | ] 79 | } 80 | ], 81 | "source": [ 82 | "divided(5, 0)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 38, 88 | "metadata": {}, 89 | "outputs": [], 90 | "source": [ 91 | "def dividedPlus(a, b):\n", 92 | " try:\n", 93 | " print(f\"a/b = {a/b}\")\n", 94 | " except:\n", 95 | " print(f\"b 不能給0哦!\")\n", 96 | "\n", 97 | " print('謝謝使用')" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 34, 103 | "metadata": {}, 104 | "outputs": [ 105 | { 106 | "name": "stdout", 107 | "output_type": "stream", 108 | "text": [ 109 | "a/b = 0.5\n" 110 | ] 111 | } 112 | ], 113 | "source": [ 114 | "dividedPlus(5,10)" 115 | ] 116 | }, 117 | { 118 | "cell_type": "code", 119 | "execution_count": 39, 120 | "metadata": {}, 121 | "outputs": [ 122 | { 123 | "name": "stdout", 124 | "output_type": "stream", 125 | "text": [ 126 | "b 不能給0哦!\n", 127 | "謝謝使用\n" 128 | ] 129 | } 130 | ], 131 | "source": [ 132 | "dividedPlus(5,0)" 133 | ] 134 | }, 135 | { 136 | "cell_type": "code", 137 | "execution_count": 40, 138 | "metadata": {}, 139 | "outputs": [], 140 | "source": [ 141 | "def dividedPlusReturn(a, b):\n", 142 | " try:\n", 143 | " return a/b\n", 144 | " except:\n", 145 | " print(f\"b 不能給0哦!\")" 146 | ] 147 | }, 148 | { 149 | "cell_type": "code", 150 | "execution_count": 47, 151 | "metadata": {}, 152 | "outputs": [], 153 | "source": [ 154 | "def dividedPlusPrint(a, b):\n", 155 | " try:\n", 156 | " print(f\"我們得到{a/b}\")\n", 157 | " except:\n", 158 | " print(f\"b 不能給0哦!\")" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": 42, 164 | "metadata": {}, 165 | "outputs": [ 166 | { 167 | "data": { 168 | "text/plain": [ 169 | "0.5" 170 | ] 171 | }, 172 | "execution_count": 42, 173 | "metadata": {}, 174 | "output_type": "execute_result" 175 | } 176 | ], 177 | "source": [ 178 | "dividedPlusReturn(1, 2)" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": 43, 184 | "metadata": {}, 185 | "outputs": [ 186 | { 187 | "name": "stdout", 188 | "output_type": "stream", 189 | "text": [ 190 | "0.5\n" 191 | ] 192 | } 193 | ], 194 | "source": [ 195 | "dividedPlusPrint(1, 2)" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 44, 201 | "metadata": {}, 202 | "outputs": [], 203 | "source": [ 204 | "result = dividedPlusReturn(1, 2)" 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": 53, 210 | "metadata": {}, 211 | "outputs": [ 212 | { 213 | "data": { 214 | "text/plain": [ 215 | "1.5" 216 | ] 217 | }, 218 | "execution_count": 53, 219 | "metadata": {}, 220 | "output_type": "execute_result" 221 | } 222 | ], 223 | "source": [ 224 | "result + 1 " 225 | ] 226 | }, 227 | { 228 | "cell_type": "code", 229 | "execution_count": 48, 230 | "metadata": {}, 231 | "outputs": [ 232 | { 233 | "name": "stdout", 234 | "output_type": "stream", 235 | "text": [ 236 | "我們得到0.5\n" 237 | ] 238 | } 239 | ], 240 | "source": [ 241 | "result_ = dividedPlusPrint(1, 2)" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 54, 247 | "metadata": {}, 248 | "outputs": [ 249 | { 250 | "ename": "TypeError", 251 | "evalue": "unsupported operand type(s) for +: 'NoneType' and 'int'", 252 | "output_type": "error", 253 | "traceback": [ 254 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 255 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 256 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mresult_\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", 257 | "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'NoneType' and 'int'" 258 | ] 259 | } 260 | ], 261 | "source": [ 262 | "result_ + 4" 263 | ] 264 | }, 265 | { 266 | "cell_type": "code", 267 | "execution_count": 51, 268 | "metadata": {}, 269 | "outputs": [], 270 | "source": [ 271 | "def S(s = 0):\n", 272 | " if s >= 90:\n", 273 | " print('A')\n", 274 | " elif s >= 80:\n", 275 | " print(\"B\")\n", 276 | " elif s > 60:\n", 277 | " print('D')\n", 278 | " elif s > 70:\n", 279 | " print(\"C\")\n", 280 | " else :\n", 281 | " print(\"F\")" 282 | ] 283 | }, 284 | { 285 | "cell_type": "code", 286 | "execution_count": 52, 287 | "metadata": {}, 288 | "outputs": [ 289 | { 290 | "name": "stdout", 291 | "output_type": "stream", 292 | "text": [ 293 | "If your score = 0, your will get S-Rating: F\n", 294 | "If your score = 1, your will get S-Rating: F\n", 295 | "If your score = 2, your will get S-Rating: F\n", 296 | "If your score = 3, your will get S-Rating: F\n", 297 | "If your score = 4, your will get S-Rating: F\n", 298 | "If your score = 5, your will get S-Rating: F\n", 299 | "If your score = 6, your will get S-Rating: F\n", 300 | "If your score = 7, your will get S-Rating: F\n", 301 | "If your score = 8, your will get S-Rating: F\n", 302 | "If your score = 9, your will get S-Rating: F\n", 303 | "If your score = 10, your will get S-Rating: F\n", 304 | "If your score = 11, your will get S-Rating: F\n", 305 | "If your score = 12, your will get S-Rating: F\n", 306 | "If your score = 13, your will get S-Rating: F\n", 307 | "If your score = 14, your will get S-Rating: F\n", 308 | "If your score = 15, your will get S-Rating: F\n", 309 | "If your score = 16, your will get S-Rating: F\n", 310 | "If your score = 17, your will get S-Rating: F\n", 311 | "If your score = 18, your will get S-Rating: F\n", 312 | "If your score = 19, your will get S-Rating: F\n", 313 | "If your score = 20, your will get S-Rating: F\n", 314 | "If your score = 21, your will get S-Rating: F\n", 315 | "If your score = 22, your will get S-Rating: F\n", 316 | "If your score = 23, your will get S-Rating: F\n", 317 | "If your score = 24, your will get S-Rating: F\n", 318 | "If your score = 25, your will get S-Rating: F\n", 319 | "If your score = 26, your will get S-Rating: F\n", 320 | "If your score = 27, your will get S-Rating: F\n", 321 | "If your score = 28, your will get S-Rating: F\n", 322 | "If your score = 29, your will get S-Rating: F\n", 323 | "If your score = 30, your will get S-Rating: F\n", 324 | "If your score = 31, your will get S-Rating: F\n", 325 | "If your score = 32, your will get S-Rating: F\n", 326 | "If your score = 33, your will get S-Rating: F\n", 327 | "If your score = 34, your will get S-Rating: F\n", 328 | "If your score = 35, your will get S-Rating: F\n", 329 | "If your score = 36, your will get S-Rating: F\n", 330 | "If your score = 37, your will get S-Rating: F\n", 331 | "If your score = 38, your will get S-Rating: F\n", 332 | "If your score = 39, your will get S-Rating: F\n", 333 | "If your score = 40, your will get S-Rating: F\n", 334 | "If your score = 41, your will get S-Rating: F\n", 335 | "If your score = 42, your will get S-Rating: F\n", 336 | "If your score = 43, your will get S-Rating: F\n", 337 | "If your score = 44, your will get S-Rating: F\n", 338 | "If your score = 45, your will get S-Rating: F\n", 339 | "If your score = 46, your will get S-Rating: F\n", 340 | "If your score = 47, your will get S-Rating: F\n", 341 | "If your score = 48, your will get S-Rating: F\n", 342 | "If your score = 49, your will get S-Rating: F\n", 343 | "If your score = 50, your will get S-Rating: F\n", 344 | "If your score = 51, your will get S-Rating: F\n", 345 | "If your score = 52, your will get S-Rating: F\n", 346 | "If your score = 53, your will get S-Rating: F\n", 347 | "If your score = 54, your will get S-Rating: F\n", 348 | "If your score = 55, your will get S-Rating: F\n", 349 | "If your score = 56, your will get S-Rating: F\n", 350 | "If your score = 57, your will get S-Rating: F\n", 351 | "If your score = 58, your will get S-Rating: F\n", 352 | "If your score = 59, your will get S-Rating: F\n", 353 | "If your score = 60, your will get S-Rating: F\n", 354 | "If your score = 61, your will get S-Rating: D\n", 355 | "If your score = 62, your will get S-Rating: D\n", 356 | "If your score = 63, your will get S-Rating: D\n", 357 | "If your score = 64, your will get S-Rating: D\n", 358 | "If your score = 65, your will get S-Rating: D\n", 359 | "If your score = 66, your will get S-Rating: D\n", 360 | "If your score = 67, your will get S-Rating: D\n", 361 | "If your score = 68, your will get S-Rating: D\n", 362 | "If your score = 69, your will get S-Rating: D\n", 363 | "If your score = 70, your will get S-Rating: D\n", 364 | "If your score = 71, your will get S-Rating: D\n", 365 | "If your score = 72, your will get S-Rating: D\n", 366 | "If your score = 73, your will get S-Rating: D\n", 367 | "If your score = 74, your will get S-Rating: D\n", 368 | "If your score = 75, your will get S-Rating: D\n", 369 | "If your score = 76, your will get S-Rating: D\n", 370 | "If your score = 77, your will get S-Rating: D\n", 371 | "If your score = 78, your will get S-Rating: D\n", 372 | "If your score = 79, your will get S-Rating: D\n", 373 | "If your score = 80, your will get S-Rating: B\n", 374 | "If your score = 81, your will get S-Rating: B\n", 375 | "If your score = 82, your will get S-Rating: B\n", 376 | "If your score = 83, your will get S-Rating: B\n", 377 | "If your score = 84, your will get S-Rating: B\n", 378 | "If your score = 85, your will get S-Rating: B\n", 379 | "If your score = 86, your will get S-Rating: B\n", 380 | "If your score = 87, your will get S-Rating: B\n", 381 | "If your score = 88, your will get S-Rating: B\n", 382 | "If your score = 89, your will get S-Rating: B\n", 383 | "If your score = 90, your will get S-Rating: A\n", 384 | "If your score = 91, your will get S-Rating: A\n", 385 | "If your score = 92, your will get S-Rating: A\n", 386 | "If your score = 93, your will get S-Rating: A\n", 387 | "If your score = 94, your will get S-Rating: A\n", 388 | "If your score = 95, your will get S-Rating: A\n", 389 | "If your score = 96, your will get S-Rating: A\n", 390 | "If your score = 97, your will get S-Rating: A\n", 391 | "If your score = 98, your will get S-Rating: A\n", 392 | "If your score = 99, your will get S-Rating: A\n", 393 | "If your score = 100, your will get S-Rating: A\n" 394 | ] 395 | } 396 | ], 397 | "source": [ 398 | "def SPlus(s = 0):\n", 399 | " if s >= 90:\n", 400 | " return 'A'\n", 401 | " elif s >= 80:\n", 402 | " return 'B'\n", 403 | " elif s > 60:\n", 404 | " return \"D\"\n", 405 | " elif s > 70:\n", 406 | " return \"C\"\n", 407 | " else :\n", 408 | " return \"F\"\n", 409 | "\n", 410 | "\n", 411 | "scoreSet = list(range(101))\n", 412 | "for score in scoreSet:\n", 413 | " SRating = SPlus(score)\n", 414 | " print('If your score = {}, your will get S-Rating: {}'.format(score, SRating))" 415 | ] 416 | }, 417 | { 418 | "cell_type": "code", 419 | "execution_count": 55, 420 | "metadata": {}, 421 | "outputs": [ 422 | { 423 | "data": { 424 | "text/plain": [ 425 | "0" 426 | ] 427 | }, 428 | "execution_count": 55, 429 | "metadata": {}, 430 | "output_type": "execute_result" 431 | } 432 | ], 433 | "source": [ 434 | "round(0.5)" 435 | ] 436 | }, 437 | { 438 | "cell_type": "code", 439 | "execution_count": 56, 440 | "metadata": {}, 441 | "outputs": [ 442 | { 443 | "data": { 444 | "text/plain": [ 445 | "2" 446 | ] 447 | }, 448 | "execution_count": 56, 449 | "metadata": {}, 450 | "output_type": "execute_result" 451 | } 452 | ], 453 | "source": [ 454 | "round(1.5)" 455 | ] 456 | }, 457 | { 458 | "cell_type": "code", 459 | "execution_count": 57, 460 | "metadata": {}, 461 | "outputs": [ 462 | { 463 | "data": { 464 | "text/plain": [ 465 | "2" 466 | ] 467 | }, 468 | "execution_count": 57, 469 | "metadata": {}, 470 | "output_type": "execute_result" 471 | } 472 | ], 473 | "source": [ 474 | "round(2.5)" 475 | ] 476 | }, 477 | { 478 | "cell_type": "code", 479 | "execution_count": 58, 480 | "metadata": {}, 481 | "outputs": [ 482 | { 483 | "data": { 484 | "text/plain": [ 485 | "4" 486 | ] 487 | }, 488 | "execution_count": 58, 489 | "metadata": {}, 490 | "output_type": "execute_result" 491 | } 492 | ], 493 | "source": [ 494 | "round(4.5)" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": null, 500 | "metadata": {}, 501 | "outputs": [], 502 | "source": [] 503 | } 504 | ], 505 | "metadata": { 506 | "kernelspec": { 507 | "display_name": "Python 3", 508 | "language": "python", 509 | "name": "python3" 510 | }, 511 | "language_info": { 512 | "codemirror_mode": { 513 | "name": "ipython", 514 | "version": 3 515 | }, 516 | "file_extension": ".py", 517 | "mimetype": "text/x-python", 518 | "name": "python", 519 | "nbconvert_exporter": "python", 520 | "pygments_lexer": "ipython3", 521 | "version": "3.6.4" 522 | } 523 | }, 524 | "nbformat": 4, 525 | "nbformat_minor": 2 526 | } 527 | -------------------------------------------------------------------------------- /YW式APCS專區/觀念解釋7_31.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [] 9 | } 10 | ], 11 | "metadata": { 12 | "kernelspec": { 13 | "display_name": "Python 3", 14 | "language": "python", 15 | "name": "python3" 16 | }, 17 | "language_info": { 18 | "codemirror_mode": { 19 | "name": "ipython", 20 | "version": 3 21 | }, 22 | "file_extension": ".py", 23 | "mimetype": "text/x-python", 24 | "name": "python", 25 | "nbconvert_exporter": "python", 26 | "pygments_lexer": "ipython3", 27 | "version": "3.6.4" 28 | } 29 | }, 30 | "nbformat": 4, 31 | "nbformat_minor": 2 32 | } 33 | -------------------------------------------------------------------------------- /YW式APCS專區/觀念解釋8_1.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "data": { 10 | "text/plain": [ 11 | "'12345'" 12 | ] 13 | }, 14 | "execution_count": 1, 15 | "metadata": {}, 16 | "output_type": "execute_result" 17 | } 18 | ], 19 | "source": [ 20 | "st = '12345'\n", 21 | "st[:10]" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 4, 27 | "metadata": {}, 28 | "outputs": [ 29 | { 30 | "data": { 31 | "text/plain": [ 32 | "['av',\n", 33 | " 'ds',\n", 34 | " 'fe',\n", 35 | " 'qw',\n", 36 | " 'fv',\n", 37 | " 'c4',\n", 38 | " 'q3',\n", 39 | " 'r4',\n", 40 | " 'qf',\n", 41 | " 'as',\n", 42 | " 'df',\n", 43 | " 'as',\n", 44 | " 'df',\n", 45 | " '4f',\n", 46 | " 'ad',\n", 47 | " 'sf',\n", 48 | " 'ad',\n", 49 | " 'sf',\n", 50 | " 'a']" 51 | ] 52 | }, 53 | "execution_count": 4, 54 | "metadata": {}, 55 | "output_type": "execute_result" 56 | } 57 | ], 58 | "source": [ 59 | "st='avdsfeqwfvc4q3r4qfasdfasdf4fadsfadsfa'\n", 60 | "\n", 61 | "[st[i:i+2] for i in range(0, len(st), 2)]" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 5, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "data": { 71 | "text/plain": [ 72 | "dict" 73 | ] 74 | }, 75 | "execution_count": 5, 76 | "metadata": {}, 77 | "output_type": "execute_result" 78 | } 79 | ], 80 | "source": [ 81 | "S = {}\n", 82 | "type(S)" 83 | ] 84 | }, 85 | { 86 | "cell_type": "code", 87 | "execution_count": 7, 88 | "metadata": {}, 89 | "outputs": [ 90 | { 91 | "ename": "AttributeError", 92 | "evalue": "'dict' object has no attribute 'add'", 93 | "output_type": "error", 94 | "traceback": [ 95 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 96 | "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", 97 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mS\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m9\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mS\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 98 | "\u001b[0;31mAttributeError\u001b[0m: 'dict' object has no attribute 'add'" 99 | ] 100 | } 101 | ], 102 | "source": [ 103 | "S.add(9)\n", 104 | "S" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": null, 110 | "metadata": {}, 111 | "outputs": [], 112 | "source": [] 113 | } 114 | ], 115 | "metadata": { 116 | "kernelspec": { 117 | "display_name": "Python 3", 118 | "language": "python", 119 | "name": "python3" 120 | }, 121 | "language_info": { 122 | "codemirror_mode": { 123 | "name": "ipython", 124 | "version": 3 125 | }, 126 | "file_extension": ".py", 127 | "mimetype": "text/x-python", 128 | "name": "python", 129 | "nbconvert_exporter": "python", 130 | "pygments_lexer": "ipython3", 131 | "version": "3.6.4" 132 | } 133 | }, 134 | "nbformat": 4, 135 | "nbformat_minor": 2 136 | } 137 | -------------------------------------------------------------------------------- /guessing_game.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | from random import randint 3 | def game(): 4 | ans = randint(1, 100) 5 | guess = -1 6 | 7 | while guess != ans: 8 | guess = int(input("請輸入一個數字: ")) 9 | 10 | if guess>ans: 11 | print("太大了!") 12 | elif guess