├── README.md
├── Unit1_code.ipynb
├── Unit2_code.ipynb
├── Unit3_code.ipynb
├── Unit4_code.ipynb
├── Unit5_code.ipynb
├── Unit6_code.ipynb
├── Unit7_code.ipynb
├── Unit7_code_Project_1008_幾個水桶.ipynb
└── Unit7_費氏數列.ipynb
/README.md:
--------------------------------------------------------------------------------
1 | # LearningNote-ALG101-too-weak-to-leetcode
2 | 學習筆記-Lidemy [ALG101] 先別急著寫 leetcode課程
3 |
4 | 這裡紀錄學習這門課的code 筆記和心得,code使用Python撰寫
5 |
6 | 課程連結 https://lidemy.com/courses/793973/lectures/14419433
7 |
8 | ## Code
9 | - [Unit 1 要學好程式,從不要寫程式開始](https://github.com/SYkkk55/LearningNote-ALG101-too-weak-to-leetcode/blob/main/Unit1_code.py) OK
10 |
11 | - [Unit 2 寫程式之前,先學會「看程式」](https://github.com/SYkkk55/LearningNote-ALG101-too-weak-to-leetcode/blob/main/Unit2_code.py) OK
12 |
13 | - [Unit 3 寫程式前的最後一步:看懂題目](https://github.com/SYkkk55/LearningNote-ALG101-too-weak-to-leetcode/blob/main/Unit3_code.ipynb)OK
14 |
15 | - [Unit 4 主角總是最後才登場:寫程式囉](https://github.com/SYkkk55/LearningNote-ALG101-too-weak-to-leetcode/blob/main/Unit4_code.ipynb)OK
16 |
17 | - [Unit 5 經典題目解解看](https://github.com/SYkkk55/LearningNote-ALG101-too-weak-to-leetcode/blob/main/Unit5_code.ipynb) OK
18 | - 身分證驗證
19 | - 數字各位數加總
20 | - LIOJ 1025 等比數列
21 | - LIOJ 1027 信用卡驗證
22 | - LIOJ 1028 生命靈數
23 | - LIOJ 1029 加減乘除
24 | - LIOJ 1030 判斷迴文
25 | - LIOJ 1031 完全平方和
26 | - LIOJ 1032 平面距離計算
27 | - LIOJ 1023 最近點對
28 | - LIOJ 1034 凱薩加密
29 | - LIOJ 1046 圈圈叉叉
30 |
31 |
32 |
33 | - [Unit 6 內建函式做做看](https://github.com/SYkkk55/Learning-ALG101-too-weak-to-leetcode-Note/blob/main/Unit6_code.ipynb) OK
34 |
35 | - [Unit 7 國中題目大挑戰](https://github.com/SweetornotspicyMarathon/Learning-ALG101-too-weak-to-leetcode-Note-main/blob/main/Unit7_code.ipynb) OK
36 |
37 | - [Unit7_code_Project_1008_幾個水桶](https://github.com/SweetornotspicyMarathon/Learning-ALG101-too-weak-to-leetcode-Note-main/blob/main/Unit7_code_Project_1008_%E5%B9%BE%E5%80%8B%E6%B0%B4%E6%A1%B6.ipynb) OK
38 |
39 | - [Unit7_費氏數列](https://github.com/SweetornotspicyMarathon/Learning-ALG101-too-weak-to-leetcode-Note-main/blob/main/Unit7_%E8%B2%BB%E6%B0%8F%E6%95%B8%E5%88%97.ipynb) OK
40 |
41 | - [Unit 8 初學者只管拿分,誰管你什麼效率]()
42 |
43 | - [Unit 9 未來的路還很漫長,你還差得遠呢]()
44 |
45 |
46 | ## Medium
47 | - [[Lidemy 學習筆記]-先別急著寫 leetcode Unit0](https://sweetornotspicymarathon.medium.com/lidemy-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%88%E5%88%A5%E6%80%A5%E8%91%97%E5%AF%AB-leetcode-unit0-bae0b56f229c)
48 |
49 | - [[Lidemy 學習筆記]-先別急著寫 leetcode Unit1//教練,我想要寫Code](https://sweetornotspicymarathon.medium.com/lidemy-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%88%E5%88%A5%E6%80%A5%E8%91%97%E5%AF%AB-leetcode-unit1-%E6%95%99%E7%B7%B4-%E6%88%91%E6%83%B3%E8%A6%81%E5%AF%ABcode-3fc0f89a7cb0)
50 |
51 | - [[Lidemy 學習筆記]-先別急著寫 leetcode Project1//實作:字串反轉、陣列總和、找最大值](https://sweetornotspicymarathon.medium.com/lidemy-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%88%E5%88%A5%E6%80%A5%E8%91%97%E5%AF%AB-leetcode-project1-%E5%AF%A6%E4%BD%9C-%E5%AD%97%E4%B8%B2%E5%8F%8D%E8%BD%89-%E9%99%A3%E5%88%97%E7%B8%BD%E5%92%8C-%E6%89%BE%E6%9C%80%E5%A4%A7%E5%80%BC-39854b0863ce)
52 |
53 | - [[Lidemy 學習筆記]-先別急著寫 leetcode Project2//實作:找陣列次小值、大小寫互換、印出因數](https://sweetornotspicymarathon.medium.com/lidemy-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%88%E5%88%A5%E6%80%A5%E8%91%97%E5%AF%AB-leetcode-project2-%E5%AF%A6%E4%BD%9C-%E6%89%BE%E9%99%A3%E5%88%97%E6%AC%A1%E5%B0%8F%E5%80%BC-%E5%A4%A7%E5%B0%8F%E5%AF%AB%E4%BA%92%E6%8F%9B-%E5%8D%B0%E5%87%BA%E5%9B%A0%E6%95%B8-32537515909a)
54 |
55 | - [[Lidemy 學習筆記]-先別急著寫 leetcode Project3//實作:靈魂伴侶、音速小子、貪婪的小偷](https://sweetornotspicymarathon.medium.com/lidemy-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%88%E5%88%A5%E6%80%A5%E8%91%97%E5%AF%AB-leetcode-project3-%E5%AF%A6%E4%BD%9C-%E9%9D%88%E9%AD%82%E4%BC%B4%E4%BE%B6-%E9%9F%B3%E9%80%9F%E5%B0%8F%E5%AD%90-%E8%B2%AA%E5%A9%AA%E7%9A%84%E5%B0%8F%E5%81%B7-f0e74adbe582)
56 |
57 | - [[Lidemy 學習筆記]-先別急著寫 leetcode U4//實作:判斷質數、好多星星、印出金字塔、印出聖誕樹、NM乘法表、水仙花數](https://sweetornotspicymarathon.medium.com/lidemy-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%88%E5%88%A5%E6%80%A5%E8%91%97%E5%AF%AB-leetcode-u4-%E5%AF%A6%E4%BD%9C-%E5%88%A4%E6%96%B7%E8%B3%AA%E6%95%B8-%E5%A5%BD%E5%A4%9A%E6%98%9F%E6%98%9F-%E5%8D%B0%E5%87%BA%E9%87%91%E5%AD%97%E5%A1%94-%E5%8D%B0%E5%87%BA%E8%81%96%E8%AA%95%E6%A8%B9-nm%E4%B9%98%E6%B3%95%E8%A1%A8-%E6%B0%B4%E4%BB%99%E8%8A%B1%E6%95%B8-e138dd6a54e)
58 |
59 | - [[Lidemy 學習筆記]-先別急著寫 leetcode U5//實作:經典題目](https://sweetornotspicymarathon.medium.com/lidemy-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%88%E5%88%A5%E6%80%A5%E8%91%97%E5%AF%AB-leetcode-u5-%E5%AF%A6%E4%BD%9C-%E7%B6%93%E5%85%B8%E9%A1%8C%E7%9B%AE-2cf0f7a2b918)
60 |
61 | - [[Lidemy 學習筆記]-先別急著寫 leetcode U6//實作:內建函式](https://sweetornotspicymarathon.medium.com/lidemy-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%88%E5%88%A5%E6%80%A5%E8%91%97%E5%AF%AB-leetcode-u6-%E5%AF%A6%E4%BD%9C-%E5%85%A7%E5%BB%BA%E5%87%BD%E5%BC%8F-17dced76c823)
62 |
63 | - [[Lidemy 學習筆記]-先別急著寫 leetcode //#1008 幾個水桶解題過程](https://sweetornotspicymarathon.medium.com/lidemy-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%88%E5%88%A5%E6%80%A5%E8%91%97%E5%AF%AB-leetcode-1008-%E5%B9%BE%E5%80%8B%E6%B0%B4%E6%A1%B6%E8%A7%A3%E9%A1%8C%E9%81%8E%E7%A8%8B-46754e2f5419)
64 |
65 | - [[Lidemy 學習筆記]-先別急著寫 leetcode U7//實作:NPSC題](https://sweetornotspicymarathon.medium.com/lidemy-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%88%E5%88%A5%E6%80%A5%E8%91%97%E5%AF%AB-leetcode-u7-%E5%AF%A6%E4%BD%9C-npsc%E9%A1%8C-2d1adf97b20)
66 |
67 | - [一次搞懂費氏數列和遞迴(Python code)](https://sweetornotspicymarathon.medium.com/%E4%B8%80%E6%AC%A1%E6%90%9E%E6%87%82%E8%B2%BB%E5%BC%8F%E6%95%B8%E5%88%97%E5%92%8C%E9%81%9E%E8%BF%B4-python-code-a0c741a748da)
68 |
--------------------------------------------------------------------------------
/Unit1_code.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "20200926_Lidemy1.7Project1.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyMei4zYfkQjtakxAY0TPB4s",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "view-in-github",
21 | "colab_type": "text"
22 | },
23 | "source": [
24 | "
"
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {
30 | "id": "YdPQundgqhe5"
31 | },
32 | "source": [
33 | "# 第一題 字串反轉\n",
34 | "\n",
35 | " input: hello\n",
36 | "\n",
37 | " output: olleh"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "metadata": {
43 | "id": "2D5QCpDXhLmO",
44 | "colab": {
45 | "base_uri": "https://localhost:8080/",
46 | "height": 51
47 | },
48 | "outputId": "8ac65eb2-5852-4c75-908d-75f0512ffcc7"
49 | },
50 | "source": [
51 | "import sys\n",
52 | "\n",
53 | "string=input()\n",
54 | "new_str=[]\n",
55 | "\n",
56 | "\n",
57 | "def str_reversal(string):\n",
58 | " # 字串陣列反轉\n",
59 | " for i in range(len(string)):\n",
60 | " new_str.append(string[len(string)-i-1])\n",
61 | "\n",
62 | " # 字串合併\n",
63 | " new_comb=''\n",
64 | " for words in new_str:\n",
65 | " new_comb+=words\n",
66 | "\n",
67 | " print(new_comb) \n",
68 | "\n",
69 | "\n",
70 | "str_reversal(string)"
71 | ],
72 | "execution_count": null,
73 | "outputs": [
74 | {
75 | "output_type": "stream",
76 | "text": [
77 | "Today is working day\n",
78 | "yad gnikrow si yadoT\n"
79 | ],
80 | "name": "stdout"
81 | }
82 | ]
83 | },
84 | {
85 | "cell_type": "code",
86 | "metadata": {
87 | "id": "IKeCpZx2CdhP"
88 | },
89 | "source": [
90 | ""
91 | ],
92 | "execution_count": null,
93 | "outputs": []
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "metadata": {
98 | "id": "W2UlGvM7quA9"
99 | },
100 | "source": [
101 | "# 第二題 陣列總和\n",
102 | "input : [1,2,3]\n",
103 | "\n",
104 | "output : [6]\n"
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "metadata": {
110 | "id": "KGIOah-YrA0d",
111 | "colab": {
112 | "base_uri": "https://localhost:8080/",
113 | "height": 34
114 | },
115 | "outputId": "ce851983-334c-4630-e2f2-6d328c26358b"
116 | },
117 | "source": [
118 | "arr=[1,2,3]\n",
119 | "print(sum(arr))\n",
120 | "\n"
121 | ],
122 | "execution_count": null,
123 | "outputs": [
124 | {
125 | "output_type": "stream",
126 | "text": [
127 | "6\n"
128 | ],
129 | "name": "stdout"
130 | }
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "metadata": {
136 | "id": "Jwd2v_QrJ-Ta"
137 | },
138 | "source": [
139 | ""
140 | ],
141 | "execution_count": null,
142 | "outputs": []
143 | },
144 | {
145 | "cell_type": "markdown",
146 | "metadata": {
147 | "id": "niE8uB26KaKi"
148 | },
149 | "source": [
150 | "# 第三題 找最大值\n",
151 | "input : [1,2,3]\n",
152 | "\n",
153 | "output : [3]"
154 | ]
155 | },
156 | {
157 | "cell_type": "code",
158 | "metadata": {
159 | "id": "OR-qPh8MKlvF",
160 | "colab": {
161 | "base_uri": "https://localhost:8080/",
162 | "height": 34
163 | },
164 | "outputId": "a42b85b9-ab07-4886-e736-b532d86aae92"
165 | },
166 | "source": [
167 | "arr=[1,2,3]\n",
168 | "bigest=arr[1]\n",
169 | "for i in arr:\n",
170 | " if i>bigest:\n",
171 | " biggest=i\n",
172 | "print(biggest) "
173 | ],
174 | "execution_count": null,
175 | "outputs": [
176 | {
177 | "output_type": "stream",
178 | "text": [
179 | "3\n"
180 | ],
181 | "name": "stdout"
182 | }
183 | ]
184 | }
185 | ]
186 | }
--------------------------------------------------------------------------------
/Unit2_code.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "20200926_LidemyProject2.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyO3GsVmykT6AxRH3Dq2DvOT"
9 | },
10 | "kernelspec": {
11 | "name": "python3",
12 | "display_name": "Python 3"
13 | }
14 | },
15 | "cells": [
16 | {
17 | "cell_type": "markdown",
18 | "metadata": {
19 | "id": "yszZPJ7hzNe5"
20 | },
21 | "source": [
22 | "# Lidemy project2\n",
23 | "# 第一題\n",
24 | "# 找陣列次小值\n",
25 | "\n",
26 | "input : [1,2,3,4,5,6,7,8,9,10]\n",
27 | "\n",
28 | "output : [2]\n"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "metadata": {
34 | "id": "iy8CPGpGufKK",
35 | "colab": {
36 | "base_uri": "https://localhost:8080/",
37 | "height": 34
38 | },
39 | "outputId": "35e4e124-71ce-4bf4-97f4-efb8926f5c1e"
40 | },
41 | "source": [
42 | "# 1\n",
43 | "\n",
44 | "arr=[100,123,53,78,961,32,1,456,1000]\n",
45 | "\n",
46 | "# 最小值 min_1,先令為陣列中的最大值\n",
47 | "# Li 則是令一個變數:極大值\n",
48 | "min_1=arr[arr.index(max(arr))] # 最小值\n",
49 | "min_2=arr[arr.index(max(arr))] # 次小值\n",
50 | "\n",
51 | "for i in range(len(arr)):\n",
52 | "\n",
53 | " if arr[i] < min_1:\n",
54 | " \n",
55 | " min_2=min_1\n",
56 | " min_1=arr[i]\n",
57 | "\n",
58 | " elif arr[i]
"
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {
30 | "id": "HqbiE0EnIWFG"
31 | },
32 | "source": [
33 | "https://oj.lidemy.com/problem/1008\n",
34 | "\n",
35 | "\n",
36 | "Lidemy 課程的U3-1\n",
37 | "\n",
38 | "題目是第 1008 題"
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {
44 | "id": "e9x1t8YkIn5M"
45 | },
46 | "source": [
47 | "Input\n",
48 | "輸入為一個數字 M(1 <= M <= 2^{31} - 11<=M<=2 \n",
49 | "31\n",
50 | " −1)\n",
51 | "\n",
52 | "\n",
53 | "Output\n",
54 | "請輸出若是要取 M 個單位的水,最少需要帶幾個水桶\n",
55 | "\n",
56 | "\n",
57 | "20 單位的水,帶一個 16 的水桶加一個 4 的水桶即可,所以答案是 2"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "metadata": {
63 | "id": "4UmexksSxjqh",
64 | "colab": {
65 | "base_uri": "https://localhost:8080/",
66 | "height": 34
67 | },
68 | "outputId": "a3537baa-de17-4a69-c89b-0e71907bc36d"
69 | },
70 | "source": [
71 | "# 20單位的水\n",
72 | "WaterM=20\n",
73 | "M=WaterM\n",
74 | "arr=[]\n",
75 | "\n",
76 | "\n",
77 | "# Total 紀錄2的倍數的總和\n",
78 | "# Count=0 紀錄2的倍數\n",
79 | "Total=0\n",
80 | "\n",
81 | "\n",
82 | "def persernt_2(M):\n",
83 | " Count=0\n",
84 | " while M>=2:\n",
85 | " M=int(M/2)\n",
86 | " Count+=1 \n",
87 | " if M<=1:\n",
88 | " return pow(2,Count)\n",
89 | "\n",
90 | "\n",
91 | "while Total
"
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "metadata": {
30 | "id": "HqbiE0EnIWFG"
31 | },
32 | "source": [
33 | "https://oj.lidemy.com/problem/1008\n",
34 | "\n",
35 | "\n",
36 | "Lidemy 課程的U3-1\n",
37 | "\n",
38 | "題目是第 1008 題"
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "metadata": {
44 | "id": "e9x1t8YkIn5M"
45 | },
46 | "source": [
47 | "Input\n",
48 | "輸入為一個數字 M(1 <= M <= 2^{31} - 11<=M<=2 \n",
49 | "31\n",
50 | " −1)\n",
51 | "\n",
52 | "\n",
53 | "Output\n",
54 | "請輸出若是要取 M 個單位的水,最少需要帶幾個水桶\n",
55 | "\n",
56 | "\n",
57 | "20 單位的水,帶一個 16 的水桶加一個 4 的水桶即可,所以答案是 2"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "metadata": {
63 | "id": "4UmexksSxjqh",
64 | "colab": {
65 | "base_uri": "https://localhost:8080/",
66 | "height": 34
67 | },
68 | "outputId": "a3537baa-de17-4a69-c89b-0e71907bc36d"
69 | },
70 | "source": [
71 | "# 20單位的水\n",
72 | "WaterM=20\n",
73 | "M=WaterM\n",
74 | "arr=[]\n",
75 | "\n",
76 | "\n",
77 | "# Total 紀錄2的倍數的總和\n",
78 | "# Count=0 紀錄2的倍數\n",
79 | "Total=0\n",
80 | "\n",
81 | "\n",
82 | "def persernt_2(M):\n",
83 | " Count=0\n",
84 | " while M>=2:\n",
85 | " M=int(M/2)\n",
86 | " Count+=1 \n",
87 | " if M<=1:\n",
88 | " return pow(2,Count)\n",
89 | "\n",
90 | "\n",
91 | "while Total
"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "metadata": {
30 | "id": "U3w3Q_PYk5Zp"
31 | },
32 | "source": [
33 | "# Lidemy U5.3 身分證驗證\n",
34 | "\n",
35 | "# 身分證規則\n",
36 | "\n",
37 | "# 1.英文轉對應數字\n",
38 | "# A 10\n",
39 | "# B 11 ...\n",
40 | "\n",
41 | "# 2.轉成數字後就會有11位數,再把每一位數乘以1、9 8 7 6 5 3 2 1 1 \n",
42 | "# 接著相加後除以10 \n",
43 | "# 如果整除就通過驗證"
44 | ],
45 | "execution_count": null,
46 | "outputs": []
47 | },
48 | {
49 | "cell_type": "code",
50 | "metadata": {
51 | "id": "URCc64rjlveq",
52 | "colab": {
53 | "base_uri": "https://localhost:8080/"
54 | },
55 | "outputId": "f26f07e8-065c-4f44-88c9-a69e8ac160cb"
56 | },
57 | "source": [
58 | "# 判斷身份證字號是否合法\n",
59 | "def isVailid(IDString):\n",
60 | "\n",
61 | " # 1.處理英文字母的部分,轉成數字\n",
62 | " \n",
63 | "\n",
64 | " dict = {'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15,\n",
65 | " 'G':16, 'H':17, 'I':34, 'J':18, 'K':19, \n",
66 | " 'M':21, 'N':22, 'O':35, 'P':23, 'Q':24, 'T':27,\n",
67 | " 'U':28, 'V':29, 'W':32, 'X':30, 'Z':33,\n",
68 | " 'L':20, 'R':25, 'S':26, 'Y':31}\n",
69 | " \n",
70 | " \n",
71 | " ID_Num=str(dict[str(IDString[0])])\n",
72 | " ID_Num=ID_Num+(str(IDString[1:]))\n",
73 | "\n",
74 | "\n",
75 | " # 2.每個數字依序乘以 1 9 8 7 6 5 4 3 2 1 1 \n",
76 | " Sum=0\n",
77 | " \n",
78 | " for i in range(len(ID_Num)):\n",
79 | " if i==0:\n",
80 | " Sum+=int(ID_Num[i])*1\n",
81 | "\n",
82 | " elif i==10:\n",
83 | " Sum+=int(ID_Num[i])*1\n",
84 | "\n",
85 | " else:\n",
86 | " Sum+=int(ID_Num[i])*(10-i)\n",
87 | "\n",
88 | "\n",
89 | " if Sum%10==0:\n",
90 | " print(\"True\")\n",
91 | " else:\n",
92 | " print(\"False\")\n",
93 | "\n",
94 | "isVailid('A222345123')"
95 | ],
96 | "execution_count": null,
97 | "outputs": [
98 | {
99 | "output_type": "stream",
100 | "text": [
101 | "False\n"
102 | ],
103 | "name": "stdout"
104 | }
105 | ]
106 | },
107 | {
108 | "cell_type": "code",
109 | "metadata": {
110 | "id": "fcQcpl7wP7eX"
111 | },
112 | "source": [
113 | ""
114 | ],
115 | "execution_count": null,
116 | "outputs": []
117 | },
118 | {
119 | "cell_type": "code",
120 | "metadata": {
121 | "id": "qniFDyqDxE3l"
122 | },
123 | "source": [
124 | "# Lidemy U5.4 數字各位數加總\n",
125 | "\n",
126 | "# 數字各位數加總的意思是指說\n",
127 | "# 如果數字為 1234 那加總的結果是 1+2+3+4=10\n",
128 | "# 數字為1111 加總結果為1+1+1+1=4\n",
129 | "# 數字為-1111 加總結果為1+1+1+1=4\n",
130 | "\n",
131 | "# 這個題目有兩個對於輸入進來的數字的處理方法\n",
132 | "# 1.一個是用數字解,依照輸入進來的數字一直除以10取餘數,\n",
133 | "# 而可以得各位數數值\n",
134 | "# 2.轉換成文字解,直接變成字串,依照字串每一個直接加\n",
135 | "\n",
136 | "# 特別注意第一種方法遇到負數的情況\n"
137 | ],
138 | "execution_count": null,
139 | "outputs": []
140 | },
141 | {
142 | "cell_type": "code",
143 | "metadata": {
144 | "id": "DEfIhkNRwvzY",
145 | "colab": {
146 | "base_uri": "https://localhost:8080/"
147 | },
148 | "outputId": "664f179d-3888-46f0-b940-a0410823c65d"
149 | },
150 | "source": [
151 | "\n",
152 | "# 解法一-數字解法\n",
153 | "def adddigitals_1(Number):\n",
154 | " # 先處理負數問題\n",
155 | " if Number <0 :\n",
156 | " Number=Number*-1\n",
157 | " \n",
158 | " Sum=0\n",
159 | " while Number!=0:\n",
160 | " Sum=Sum+Number%10\n",
161 | " Number=int(Number/10 )\n",
162 | " \n",
163 | " \n",
164 | " print(Sum)\n",
165 | "adddigitals_1(-1234) # ans = 10"
166 | ],
167 | "execution_count": null,
168 | "outputs": [
169 | {
170 | "output_type": "stream",
171 | "text": [
172 | "10\n"
173 | ],
174 | "name": "stdout"
175 | }
176 | ]
177 | },
178 | {
179 | "cell_type": "code",
180 | "metadata": {
181 | "colab": {
182 | "base_uri": "https://localhost:8080/"
183 | },
184 | "id": "vpFPbiTzSdVv",
185 | "outputId": "b7741263-69d7-4b59-917b-404ac001ef34"
186 | },
187 | "source": [
188 | "# 解法二-字串解法\n",
189 | "def adddigitals_2(Number):\n",
190 | " \n",
191 | " Sum=0\n",
192 | "\n",
193 | " for i in range(len(str(Number))):\n",
194 | " Sum=Sum+int(Number[i])\n",
195 | " print(Sum)\n",
196 | "\n",
197 | "\n",
198 | " print(Sum)\n",
199 | "adddigitals_1(5678) # ans = 10"
200 | ],
201 | "execution_count": null,
202 | "outputs": [
203 | {
204 | "output_type": "stream",
205 | "text": [
206 | "26\n"
207 | ],
208 | "name": "stdout"
209 | }
210 | ]
211 | },
212 | {
213 | "cell_type": "code",
214 | "metadata": {
215 | "id": "AN5TCTgqnu3t"
216 | },
217 | "source": [
218 | "# Lidemy U5 Project 第一題\n",
219 | "# LIOJ 1025等比數列\n",
220 | "\n",
221 | "import sys\n",
222 | "N=int(input())\n",
223 | "\n",
224 | "\n",
225 | "Ai=(input().split(\" \"))\n",
226 | "\n",
227 | "\n",
228 | "def isgeometric(N,Ai):\n",
229 | " if N==2:\n",
230 | " return True\n",
231 | "\n",
232 | " else:\n",
233 | " # 等差級數\n",
234 | " geometric=float(int(Ai[1])/int(Ai[0]))\n",
235 | " # print(\"geometric :\",geometric)\n",
236 | "\n",
237 | " for i in range(N-1):\n",
238 | " # print(\"Ai[i+1] :\",Ai[i+1])\n",
239 | " # print(\"Ai[i] :\",Ai[i])\n",
240 | "\n",
241 | " if float(int(Ai[i+1])/int(Ai[i]))!=geometric:\n",
242 | " \n",
243 | " return False\n",
244 | " \n",
245 | " \n",
246 | " return True \n",
247 | "\n",
248 | "if isgeometric(N,Ai):\n",
249 | " print(\"Yes\")\n",
250 | "else:\n",
251 | " print(\"No\") "
252 | ],
253 | "execution_count": null,
254 | "outputs": []
255 | },
256 | {
257 | "cell_type": "code",
258 | "metadata": {
259 | "id": "mNJzmrLHnymK"
260 | },
261 | "source": [
262 | "# Lidemy Project5 信用卡驗證 LIOJ#1027\n",
263 | "# https://oj.lidemy.com/problem/1027?_ga=2.203859797.47464004.1605858245-1563532151.1605858245\n",
264 | "\n",
265 | "# 信用卡15碼\n",
266 | "# 第一碼 : Master Card是5開頭, Visa是4, JCB則是3\n",
267 | "\n",
268 | "# 前15位數經過加權加總得一個數字,此數除以10取餘數\n",
269 | "# 如果餘數為0=>檢查碼為0 ,否則用10減去之後的結果\n",
270 | "\n",
271 | "# 加權的部分\n",
272 | "# 左邊起奇數*2 偶數*1\n",
273 | "# 如果加權比10大則減去9\n",
274 | "\n",
275 | "# 例如\n",
276 | "# 5412-3456-7890-1232\n",
277 | "# 第一位5=>Master Card\n",
278 | "# 5 1 3 5 7 9 1 3=>都是乘以2,但比10大時減去9 \n",
279 | " # =>1(10-9) + 2 + 6 + 1(10-9) + 5(14-9) + 9(18-9) + 2 + 6 = 32\n",
280 | "# 4 2 4 6 8 0 2 2=>都是乘以1\n",
281 | " # =>4 + 2 + 4 + 6 + 8 + 0 + 2 = 26\n",
282 | "\n",
283 | "# 奇數偶數相加=32+26=58\n",
284 | "# 58%10=8 ,檢查碼=10-8=2\n",
285 | "# 卡號的第16位=2,因此為合法\n",
286 | "\n",
287 | "# 輸入: 5412-3456-7890-1232\n",
288 | "# 輸出: 不合法,請輸出 INVALID;\n",
289 | " # 合法 VISA 與MASTER_CARD"
290 | ],
291 | "execution_count": null,
292 | "outputs": []
293 | },
294 | {
295 | "cell_type": "code",
296 | "metadata": {
297 | "colab": {
298 | "base_uri": "https://localhost:8080/"
299 | },
300 | "id": "a6-jSveloO1h",
301 | "outputId": "c6ef5c51-67fc-4de5-85dc-eb6cfe228ee0"
302 | },
303 | "source": [
304 | "import sys\n",
305 | "# input card number\n",
306 | "CardNum=str(input())\n",
307 | "\n",
308 | "\n",
309 | "\n",
310 | "# step0 clean card number\n",
311 | "CardNum=CardNum[0:4]+CardNum[5:9]+CardNum[10:14]+CardNum[15:19]\n",
312 | "\n",
313 | "\n",
314 | "# step1 check is it valid?\n",
315 | "\n",
316 | "def IsValide(CardNum):\n",
317 | "\n",
318 | " Sum_1=0 #奇數總和\n",
319 | " Sum_2=0 #偶數總和\n",
320 | "\n",
321 | " for i in range(len(CardNum)-1):\n",
322 | "\n",
323 | " if i%2==0: \n",
324 | " \n",
325 | " if (int(CardNum[i])*2)>=10:\n",
326 | "\n",
327 | " Sum_1=Sum_1+(int(CardNum[i])*2-9)\n",
328 | "\n",
329 | " else:\n",
330 | " Sum_1=Sum_1+(int(CardNum[i])*2)\n",
331 | " \n",
332 | " \n",
333 | " else:\n",
334 | " Sum_2=Sum_2+int(CardNum[i])*1\n",
335 | " \n",
336 | " \n",
337 | " # check number\n",
338 | "\n",
339 | "\n",
340 | " if 10-((Sum_1+Sum_2)%10)!= int(CardNum[15]):\n",
341 | " print(\"INVALID\")\n",
342 | " else:\n",
343 | " if int(CardNum[0])==5:\n",
344 | " print(\"MASTER_CARD\") \n",
345 | " elif int(CardNum[0])==4:\n",
346 | " print(\"VISA\") \n",
347 | "\n",
348 | "IsValide(CardNum) "
349 | ],
350 | "execution_count": null,
351 | "outputs": [
352 | {
353 | "output_type": "stream",
354 | "text": [
355 | "4498-6640-2633-4106\n",
356 | "VISA\n"
357 | ],
358 | "name": "stdout"
359 | }
360 | ]
361 | },
362 | {
363 | "cell_type": "code",
364 | "metadata": {
365 | "id": "UdDIhc9kCx84"
366 | },
367 | "source": [
368 | "# Lidemy Project5 生命靈數 #1028\n",
369 | "# https://oj.lidemy.com/problem/1028?_ga=2.232081882.47464004.1605858245-1563532151.1605858245\n",
370 | "\n",
371 | "# 所有的數字相加加到只剩下個位數\n",
372 | "# 例如: 1991 11 7\n",
373 | "# 1+9+9+1+1+1+7=31 =>3+1=4\n",
374 | "\n"
375 | ],
376 | "execution_count": null,
377 | "outputs": []
378 | },
379 | {
380 | "cell_type": "code",
381 | "metadata": {
382 | "colab": {
383 | "base_uri": "https://localhost:8080/"
384 | },
385 | "id": "Mwq7D_X6DNLi",
386 | "outputId": "97d86b09-ee80-4c2a-dacd-2e38b0ce43ec"
387 | },
388 | "source": [
389 | "import sys\n",
390 | "\n",
391 | "BD=str(input())\n",
392 | "# Clean string\n",
393 | "BD=BD.replace(\" \",\"\")\n",
394 | "\n",
395 | "\n",
396 | "\n",
397 | "# Add\n",
398 | "while (len(BD))!=1:\n",
399 | " sum=0\n",
400 | " for i in range(len((BD))):\n",
401 | " sum=sum+int(BD[i])\n",
402 | " \n",
403 | " BD=str(sum)\n",
404 | "\n",
405 | "print(sum) "
406 | ],
407 | "execution_count": null,
408 | "outputs": [
409 | {
410 | "output_type": "stream",
411 | "text": [
412 | "111\n",
413 | "3\n"
414 | ],
415 | "name": "stdout"
416 | }
417 | ]
418 | },
419 | {
420 | "cell_type": "code",
421 | "metadata": {
422 | "id": "e_auk_G-FzTB"
423 | },
424 | "source": [
425 | "# Lidemy Project5 加減乘除 #1029\n",
426 | "# https://oj.lidemy.com/problem/1029\n",
427 | "# 輸入兩數 和 運算符號\n",
428 | "# 輸出運算結果以整數表示"
429 | ],
430 | "execution_count": null,
431 | "outputs": []
432 | },
433 | {
434 | "cell_type": "code",
435 | "metadata": {
436 | "colab": {
437 | "base_uri": "https://localhost:8080/"
438 | },
439 | "id": "wK6lW4KtFz2n",
440 | "outputId": "62ca7504-cb4e-429b-dc5e-722512f6851a"
441 | },
442 | "source": [
443 | "import sys\n",
444 | "\n",
445 | "AB=str(input())\n",
446 | "\n",
447 | "\n",
448 | "A=int(AB.split(' ')[0])\n",
449 | "Operator=AB.split(' ')[1]\n",
450 | "B=int(AB.split(' ')[2])\n",
451 | "\n",
452 | "\n",
453 | "\n",
454 | "if Operator==\"+\":\n",
455 | " print(int(A+B))\n",
456 | " \n",
457 | "elif Operator==\"-\":\n",
458 | " print(int(A-B))\n",
459 | "\n",
460 | "elif Operator==\"*\":\n",
461 | " print(int(A*B))\n",
462 | "\n",
463 | "elif Operator==\"/\":\n",
464 | " print(int(A/B))\n"
465 | ],
466 | "execution_count": null,
467 | "outputs": [
468 | {
469 | "output_type": "stream",
470 | "text": [
471 | "3 * 4\n",
472 | "12\n"
473 | ],
474 | "name": "stdout"
475 | }
476 | ]
477 | },
478 | {
479 | "cell_type": "code",
480 | "metadata": {
481 | "id": "Co78TmRh66-z"
482 | },
483 | "source": [
484 | "# Lidemy project U5 LIOJ#1030 判斷迴文\n",
485 | "# https://oj.lidemy.com/problem/1030\n",
486 | "\n",
487 | "# 迴文定義是把它反過來還是長一樣\n",
488 | "# 例如 輸入: abbbba 輸出:True\n",
489 | "# 輸入: ac 輸出:False\n",
490 | "\n"
491 | ],
492 | "execution_count": null,
493 | "outputs": []
494 | },
495 | {
496 | "cell_type": "code",
497 | "metadata": {
498 | "colab": {
499 | "base_uri": "https://localhost:8080/"
500 | },
501 | "id": "SB30Wcq0BB7d",
502 | "outputId": "8722e7f3-f69b-4efa-a3bc-7e2bd931c5fc"
503 | },
504 | "source": [
505 | "import sys\n",
506 | "\n",
507 | "S=str(input())\n",
508 | "\n",
509 | "\n",
510 | "def isrecover(S):\n",
511 | " for i in range(len(S)):\n",
512 | " if S[i]==S[len(S)-i-1]:\n",
513 | " continue\n",
514 | "\n",
515 | " elif S[i]!=S[len(S)-i-1]:\n",
516 | " return False\n",
517 | " return True \n",
518 | "\n",
519 | "\n",
520 | "if (isrecover(S)):\n",
521 | " print(\"True\")\n",
522 | "\n",
523 | "else:\n",
524 | " print(\"False\")\n",
525 | "\n",
526 | "\n",
527 | " \n",
528 | " \n",
529 | " \n",
530 | "\n",
531 | " "
532 | ],
533 | "execution_count": null,
534 | "outputs": [
535 | {
536 | "output_type": "stream",
537 | "text": [
538 | "ASD\n",
539 | "False\n"
540 | ],
541 | "name": "stdout"
542 | }
543 | ]
544 | },
545 | {
546 | "cell_type": "code",
547 | "metadata": {
548 | "id": "wwfAhfRsCmW9"
549 | },
550 | "source": [
551 | "# Lidemy Project U5 LIOJ#1031 完全平方和\n",
552 | "# https://oj.lidemy.com/problem/1031\n",
553 | "# 請輸出 1~N 中完全平方數的總和\n",
554 | "\n",
555 | "# 例如: 輸入30 輸出為55 ,因為1+4+9+16+25= 55\n"
556 | ],
557 | "execution_count": null,
558 | "outputs": []
559 | },
560 | {
561 | "cell_type": "code",
562 | "metadata": {
563 | "colab": {
564 | "base_uri": "https://localhost:8080/"
565 | },
566 | "id": "N1JEdKI3EzDR",
567 | "outputId": "c5ab26ed-0958-42a4-b242-1b145e6caece"
568 | },
569 | "source": [
570 | "import sys\n",
571 | "\n",
572 | "N=int(str(input()))\n",
573 | "\n",
574 | "sum=0\n",
575 | "for i in range(1,N+1):\n",
576 | "\n",
577 | " if i**2>N:\n",
578 | " break\n",
579 | " else:\n",
580 | " sum+= i**2 \n",
581 | "print(sum)"
582 | ],
583 | "execution_count": null,
584 | "outputs": [
585 | {
586 | "output_type": "stream",
587 | "text": [
588 | "30\n",
589 | "55\n"
590 | ],
591 | "name": "stdout"
592 | }
593 | ]
594 | },
595 | {
596 | "cell_type": "code",
597 | "metadata": {
598 | "id": "wz34gGWjGZlu"
599 | },
600 | "source": [
601 | "# Lidemy Project U5 LIOJ#1032 平面距離計算\n",
602 | "\n",
603 | "# 兩個點 (x1, y1) 與 (x2,y2) 的距離計算公式則為 \\sqrt{|x1-x2|^2 + |y1-y2|^2 } \n",
604 | "\n",
605 | "# 輸入:\n",
606 | "# 第一行為一個正整數 T(1<=T<=1001<=T<=100),代表一共有幾筆測試資料\n",
607 | "# 每一筆測試資料會有 4 個整數,x1, y1, x2, y2,範圍均在 -100~100,彼此間以空行分開\n",
608 | "\n",
609 | "\n",
610 | "1\n",
611 | "1\n",
612 | "2\n",
613 | "2\n",
614 | "3\n",
615 | "3\n",
616 | "4\n",
617 | "4\n",
618 | "\n",
619 | "# 輸出:\n",
620 | "1.41\n",
621 | "1.41\n"
622 | ],
623 | "execution_count": null,
624 | "outputs": []
625 | },
626 | {
627 | "cell_type": "code",
628 | "metadata": {
629 | "id": "oa4t-fCFGzUY",
630 | "colab": {
631 | "base_uri": "https://localhost:8080/"
632 | },
633 | "outputId": "903b9337-0b5d-49a0-829b-dfdab6b80148"
634 | },
635 | "source": [
636 | "import sys\n",
637 | "Case=int(input()) # 幾筆\n",
638 | "\n",
639 | "\n",
640 | "XY=[]\n",
641 | "for i in range(Case*4):\n",
642 | " a=int(input())\n",
643 | " XY.append(a)\n",
644 | "\n",
645 | "\n",
646 | "for i in range(Case):\n",
647 | " X1=XY[i*4+0]\n",
648 | " Y1=XY[i*4+1]\n",
649 | " X2=XY[i*4+2]\n",
650 | " Y2=XY[i*4+3]\n",
651 | "\n",
652 | " \n",
653 | " distance=((X1-X2)**2+(Y1-Y2)**2)**0.5\n",
654 | " print('%.2f'%round(distance,2))"
655 | ],
656 | "execution_count": null,
657 | "outputs": [
658 | {
659 | "output_type": "stream",
660 | "text": [
661 | "2\n",
662 | "1\n",
663 | "1\n",
664 | "2\n",
665 | "2\n",
666 | "3\n",
667 | "3\n",
668 | "4\n",
669 | "4\n",
670 | "1.41\n",
671 | "1.41\n"
672 | ],
673 | "name": "stdout"
674 | }
675 | ]
676 | },
677 | {
678 | "cell_type": "markdown",
679 | "metadata": {
680 | "id": "7NzzIlDjJz2M"
681 | },
682 | "source": [
683 | "# Lidemy LIOJ #1023 最近點對\n",
684 | "\n",
685 | "https://oj.lidemy.com/problem/1033\n",
686 | "\n",
687 | "求距離最近的兩點\n",
688 | "\n",
689 | "如果有多組距離一樣近,則輸出第一組資料\n",
690 | "\n",
691 | "EX:\n",
692 | "輸入\n",
693 | "\n",
694 | "\n",
695 | "```\n",
696 | "4\n",
697 | "2 2\n",
698 | "1 1\n",
699 | "10 10\n",
700 | "100 100\n",
701 | "```\n",
702 | "\n",
703 | "輸出\n",
704 | "\n",
705 | "\n",
706 | "```\n",
707 | "1 1\n",
708 | "2 2\n",
709 | "```\n"
710 | ]
711 | },
712 | {
713 | "cell_type": "code",
714 | "metadata": {
715 | "colab": {
716 | "base_uri": "https://localhost:8080/"
717 | },
718 | "id": "Hc_-OfNafbNj",
719 | "outputId": "7bcdae28-f6db-47e1-f776-e0304f2484a9"
720 | },
721 | "source": [
722 | "import sys\n",
723 | "case=int(input()) \n",
724 | "XY=[]\n",
725 | "for i in range(case):\n",
726 | " a=(input().split(' '))\n",
727 | " \n",
728 | " XY.append(a[0])\n",
729 | " XY.append(a[1])\n",
730 | "\n",
731 | "def CountDistance(m,n):\n",
732 | "\n",
733 | " X1=int(XY[m*2+0])\n",
734 | " Y1=int(XY[m*2+1])\n",
735 | " X2=int(XY[n*2+0])\n",
736 | " Y2=int(XY[n*2+1])\n",
737 | "\n",
738 | " distance=((X1-X2)**2+(Y1-Y2)**2)**0.5\n",
739 | " point=[X1,Y1,X2,Y2]\n",
740 | " return distance,point\n",
741 | "\n",
742 | "\n",
743 | "def find_min_point(case):\n",
744 | " for m in range(case):\n",
745 | " for n in range(m+1,case):\n",
746 | " if m ==0 and n==1:\n",
747 | " min_distance,min_point=CountDistance(m,n)\n",
748 | "\n",
749 | " else:\n",
750 | " distance,point=CountDistance(m,n)\n",
751 | " \n",
752 | " if distance Input\n",
804 | "第一行為一個數字 N,代表要往右移的位數,0<=N<=1000<=N<=100\n",
805 | "第二行為一個字串 S,1<= length(S) <= 10001<=length(S)<=1000,代表原文,只會由小寫字母組成\n",
806 | "\n",
807 | "\n",
808 | "> Output\n",
809 | "請輸出把 S 的每個字母右移 N 位之後的結果\n",
810 | "\n",
811 | "EX:\n",
812 | "\n",
813 | "\n",
814 | "```\n",
815 | "輸入\n",
816 | "10\n",
817 | "xray\n",
818 | "```\n",
819 | "\n",
820 | "\n",
821 | "```\n",
822 | "輸出\n",
823 | "hbki\n",
824 | "```\n",
825 | "打算先把英文轉乘數字=>移動後再轉回英文輸出\n"
826 | ]
827 | },
828 | {
829 | "cell_type": "code",
830 | "metadata": {
831 | "colab": {
832 | "base_uri": "https://localhost:8080/"
833 | },
834 | "id": "lGraH7D1kARb",
835 | "outputId": "dfc76a1b-0838-487c-e251-27efa00d661d"
836 | },
837 | "source": [
838 | "import sys\n",
839 | "MoveNum=int(input()) \n",
840 | "S=(input())\n",
841 | " \n",
842 | "\n",
843 | "\n",
844 | "dict = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6,\n",
845 | " 'g':7, 'h':8, 'i':9, 'j':10, 'k':11, \n",
846 | " 'l':12, 'm':13, 'n':14, 'o':15, 'p':16,\n",
847 | " 'q':17, 'r':18, 's':19, 't':20, 'u':21,\n",
848 | " 'v':22, 'w':23, 'x':24, 'y':25, 'z':26, \n",
849 | " }\n",
850 | "\n",
851 | "key_list = list(dict.keys()) \n",
852 | "val_list = list(dict.values()) \n",
853 | "\n",
854 | "\n",
855 | "def StringToNumMove(S,MoveNum,dict):\n",
856 | " NewS=[]\n",
857 | "\n",
858 | " for i in range(len(S)):\n",
859 | " Number=int(dict[S[i]])+MoveNum\n",
860 | " while Number>26:\n",
861 | " Number=Number-26\n",
862 | " NewS.append(key_list[val_list.index(Number)]) \n",
863 | " print(key_list[val_list.index(Number)],end=\"\") \n",
864 | " \n",
865 | "StringToNumMove(S,MoveNum,dict)\n"
866 | ],
867 | "execution_count": null,
868 | "outputs": [
869 | {
870 | "output_type": "stream",
871 | "text": [
872 | "100\n",
873 | "zzz\n",
874 | "vvv"
875 | ],
876 | "name": "stdout"
877 | }
878 | ]
879 | },
880 | {
881 | "cell_type": "code",
882 | "metadata": {
883 | "id": "DvGreTYPwvKB"
884 | },
885 | "source": [
886 | "# LIOJ #1046 圈圈叉叉\n",
887 | "\n",
888 | "# 就是常在玩的圈圈叉叉的遊戲\n",
889 | "# 輸入為 O或X\n",
890 | "# 最後的結果可能為平手 或是某一方勝利\n",
891 | "# 因此輸出 DRAW 或 O 或 X"
892 | ],
893 | "execution_count": null,
894 | "outputs": []
895 | },
896 | {
897 | "cell_type": "code",
898 | "metadata": {
899 | "colab": {
900 | "base_uri": "https://localhost:8080/"
901 | },
902 | "id": "aWNNLViNxyxC",
903 | "outputId": "cb366643-67d4-4d49-b7ba-a9ac8d97afbf"
904 | },
905 | "source": [
906 | "import sys\n",
907 | "\n",
908 | "S1=(input())\n",
909 | "S2=(input())\n",
910 | "S3=(input())\n",
911 | "\n",
912 | "def win8function(S1,S2,S3):\n",
913 | " win=[]\n",
914 | "\n",
915 | " # 1.橫的\n",
916 | " if S1[0]==S1[1]==S1[2]:\n",
917 | " win.append(S1[0])\n",
918 | " if S2[0]==S2[1]==S2[2]:\n",
919 | " win.append(S2[0])\n",
920 | " if S3[0]==S3[1]==S3[2]:\n",
921 | " win.append(S3[0])\n",
922 | "\n",
923 | " # 2.直的\n",
924 | " if S1[0]==S2[0]==S3[0]:\n",
925 | " win.append(S1[0])\n",
926 | " if S1[1]==S2[1]==S3[1]:\n",
927 | " win.append(S1[1])\n",
928 | " if S1[2]==S2[2]==S3[2]:\n",
929 | " win.append(S1[2])\n",
930 | "\n",
931 | " # 3.左斜 \n",
932 | " if S1[0]==S2[1]==S3[2]:\n",
933 | " win.append(S1[0])\n",
934 | " # 4.右斜\n",
935 | " if S1[2]==S2[1]==S3[0]:\n",
936 | " win.append(S1[2])\n",
937 | "\n",
938 | " win=list(set(win))\n",
939 | " if len(win)==2 or len(win)==0:\n",
940 | " print(\"DRAW\")\n",
941 | " else:\n",
942 | " print(win[0])\n",
943 | "\n",
944 | "win8function(S1,S2,S3)"
945 | ],
946 | "execution_count": null,
947 | "outputs": [
948 | {
949 | "output_type": "stream",
950 | "text": [
951 | "OXX\n",
952 | "XXO\n",
953 | "OOX\n",
954 | "DRAW\n"
955 | ],
956 | "name": "stdout"
957 | }
958 | ]
959 | }
960 | ]
961 | }
--------------------------------------------------------------------------------
/Unit6_code.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "U6.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyMIal3EgF9vFziHz2hv6vRJ",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "view-in-github",
21 | "colab_type": "text"
22 | },
23 | "source": [
24 | "
"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "metadata": {
30 | "colab": {
31 | "base_uri": "https://localhost:8080/"
32 | },
33 | "id": "ZH1eG9g3aG0N",
34 | "outputId": "4b97c129-1365-4c8c-a45d-9e7755ed1729"
35 | },
36 | "source": [
37 | "# Lidemy Project6 Array reverse LIOJ1036\r\n",
38 | "\r\n",
39 | "array=[]\r\n",
40 | "\r\n",
41 | "# input\r\n",
42 | "N=int(input())\r\n",
43 | "for i in range(N):\r\n",
44 | " array.append(int(input()))\r\n",
45 | "\r\n",
46 | "# reverse\r\n",
47 | "for j in range(len(array)): \r\n",
48 | " print(array[len(array)-j-1])"
49 | ],
50 | "execution_count": null,
51 | "outputs": [
52 | {
53 | "output_type": "stream",
54 | "text": [
55 | "5\n",
56 | "1\n",
57 | "2\n",
58 | "3\n",
59 | "4\n",
60 | "5\n",
61 | "5\n",
62 | "4\n",
63 | "3\n",
64 | "2\n",
65 | "1\n"
66 | ],
67 | "name": "stdout"
68 | }
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "metadata": {
74 | "colab": {
75 | "base_uri": "https://localhost:8080/"
76 | },
77 | "id": "1y92s2FjdttX",
78 | "outputId": "c959e547-195f-41d6-f29e-565579fae766"
79 | },
80 | "source": [
81 | "# Lidemy Project6 Array reverse LIOJ1036\r\n",
82 | "\r\n",
83 | "# input\r\n",
84 | "N=int(input())\r\n",
85 | "\r\n",
86 | "\r\n",
87 | "# reverse\r\n",
88 | "def array_reverse(N):\r\n",
89 | " array=[]\r\n",
90 | "\r\n",
91 | " for i in range(N):\r\n",
92 | " array.append(int(input()))\r\n",
93 | "\r\n",
94 | " for j in range(len(array)): \r\n",
95 | " print(array[len(array)-j-1])\r\n",
96 | "\r\n",
97 | "array_reverse(N) "
98 | ],
99 | "execution_count": null,
100 | "outputs": [
101 | {
102 | "output_type": "stream",
103 | "text": [
104 | "3\n",
105 | "1\n",
106 | "2\n",
107 | "3\n",
108 | "3\n",
109 | "2\n",
110 | "1\n"
111 | ],
112 | "name": "stdout"
113 | }
114 | ]
115 | },
116 | {
117 | "cell_type": "code",
118 | "metadata": {
119 | "colab": {
120 | "base_uri": "https://localhost:8080/"
121 | },
122 | "id": "3Swa4j5Jjhv4",
123 | "outputId": "e477f9c4-6ec2-483d-af4e-212a492a95a0"
124 | },
125 | "source": [
126 | "# Lidemy Project6 Array filter LIOJ1037\r\n",
127 | "\r\n",
128 | "# delete num\r\n",
129 | "delete=int(input())\r\n",
130 | "\r\n",
131 | "# total num\r\n",
132 | "N = int(input())\r\n",
133 | "\r\n",
134 | "# filter\r\n",
135 | "def filter(delete,N):\r\n",
136 | " array=[]\r\n",
137 | "\r\n",
138 | " for i in range(N):\r\n",
139 | " tmp=int(input())\r\n",
140 | "\r\n",
141 | " if tmp!=delete :\r\n",
142 | " array.append(tmp)\r\n",
143 | "\r\n",
144 | " for j in range(len(array)): \r\n",
145 | " print(array[j]) \r\n",
146 | "\r\n",
147 | "filter(delete,N)"
148 | ],
149 | "execution_count": null,
150 | "outputs": [
151 | {
152 | "output_type": "stream",
153 | "text": [
154 | "3\n",
155 | "5\n",
156 | "1\n",
157 | "2\n",
158 | "3\n",
159 | "4\n",
160 | "5\n",
161 | "1\n",
162 | "2\n",
163 | "4\n",
164 | "5\n"
165 | ],
166 | "name": "stdout"
167 | }
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "metadata": {
173 | "colab": {
174 | "base_uri": "https://localhost:8080/"
175 | },
176 | "id": "rParmOxNlSTY",
177 | "outputId": "999823bc-0211-4dbc-b463-ac924d57ee77"
178 | },
179 | "source": [
180 | "# Lidemy Project6 Array indexOf LIOJ1038\r\n",
181 | "\r\n",
182 | "# target num\r\n",
183 | "target=int(input())\r\n",
184 | "\r\n",
185 | "# total num\r\n",
186 | "N = int(input())\r\n",
187 | "\r\n",
188 | "# indexof\r\n",
189 | "def indexof(target,N):\r\n",
190 | " array=[]\r\n",
191 | "\r\n",
192 | " for i in range(N):\r\n",
193 | " array.append(int(input()))\r\n",
194 | "\r\n",
195 | " for j in range(len(array)): \r\n",
196 | " \r\n",
197 | " if array[j]==target:\r\n",
198 | " print(j)\r\n",
199 | " break\r\n",
200 | " else:\r\n",
201 | " print(\"-1\") \r\n",
202 | "\r\n",
203 | "indexof(target,N)"
204 | ],
205 | "execution_count": null,
206 | "outputs": [
207 | {
208 | "output_type": "stream",
209 | "text": [
210 | "3\n",
211 | "5\n",
212 | "1\n",
213 | "2\n",
214 | "3\n",
215 | "3\n",
216 | "3\n",
217 | "J : 0\n",
218 | "J : 1\n",
219 | "J : 2\n",
220 | "2\n"
221 | ],
222 | "name": "stdout"
223 | }
224 | ]
225 | },
226 | {
227 | "cell_type": "code",
228 | "metadata": {
229 | "colab": {
230 | "base_uri": "https://localhost:8080/"
231 | },
232 | "id": "zpYgmKiJo9MM",
233 | "outputId": "a92ba996-ed89-4121-c6dc-6679ee17ec72"
234 | },
235 | "source": [
236 | "# Lidemy Project6 Array fill LIOJ1039\r\n",
237 | "\r\n",
238 | "# target num\r\n",
239 | "target=int(input())\r\n",
240 | "\r\n",
241 | "# total num\r\n",
242 | "N = int(input())\r\n",
243 | "\r\n",
244 | "# indexof\r\n",
245 | "def fill(target,N):\r\n",
246 | " array=[]\r\n",
247 | "\r\n",
248 | " for i in range(N):\r\n",
249 | " array.append(int(input()))\r\n",
250 | "\r\n",
251 | " for j in range(len(array)): \r\n",
252 | " print(target)\r\n",
253 | "\r\n",
254 | "fill(target,N)"
255 | ],
256 | "execution_count": null,
257 | "outputs": [
258 | {
259 | "output_type": "stream",
260 | "text": [
261 | "10\n",
262 | "3\n",
263 | "1\n",
264 | "2\n",
265 | "3\n",
266 | "10\n",
267 | "10\n",
268 | "10\n"
269 | ],
270 | "name": "stdout"
271 | }
272 | ]
273 | },
274 | {
275 | "cell_type": "code",
276 | "metadata": {
277 | "colab": {
278 | "base_uri": "https://localhost:8080/"
279 | },
280 | "id": "P1PvHwvAq25Y",
281 | "outputId": "d3f161af-b4ad-47b5-d09c-256de044fc19"
282 | },
283 | "source": [
284 | "# Lidemy Project6 Array join LIOJ1040\r\n",
285 | "\r\n",
286 | "# target num\r\n",
287 | "target=str(input())\r\n",
288 | "\r\n",
289 | "# total num\r\n",
290 | "N = int(input())\r\n",
291 | "\r\n",
292 | "# join\r\n",
293 | "def join(target,N):\r\n",
294 | " array=[]\r\n",
295 | " \r\n",
296 | " for i in range(N):\r\n",
297 | " array.append(int(input()))\r\n",
298 | "\r\n",
299 | " for j in range(len(array)): \r\n",
300 | " if j ==0:\r\n",
301 | " new_str=str(array[j])\r\n",
302 | " \r\n",
303 | " else:\r\n",
304 | " \r\n",
305 | " new_str+=str(target)\r\n",
306 | " new_str+=str(array[j])\r\n",
307 | " \r\n",
308 | " print(new_str)\r\n",
309 | "\r\n",
310 | "join(target,N)"
311 | ],
312 | "execution_count": null,
313 | "outputs": [
314 | {
315 | "output_type": "stream",
316 | "text": [
317 | "!!\n",
318 | "3\n",
319 | "1\n",
320 | "2\n",
321 | "3\n",
322 | "1!!2!!3\n"
323 | ],
324 | "name": "stdout"
325 | }
326 | ]
327 | },
328 | {
329 | "cell_type": "code",
330 | "metadata": {
331 | "colab": {
332 | "base_uri": "https://localhost:8080/"
333 | },
334 | "id": "f9_8rhE4uE4d",
335 | "outputId": "3bf280ee-6e33-41ae-c795-d83410200e6e"
336 | },
337 | "source": [
338 | "# Lidemy Project6 Array join LIOJ1040\r\n",
339 | "\r\n",
340 | "# target num\r\n",
341 | "target=str(input())\r\n",
342 | "\r\n",
343 | "# total num\r\n",
344 | "N = int(input())\r\n",
345 | "\r\n",
346 | "def join(target,N):\r\n",
347 | " array=[]\r\n",
348 | " \r\n",
349 | " for i in range(N):\r\n",
350 | " array.append(int(input()))\r\n",
351 | "\r\n",
352 | " for j in range(len(array)):\r\n",
353 | "\r\n",
354 | " if j==len(array)-1:\r\n",
355 | " print(str(array[j]),end='')\r\n",
356 | " else:\r\n",
357 | " print(str(array[j]),end='') \r\n",
358 | " print(str(target),end='')\r\n",
359 | "\r\n",
360 | "join(target,N)"
361 | ],
362 | "execution_count": null,
363 | "outputs": [
364 | {
365 | "output_type": "stream",
366 | "text": [
367 | "!!\n",
368 | "3\n",
369 | "1\n",
370 | "2\n",
371 | "3\n",
372 | "1!!2!!3"
373 | ],
374 | "name": "stdout"
375 | }
376 | ]
377 | },
378 | {
379 | "cell_type": "code",
380 | "metadata": {
381 | "colab": {
382 | "base_uri": "https://localhost:8080/"
383 | },
384 | "id": "PL4-9z0dclxu",
385 | "outputId": "d60e1a23-76db-45db-dbdf-ce7fc8706b57"
386 | },
387 | "source": [
388 | "# Lidemy Project6 String trim LIOJ1041\r\n",
389 | "\r\n",
390 | "string=str(input())\r\n",
391 | "\r\n",
392 | "def trim(string):\r\n",
393 | " New_string=''\r\n",
394 | " \r\n",
395 | " for i in range(len(string)):\r\n",
396 | " if string[i]!=' ':\r\n",
397 | " New_string=string[i:]\r\n",
398 | " break\r\n",
399 | " \r\n",
400 | " print(New_string)\r\n",
401 | " \r\n",
402 | " \r\n",
403 | "trim(string)"
404 | ],
405 | "execution_count": null,
406 | "outputs": [
407 | {
408 | "output_type": "stream",
409 | "text": [
410 | " a b c\n",
411 | "a b c\n"
412 | ],
413 | "name": "stdout"
414 | }
415 | ]
416 | },
417 | {
418 | "cell_type": "code",
419 | "metadata": {
420 | "colab": {
421 | "base_uri": "https://localhost:8080/"
422 | },
423 | "id": "erPbHauSfN_e",
424 | "outputId": "d37f71c6-ec6a-4243-f8f5-8cd747e90264"
425 | },
426 | "source": [
427 | "# Lidemy Project6 String toLowerCase LIOJ1042\r\n",
428 | "\r\n",
429 | "string=str(input())\r\n",
430 | "\r\n",
431 | "# A~Z :65~90\r\n",
432 | "# a~z :97~122\r\n",
433 | "\r\n",
434 | "def toLowerCase(string):\r\n",
435 | " New_string=''\r\n",
436 | " \r\n",
437 | " for i in range(len(string)):\r\n",
438 | "\r\n",
439 | " if 65<=ord(string[i])<=90:\r\n",
440 | " New_string+=str(chr(ord(string[i])+32))\r\n",
441 | "\r\n",
442 | " else: \r\n",
443 | " New_string+=str(string[i])\r\n",
444 | " \r\n",
445 | " print(New_string)\r\n",
446 | " \r\n",
447 | "toLowerCase(string)"
448 | ],
449 | "execution_count": null,
450 | "outputs": [
451 | {
452 | "output_type": "stream",
453 | "text": [
454 | "AbC!!\n",
455 | "abc!!\n"
456 | ],
457 | "name": "stdout"
458 | }
459 | ]
460 | },
461 | {
462 | "cell_type": "code",
463 | "metadata": {
464 | "colab": {
465 | "base_uri": "https://localhost:8080/"
466 | },
467 | "id": "Fph2-OUWg4tl",
468 | "outputId": "c356ef29-e445-40e6-d1ba-9948512404e2"
469 | },
470 | "source": [
471 | "string=str(input())\r\n",
472 | "target=str(input())\r\n",
473 | "\r\n",
474 | "def endwith(string,target):\r\n",
475 | " \r\n",
476 | " \r\n",
477 | " if string[(len(string)-len(target)):len(string)]==target:\r\n",
478 | " print(\"true\")\r\n",
479 | " \r\n",
480 | " else:\r\n",
481 | " print(\"false\")\r\n",
482 | " \r\n",
483 | "endwith(string,target)"
484 | ],
485 | "execution_count": null,
486 | "outputs": [
487 | {
488 | "output_type": "stream",
489 | "text": [
490 | "aaabc\n",
491 | "aa\n",
492 | "bc\n",
493 | "false\n"
494 | ],
495 | "name": "stdout"
496 | }
497 | ]
498 | },
499 | {
500 | "cell_type": "code",
501 | "metadata": {
502 | "colab": {
503 | "base_uri": "https://localhost:8080/"
504 | },
505 | "id": "qW747xFWux1p",
506 | "outputId": "4d8895a2-80b6-4a7a-e264-f2acdd787273"
507 | },
508 | "source": [
509 | ""
510 | ],
511 | "execution_count": null,
512 | "outputs": [
513 | {
514 | "output_type": "stream",
515 | "text": [
516 | "a\n"
517 | ],
518 | "name": "stdout"
519 | }
520 | ]
521 | },
522 | {
523 | "cell_type": "code",
524 | "metadata": {
525 | "colab": {
526 | "base_uri": "https://localhost:8080/"
527 | },
528 | "id": "ToAAdYniuxxF",
529 | "outputId": "499af967-de20-4cc8-8466-3b1faac23459"
530 | },
531 | "source": [
532 | "string=str(input())\r\n",
533 | "length=int(input())\r\n",
534 | "pad=str(input())\r\n",
535 | "\r\n",
536 | "\r\n",
537 | "def padEnd(string,length,pad):\r\n",
538 | "\r\n",
539 | " \r\n",
540 | " if len(string)>=int(length):\r\n",
541 | " print(string)\r\n",
542 | " \r\n",
543 | " \r\n",
544 | " else: \r\n",
545 | " New_str=string\r\n",
546 | "\r\n",
547 | " while int(len(New_str))
"
26 | ]
27 | },
28 | {
29 | "cell_type": "markdown",
30 | "metadata": {
31 | "id": "WjntE0b3_8tg"
32 | },
33 | "source": [
34 | "# 1008 幾個水桶\r\n"
35 | ]
36 | },
37 | {
38 | "cell_type": "code",
39 | "metadata": {
40 | "colab": {
41 | "base_uri": "https://localhost:8080/"
42 | },
43 | "id": "aiQVv1V2JUOX",
44 | "outputId": "3cc6ee61-ba6f-4415-9f2d-dffac367c6e0"
45 | },
46 | "source": [
47 | "#第一個版本\r\n",
48 | "import sys\r\n",
49 | "\r\n",
50 | "M=int(input()) # 多少公升的水\r\n",
51 | "\r\n",
52 | "def CountBucket(M):\r\n",
53 | "\r\n",
54 | " Water=M\r\n",
55 | " if Water<=2:\r\n",
56 | " return 1\r\n",
57 | "\r\n",
58 | " else:\r\n",
59 | " Bucket=0\r\n",
60 | " while Water>0:\r\n",
61 | "\r\n",
62 | " for i in range(31): \r\n",
63 | " if Water<2**i: \r\n",
64 | " tmp=2**(i-1)\r\n",
65 | " break\r\n",
66 | " else:\r\n",
67 | " continue \r\n",
68 | " \r\n",
69 | " Water =Water-tmp \r\n",
70 | " Bucket+=1\r\n",
71 | "\r\n",
72 | " return Bucket\r\n",
73 | " \r\n",
74 | "print(CountBucket(M))"
75 | ],
76 | "execution_count": null,
77 | "outputs": [
78 | {
79 | "output_type": "stream",
80 | "text": [
81 | "20\n",
82 | "2\n"
83 | ],
84 | "name": "stdout"
85 | }
86 | ]
87 | },
88 | {
89 | "cell_type": "code",
90 | "metadata": {
91 | "colab": {
92 | "base_uri": "https://localhost:8080/"
93 | },
94 | "id": "reGf3SPeLwAu",
95 | "outputId": "2a3f2141-931a-42f0-883b-4f47ebe9a10a"
96 | },
97 | "source": [
98 | "#第二個版本\r\n",
99 | "import sys\r\n",
100 | "\r\n",
101 | "M=int(input()) # 多少公升的水\r\n",
102 | "\r\n",
103 | "def CountBucket(M):\r\n",
104 | "\r\n",
105 | " Water=M\r\n",
106 | " if Water<=2:\r\n",
107 | " return 1\r\n",
108 | "\r\n",
109 | " else:\r\n",
110 | " Bucket=0\r\n",
111 | " tmp=0\r\n",
112 | " while Water>0:\r\n",
113 | "\r\n",
114 | " for i in range(31): \r\n",
115 | " if Water<2**i: \r\n",
116 | " tmp=2**(i-1)\r\n",
117 | " break\r\n",
118 | " else:\r\n",
119 | " continue \r\n",
120 | " \r\n",
121 | " Water =Water-tmp \r\n",
122 | " Bucket+=1\r\n",
123 | "\r\n",
124 | " return Bucket\r\n",
125 | " \r\n",
126 | "print(CountBucket(M))"
127 | ],
128 | "execution_count": null,
129 | "outputs": [
130 | {
131 | "output_type": "stream",
132 | "text": [
133 | "20\n",
134 | "2\n"
135 | ],
136 | "name": "stdout"
137 | }
138 | ]
139 | },
140 | {
141 | "cell_type": "code",
142 | "metadata": {
143 | "colab": {
144 | "base_uri": "https://localhost:8080/"
145 | },
146 | "id": "SK6yIcIiLv91",
147 | "outputId": "ad9cc590-82b8-4e51-bbf8-c3fe7d2ad8d3"
148 | },
149 | "source": [
150 | "#第三個版本\r\n",
151 | "import sys\r\n",
152 | "\r\n",
153 | "M=int(input()) # 多少公升的水\r\n",
154 | "\r\n",
155 | "def CountBucket(M):\r\n",
156 | "\r\n",
157 | " Water=M\r\n",
158 | " if Water<=2:\r\n",
159 | " return 1\r\n",
160 | "\r\n",
161 | " else:\r\n",
162 | " Bucket=0\r\n",
163 | " tmp=0\r\n",
164 | " while Water>0:\r\n",
165 | "\r\n",
166 | " for i in range(32): \r\n",
167 | " if Water<2**i: \r\n",
168 | " tmp=2**(i-1)\r\n",
169 | " break\r\n",
170 | " else:\r\n",
171 | " continue \r\n",
172 | " \r\n",
173 | " Water =Water-tmp \r\n",
174 | " Bucket+=1\r\n",
175 | "\r\n",
176 | " return Bucket\r\n",
177 | " \r\n",
178 | "print(CountBucket(M))"
179 | ],
180 | "execution_count": null,
181 | "outputs": [
182 | {
183 | "output_type": "stream",
184 | "text": [
185 | "20\n",
186 | "2\n"
187 | ],
188 | "name": "stdout"
189 | }
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "metadata": {
195 | "colab": {
196 | "base_uri": "https://localhost:8080/"
197 | },
198 | "id": "oVMpA8f1Lv7T",
199 | "outputId": "6fbcb641-95b6-4d4e-95b6-a6cb8d30a15b"
200 | },
201 | "source": [
202 | "# 1018 幾個水桶\r\n",
203 | "import sys\r\n",
204 | "\r\n",
205 | "M=int(input()) # 多少公升的水\r\n",
206 | "\r\n",
207 | "def CountBucket(M):\r\n",
208 | " Count=0\r\n",
209 | " Bucket=0\r\n",
210 | " Water=M\r\n",
211 | " \r\n",
212 | " if Water<=2:\r\n",
213 | " return 1\r\n",
214 | " \r\n",
215 | " else:\r\n",
216 | "\r\n",
217 | " while M>0:\r\n",
218 | " Water=M\r\n",
219 | " while Water>1:\r\n",
220 | "\r\n",
221 | " Water=int(Water/2) \r\n",
222 | " Count+=1\r\n",
223 | "\r\n",
224 | " M=M-2**Count \r\n",
225 | " Bucket+=1 \r\n",
226 | " Count=0\r\n",
227 | " \r\n",
228 | " return Bucket\r\n",
229 | "print(CountBucket(M))"
230 | ],
231 | "execution_count": null,
232 | "outputs": [
233 | {
234 | "output_type": "stream",
235 | "text": [
236 | "20\n",
237 | "2\n"
238 | ],
239 | "name": "stdout"
240 | }
241 | ]
242 | },
243 | {
244 | "cell_type": "code",
245 | "metadata": {
246 | "colab": {
247 | "base_uri": "https://localhost:8080/"
248 | },
249 | "id": "JYj46T11Lv4V",
250 | "outputId": "855c3a5a-d580-43dd-e927-054363c5c77b"
251 | },
252 | "source": [
253 | "#第五個版本\r\n",
254 | "water=int(input())\r\n",
255 | "bucket=0\r\n",
256 | "while water!=0:\r\n",
257 | " if water%2!=0:\r\n",
258 | " bucket+=1\r\n",
259 | " water=int(water/2)\r\n",
260 | "print(bucket)"
261 | ],
262 | "execution_count": null,
263 | "outputs": [
264 | {
265 | "output_type": "stream",
266 | "text": [
267 | "20\n",
268 | "2\n"
269 | ],
270 | "name": "stdout"
271 | }
272 | ]
273 | },
274 | {
275 | "cell_type": "markdown",
276 | "metadata": {
277 | "id": "TZ9IPpjuAAFm"
278 | },
279 | "source": [
280 | "# 1009 倒著念\r\n"
281 | ]
282 | },
283 | {
284 | "cell_type": "code",
285 | "metadata": {
286 | "colab": {
287 | "base_uri": "https://localhost:8080/"
288 | },
289 | "id": "SkDhbv7nAU1H",
290 | "outputId": "a1abeece-4bab-4f5b-be03-ecac1d54349f"
291 | },
292 | "source": [
293 | "S = str(input())\r\n",
294 | "\r\n",
295 | "for i in range(len(S)):\r\n",
296 | " print(S[len(S)-i-1],end='')"
297 | ],
298 | "execution_count": null,
299 | "outputs": [
300 | {
301 | "output_type": "stream",
302 | "text": [
303 | "abc\n",
304 | "cba"
305 | ],
306 | "name": "stdout"
307 | }
308 | ]
309 | },
310 | {
311 | "cell_type": "markdown",
312 | "metadata": {
313 | "id": "anEY45TtBv8O"
314 | },
315 | "source": [
316 | "# 1013 搭電梯"
317 | ]
318 | },
319 | {
320 | "cell_type": "code",
321 | "metadata": {
322 | "colab": {
323 | "base_uri": "https://localhost:8080/"
324 | },
325 | "id": "PeCnSvXGMHpB",
326 | "outputId": "cb9164ca-fb72-4fbb-ca60-b174c93e05b4"
327 | },
328 | "source": [
329 | "N= int(input()) \r\n",
330 | "\r\n",
331 | "def howmany(N):\r\n",
332 | " arr=[0,1,1,2]\r\n",
333 | " for i in range(4,21):\r\n",
334 | " arr.append(arr[i-1]+arr[i-2]) \r\n",
335 | " print(arr[N])\r\n",
336 | "\r\n",
337 | "howmany(N)"
338 | ],
339 | "execution_count": null,
340 | "outputs": [
341 | {
342 | "output_type": "stream",
343 | "text": [
344 | "20\n",
345 | "6765\n"
346 | ],
347 | "name": "stdout"
348 | }
349 | ]
350 | },
351 | {
352 | "cell_type": "markdown",
353 | "metadata": {
354 | "id": "BvDhnXK_N3zf"
355 | },
356 | "source": [
357 | "# 1014 不九人世"
358 | ]
359 | },
360 | {
361 | "cell_type": "code",
362 | "metadata": {
363 | "colab": {
364 | "base_uri": "https://localhost:8080/"
365 | },
366 | "id": "hpoVtgLcIkvo",
367 | "outputId": "9ce5d939-a02c-4b9b-f890-3896a586891d"
368 | },
369 | "source": [
370 | "N =str(input())\r\n",
371 | "\r\n",
372 | "def NoNine(N):\r\n",
373 | " print(int(N,9)) \r\n",
374 | " \r\n",
375 | "NoNine(N)"
376 | ],
377 | "execution_count": 30,
378 | "outputs": [
379 | {
380 | "output_type": "stream",
381 | "text": [
382 | "100\n",
383 | "81\n"
384 | ],
385 | "name": "stdout"
386 | }
387 | ]
388 | },
389 | {
390 | "cell_type": "markdown",
391 | "metadata": {
392 | "id": "lUsudkSMKd-4"
393 | },
394 | "source": [
395 | "# 1016 不合群的人\r\n"
396 | ]
397 | },
398 | {
399 | "cell_type": "code",
400 | "metadata": {
401 | "colab": {
402 | "base_uri": "https://localhost:8080/"
403 | },
404 | "id": "PrBLHqDQKfv_",
405 | "outputId": "15deb255-0304-4201-f458-e8a33967a7e5"
406 | },
407 | "source": [
408 | "# V1\r\n",
409 | "N=int (input())\r\n",
410 | "\r\n",
411 | "def outgroup(N):\r\n",
412 | " arr=[]\r\n",
413 | " Count_A=0\r\n",
414 | " Count_B=0\r\n",
415 | " A=[]\r\n",
416 | " B=[]\r\n",
417 | " for i in range(N):\r\n",
418 | " tmp=str(input())\r\n",
419 | " arr.append(tmp)\r\n",
420 | "\r\n",
421 | " if tmp=='A':\r\n",
422 | " Count_A+=1\r\n",
423 | " A.append(str(i+1))\r\n",
424 | " else:\r\n",
425 | " Count_B+=1\r\n",
426 | " B.append(str(i+1))\r\n",
427 | "\r\n",
428 | " if N==1:\r\n",
429 | " print(\"PEACE\") \r\n",
430 | " elif Count_A==Count_B :\r\n",
431 | " print(\"PEACE\")\r\n",
432 | " elif Count_A==N :\r\n",
433 | " print(\"PEACE\")\r\n",
434 | " elif Count_B==N :\r\n",
435 | " print(\"PEACE\") \r\n",
436 | " elif Count_A> Count_B:\r\n",
437 | " for j in range(len(B)):\r\n",
438 | " print(B[j])\r\n",
439 | " else:\r\n",
440 | " for j in range(len(A)):\r\n",
441 | " print(A[j]) \r\n",
442 | "\r\n",
443 | "outgroup(N)"
444 | ],
445 | "execution_count": 46,
446 | "outputs": [
447 | {
448 | "output_type": "stream",
449 | "text": [
450 | "6\n",
451 | "A\n",
452 | "A\n",
453 | "A\n",
454 | "B\n",
455 | "B\n",
456 | "B\n",
457 | "PEACE\n"
458 | ],
459 | "name": "stdout"
460 | }
461 | ]
462 | },
463 | {
464 | "cell_type": "code",
465 | "metadata": {
466 | "colab": {
467 | "base_uri": "https://localhost:8080/"
468 | },
469 | "id": "cOuEltaKRu3W",
470 | "outputId": "1548e9bd-ae5b-4876-b9e4-77bb81da1129"
471 | },
472 | "source": [
473 | "# V2\r\n",
474 | "N=int (input())\r\n",
475 | "\r\n",
476 | "def outgroup(N):\r\n",
477 | " arr=[]\r\n",
478 | " Count_A=0\r\n",
479 | " Count_B=0\r\n",
480 | " A=[]\r\n",
481 | " B=[]\r\n",
482 | " for i in range(N):\r\n",
483 | " tmp=str(input())\r\n",
484 | " arr.append(tmp)\r\n",
485 | "\r\n",
486 | " if tmp=='A':\r\n",
487 | " Count_A+=1\r\n",
488 | " A.append(str(i+1))\r\n",
489 | " else:\r\n",
490 | " Count_B+=1\r\n",
491 | " B.append(str(i+1))\r\n",
492 | "\r\n",
493 | " if N==1 or Count_A==Count_B or Count_A==N or Count_B==N:\r\n",
494 | " print(\"PEACE\") \r\n",
495 | " \r\n",
496 | " elif Count_A> Count_B:\r\n",
497 | " for j in range(len(B)):\r\n",
498 | " print(B[j])\r\n",
499 | " else:\r\n",
500 | " for j in range(len(A)):\r\n",
501 | " print(A[j]) \r\n",
502 | "\r\n",
503 | "outgroup(N)"
504 | ],
505 | "execution_count": 47,
506 | "outputs": [
507 | {
508 | "output_type": "stream",
509 | "text": [
510 | "5\n",
511 | "A\n",
512 | "A\n",
513 | "A\n",
514 | "B\n",
515 | "B\n",
516 | "4\n",
517 | "5\n"
518 | ],
519 | "name": "stdout"
520 | }
521 | ]
522 | },
523 | {
524 | "cell_type": "code",
525 | "metadata": {
526 | "colab": {
527 | "base_uri": "https://localhost:8080/"
528 | },
529 | "id": "0Daoz9XEfeVO",
530 | "outputId": "d6f8bf96-27bc-4b72-a4b2-9d6dd326696a"
531 | },
532 | "source": [
533 | "# V3\r\n",
534 | "N=int (input())\r\n",
535 | "\r\n",
536 | "def outgroup(N):\r\n",
537 | " arr=[]\r\n",
538 | " Count_A=0\r\n",
539 | " Count_B=0\r\n",
540 | " A=[]\r\n",
541 | " B=[]\r\n",
542 | " for i in range(N):\r\n",
543 | " arr.append(str(input()))\r\n",
544 | "\r\n",
545 | " if arr[i]=='A':\r\n",
546 | " Count_A+=1\r\n",
547 | " \r\n",
548 | " Count_B=N-Count_A\r\n",
549 | "\r\n",
550 | " if N==1 or Count_A==Count_B or Count_A==N or Count_B==N:\r\n",
551 | " print(\"PEACE\") \r\n",
552 | " \r\n",
553 | " else:\r\n",
554 | " wholose= \"A\" if Count_A
"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "metadata": {
30 | "colab": {
31 | "base_uri": "https://localhost:8080/"
32 | },
33 | "id": "aiQVv1V2JUOX",
34 | "outputId": "3cc6ee61-ba6f-4415-9f2d-dffac367c6e0"
35 | },
36 | "source": [
37 | "#第一個版本\r\n",
38 | "import sys\r\n",
39 | "\r\n",
40 | "M=int(input()) # 多少公升的水\r\n",
41 | "\r\n",
42 | "def CountBucket(M):\r\n",
43 | "\r\n",
44 | " Water=M\r\n",
45 | " if Water<=2:\r\n",
46 | " return 1\r\n",
47 | "\r\n",
48 | " else:\r\n",
49 | " Bucket=0\r\n",
50 | " while Water>0:\r\n",
51 | "\r\n",
52 | " for i in range(31): \r\n",
53 | " if Water<2**i: \r\n",
54 | " tmp=2**(i-1)\r\n",
55 | " break\r\n",
56 | " else:\r\n",
57 | " continue \r\n",
58 | " \r\n",
59 | " Water =Water-tmp \r\n",
60 | " Bucket+=1\r\n",
61 | "\r\n",
62 | " return Bucket\r\n",
63 | " \r\n",
64 | "print(CountBucket(M))"
65 | ],
66 | "execution_count": 1,
67 | "outputs": [
68 | {
69 | "output_type": "stream",
70 | "text": [
71 | "20\n",
72 | "2\n"
73 | ],
74 | "name": "stdout"
75 | }
76 | ]
77 | },
78 | {
79 | "cell_type": "code",
80 | "metadata": {
81 | "colab": {
82 | "base_uri": "https://localhost:8080/"
83 | },
84 | "id": "reGf3SPeLwAu",
85 | "outputId": "2a3f2141-931a-42f0-883b-4f47ebe9a10a"
86 | },
87 | "source": [
88 | "#第二個版本\r\n",
89 | "import sys\r\n",
90 | "\r\n",
91 | "M=int(input()) # 多少公升的水\r\n",
92 | "\r\n",
93 | "def CountBucket(M):\r\n",
94 | "\r\n",
95 | " Water=M\r\n",
96 | " if Water<=2:\r\n",
97 | " return 1\r\n",
98 | "\r\n",
99 | " else:\r\n",
100 | " Bucket=0\r\n",
101 | " tmp=0\r\n",
102 | " while Water>0:\r\n",
103 | "\r\n",
104 | " for i in range(31): \r\n",
105 | " if Water<2**i: \r\n",
106 | " tmp=2**(i-1)\r\n",
107 | " break\r\n",
108 | " else:\r\n",
109 | " continue \r\n",
110 | " \r\n",
111 | " Water =Water-tmp \r\n",
112 | " Bucket+=1\r\n",
113 | "\r\n",
114 | " return Bucket\r\n",
115 | " \r\n",
116 | "print(CountBucket(M))"
117 | ],
118 | "execution_count": 2,
119 | "outputs": [
120 | {
121 | "output_type": "stream",
122 | "text": [
123 | "20\n",
124 | "2\n"
125 | ],
126 | "name": "stdout"
127 | }
128 | ]
129 | },
130 | {
131 | "cell_type": "code",
132 | "metadata": {
133 | "colab": {
134 | "base_uri": "https://localhost:8080/"
135 | },
136 | "id": "SK6yIcIiLv91",
137 | "outputId": "ad9cc590-82b8-4e51-bbf8-c3fe7d2ad8d3"
138 | },
139 | "source": [
140 | "#第三個版本\r\n",
141 | "import sys\r\n",
142 | "\r\n",
143 | "M=int(input()) # 多少公升的水\r\n",
144 | "\r\n",
145 | "def CountBucket(M):\r\n",
146 | "\r\n",
147 | " Water=M\r\n",
148 | " if Water<=2:\r\n",
149 | " return 1\r\n",
150 | "\r\n",
151 | " else:\r\n",
152 | " Bucket=0\r\n",
153 | " tmp=0\r\n",
154 | " while Water>0:\r\n",
155 | "\r\n",
156 | " for i in range(32): \r\n",
157 | " if Water<2**i: \r\n",
158 | " tmp=2**(i-1)\r\n",
159 | " break\r\n",
160 | " else:\r\n",
161 | " continue \r\n",
162 | " \r\n",
163 | " Water =Water-tmp \r\n",
164 | " Bucket+=1\r\n",
165 | "\r\n",
166 | " return Bucket\r\n",
167 | " \r\n",
168 | "print(CountBucket(M))"
169 | ],
170 | "execution_count": 3,
171 | "outputs": [
172 | {
173 | "output_type": "stream",
174 | "text": [
175 | "20\n",
176 | "2\n"
177 | ],
178 | "name": "stdout"
179 | }
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "metadata": {
185 | "colab": {
186 | "base_uri": "https://localhost:8080/"
187 | },
188 | "id": "oVMpA8f1Lv7T",
189 | "outputId": "6fbcb641-95b6-4d4e-95b6-a6cb8d30a15b"
190 | },
191 | "source": [
192 | "# 1018 幾個水桶\r\n",
193 | "import sys\r\n",
194 | "\r\n",
195 | "M=int(input()) # 多少公升的水\r\n",
196 | "\r\n",
197 | "def CountBucket(M):\r\n",
198 | " Count=0\r\n",
199 | " Bucket=0\r\n",
200 | " Water=M\r\n",
201 | " \r\n",
202 | " if Water<=2:\r\n",
203 | " return 1\r\n",
204 | " \r\n",
205 | " else:\r\n",
206 | "\r\n",
207 | " while M>0:\r\n",
208 | " Water=M\r\n",
209 | " while Water>1:\r\n",
210 | "\r\n",
211 | " Water=int(Water/2) \r\n",
212 | " Count+=1\r\n",
213 | "\r\n",
214 | " M=M-2**Count \r\n",
215 | " Bucket+=1 \r\n",
216 | " Count=0\r\n",
217 | " \r\n",
218 | " return Bucket\r\n",
219 | "print(CountBucket(M))"
220 | ],
221 | "execution_count": 4,
222 | "outputs": [
223 | {
224 | "output_type": "stream",
225 | "text": [
226 | "20\n",
227 | "2\n"
228 | ],
229 | "name": "stdout"
230 | }
231 | ]
232 | },
233 | {
234 | "cell_type": "code",
235 | "metadata": {
236 | "colab": {
237 | "base_uri": "https://localhost:8080/"
238 | },
239 | "id": "JYj46T11Lv4V",
240 | "outputId": "855c3a5a-d580-43dd-e927-054363c5c77b"
241 | },
242 | "source": [
243 | "#第五個版本\r\n",
244 | "water=int(input())\r\n",
245 | "bucket=0\r\n",
246 | "while water!=0:\r\n",
247 | " if water%2!=0:\r\n",
248 | " bucket+=1\r\n",
249 | " water=int(water/2)\r\n",
250 | "print(bucket)"
251 | ],
252 | "execution_count": 5,
253 | "outputs": [
254 | {
255 | "output_type": "stream",
256 | "text": [
257 | "20\n",
258 | "2\n"
259 | ],
260 | "name": "stdout"
261 | }
262 | ]
263 | }
264 | ]
265 | }
266 |
--------------------------------------------------------------------------------
/Unit7_費氏數列.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "U7_費氏數列.ipynb",
7 | "provenance": [],
8 | "authorship_tag": "ABX9TyOXniFju6zbGJGiKUqul8HH",
9 | "include_colab_link": true
10 | },
11 | "kernelspec": {
12 | "name": "python3",
13 | "display_name": "Python 3"
14 | }
15 | },
16 | "cells": [
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {
20 | "id": "view-in-github",
21 | "colab_type": "text"
22 | },
23 | "source": [
24 | "
"
25 | ]
26 | },
27 | {
28 | "cell_type": "code",
29 | "metadata": {
30 | "colab": {
31 | "base_uri": "https://localhost:8080/"
32 | },
33 | "id": "_XjaXZbXDunU",
34 | "outputId": "c4bfae11-5c17-4fff-c4b0-f4c6f058ae7d"
35 | },
36 | "source": [
37 | "def fibo(n):\r\n",
38 | " if n == 0: return 0;\r\n",
39 | " elif n == 1: return 1;\r\n",
40 | " return fibo(n-1) + fibo(n-2)\r\n",
41 | "print(fibo(5))"
42 | ],
43 | "execution_count": 9,
44 | "outputs": [
45 | {
46 | "output_type": "stream",
47 | "text": [
48 | "5\n"
49 | ],
50 | "name": "stdout"
51 | }
52 | ]
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {
57 | "id": "gGZ7nmflEEPz"
58 | },
59 | "source": [
60 | "# 比較有無快取call 的次數"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "metadata": {
66 | "colab": {
67 | "base_uri": "https://localhost:8080/"
68 | },
69 | "id": "V8Y0RuKdED57",
70 | "outputId": "fb7a377e-5112-48e1-aa4d-f9c795cddde5"
71 | },
72 | "source": [
73 | "def fibo(n):\r\n",
74 | " print(\"call\")\r\n",
75 | " if n == 0: return 0;\r\n",
76 | " elif n == 1: return 1;\r\n",
77 | " return fibo(n-1) + fibo(n-2)\r\n",
78 | "print(fibo(5)) "
79 | ],
80 | "execution_count": 5,
81 | "outputs": [
82 | {
83 | "output_type": "stream",
84 | "text": [
85 | "call\n",
86 | "call\n",
87 | "call\n",
88 | "call\n",
89 | "call\n",
90 | "call\n",
91 | "call\n",
92 | "call\n",
93 | "call\n",
94 | "call\n",
95 | "call\n",
96 | "call\n",
97 | "call\n",
98 | "call\n",
99 | "call\n",
100 | "5\n"
101 | ],
102 | "name": "stdout"
103 | }
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "metadata": {
109 | "colab": {
110 | "base_uri": "https://localhost:8080/"
111 | },
112 | "id": "xG1gw0-XEQVU",
113 | "outputId": "9d310a7b-6c4a-4701-ff88-b76210cbe924"
114 | },
115 | "source": [
116 | "import functools\r\n",
117 | "\r\n",
118 | "@functools.lru_cache()\r\n",
119 | "def fib_lru_cache(n):\r\n",
120 | " print('call')\r\n",
121 | " if n == 0: return 0;\r\n",
122 | " elif n == 1: return 1;\r\n",
123 | " return fib_lru_cache(n-1) + fib_lru_cache(n-2)\r\n",
124 | " \r\n",
125 | "print(fib_lru_cache(5))"
126 | ],
127 | "execution_count": 7,
128 | "outputs": [
129 | {
130 | "output_type": "stream",
131 | "text": [
132 | "call\n",
133 | "call\n",
134 | "call\n",
135 | "call\n",
136 | "call\n",
137 | "call\n",
138 | "5\n"
139 | ],
140 | "name": "stdout"
141 | }
142 | ]
143 | },
144 | {
145 | "cell_type": "markdown",
146 | "metadata": {
147 | "id": "G4An6ehKOQrY"
148 | },
149 | "source": [
150 | "# 尾遞迴"
151 | ]
152 | },
153 | {
154 | "cell_type": "code",
155 | "metadata": {
156 | "colab": {
157 | "base_uri": "https://localhost:8080/"
158 | },
159 | "id": "p0Um7GdmOS1G",
160 | "outputId": "d37a7452-ca82-4a90-e42a-a94281b55049"
161 | },
162 | "source": [
163 | "def fibo_tail(n,result,tmp):\r\n",
164 | " # print('call')\r\n",
165 | " if n == 0: return 0\r\n",
166 | " else:\r\n",
167 | " \r\n",
168 | " return fibo_tail(n - 1, tmp, result + tmp)\r\n",
169 | "\r\n",
170 | "print(fibo_tail(5,0,1))"
171 | ],
172 | "execution_count": 9,
173 | "outputs": [
174 | {
175 | "output_type": "stream",
176 | "text": [
177 | "call\n",
178 | "call\n",
179 | "call\n",
180 | "call\n",
181 | "call\n",
182 | "call\n",
183 | "0\n"
184 | ],
185 | "name": "stdout"
186 | }
187 | ]
188 | },
189 | {
190 | "cell_type": "markdown",
191 | "metadata": {
192 | "id": "0Qx12Wqerl4T"
193 | },
194 | "source": [
195 | "# 疊代作法 - 空間換取時間\r\n"
196 | ]
197 | },
198 | {
199 | "cell_type": "code",
200 | "metadata": {
201 | "colab": {
202 | "base_uri": "https://localhost:8080/"
203 | },
204 | "id": "Uf6zFH9Brp1L",
205 | "outputId": "8df9260e-8794-440b-c458-4fec15a577b3"
206 | },
207 | "source": [
208 | "def fibo_iterative(n):\r\n",
209 | " # print('call')\r\n",
210 | " a,b=0,1\r\n",
211 | " \r\n",
212 | " for _ in range(n):\r\n",
213 | " a,b=b,a+b\r\n",
214 | " return a\r\n",
215 | "\r\n",
216 | "print(fibo_iterative(5)) "
217 | ],
218 | "execution_count": 11,
219 | "outputs": [
220 | {
221 | "output_type": "stream",
222 | "text": [
223 | "55\n"
224 | ],
225 | "name": "stdout"
226 | }
227 | ]
228 | }
229 | ]
230 | }
231 |
--------------------------------------------------------------------------------