Last active
December 18, 2017 18:48
-
-
Save wasdee/ef99c5414b59ef1bf626943b6c2f6ca3 to your computer and use it in GitHub Desktop.
ปัญหา 1 รหัสสามชุด
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# แก้ปัญหาด้วย python\n", | |
"วันหนึ่งเราลองดูยูทูปไปเรื่อยๆ แล้วก็ถูกท้าท้ายด้วยปัญหานี้เข้า" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/7Vd1dTBVbFg\" frameborder=\"0\" gesture=\"media\" allow=\"encrypted-media\" allowfullscreen></iframe>" | |
], | |
"text/plain": [ | |
"<IPython.core.display.HTML object>" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"from IPython.display import HTML\n", | |
"HTML(\n", | |
" \"\"\"<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/7Vd1dTBVbFg\" frameborder=\"0\" gesture=\"media\" allow=\"encrypted-media\" allowfullscreen></iframe>\"\"\"\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"เราลองแก้ปัญหาง่ายๆนี้แล้วทำให้เราได้เรียนรู้อะไรหลายอย่างเกี่ยวกับ python จึงอยากมาแบ่งปันให้ดู" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Python Standard library ที่ต้องใช้" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import itertools # ต้องรู้จัก 5 ดาว มีหลายฟังค์ชั่น\n", | |
"import operator # ต้องรู้จัก 2 ดาว รวมฟังค์ชั่น พื้นฐาน บวกลบคุณหาร (ปกติใช้แต่สัญลักษณ์ถ้าจะเรียกเป็นฟังก์ชั่น งงดิ ว่าจะทำงาย)\n", | |
"import functools # ต้องรู้จัก 1 ดาว reduce" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"คูณไปเรื่อยๆ = lambda x: functools.reduce(operator.mul, x, 1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"from sympy.ntheory import factorint # ต้องรู้จัก 5 ดาว คณิตฯต่างๆ ดิฟด้วยโค้ด" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"เลขโดดสิบตัว = list(range(10))\n", | |
"เลขโดดสิบตัว" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"เลขโดดสามหลัก = itertools.product(digit,repeat=3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"<itertools.product at 0x1134f1b88>" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"เลขโดดสามหลัก" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"เลขโดดเราเป็น generator นะครับ ใครไม่รู้จัก python generator ถามอากู๋เลย" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"คำตอบตามสามเงื่อนไข = []\n", | |
"คำตอบตามสองเงื่อนไข = {}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"for a, b, c in เลขโดดสามหลัก:\n", | |
" ans = [a,b,c]\n", | |
" s = sum(ans)\n", | |
" m = functools.reduce(operator.mul, ans, 1)\n", | |
" if a <= b and b <=c:\n", | |
" if m == 36:\n", | |
" try:\n", | |
" คำตอบตามสองเงื่อนไข[s].append(ans)\n", | |
" except KeyError:\n", | |
" คำตอบตามสองเงื่อนไข[s] = []\n", | |
" คำตอบตามสองเงื่อนไข[s].append(ans)\n", | |
" if c not in [a,b]:\n", | |
" คำตอบตามสามเงื่อนไข.append(ans)\n", | |
" \n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 52, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{10: [[3, 3, 4]], 11: [[2, 3, 6]], 13: [[1, 6, 6], [2, 2, 9]], 14: [[1, 4, 9]]}" | |
] | |
}, | |
"execution_count": 52, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"คำตอบตามสองเงื่อนไข" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 53, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[1, 4, 9], [2, 2, 9], [2, 3, 6], [3, 3, 4]]" | |
] | |
}, | |
"execution_count": 53, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"คำตอบตามสามเงื่อนไข" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## ดูเฉลยแล้ว ปรากฎว่าเลขสามชุดไม่ต้องเป็นหลักเดียว\n", | |
"แก้สิครับ รออะไร" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"หาตัวประกอบจำนวนเต็ม" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[2, 2, 3, 3]" | |
] | |
}, | |
"execution_count": 31, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"f = factorint(36,multiple=True);f" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[2, 3, 6, 9, 4, 12, 18, 36, 1]" | |
] | |
}, | |
"execution_count": 34, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"g = [mulSum(y) for x in range(len(f)) for y in set(itertools.combinations(f,x+1))] + [1];g" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"allP = itertools.product(g,repeat=3)\n", | |
"ANS = []\n", | |
"keys_sum = {}\n", | |
"for a, b, c in allP:\n", | |
" ans = [a,b,c]\n", | |
" s = sum(ans)\n", | |
" m = functools.reduce(operator.mul, ans, 1)\n", | |
" if a <= b and b <=c:\n", | |
" if m == 36:\n", | |
" try:\n", | |
" keys_sum[s].append(ans)\n", | |
" except KeyError:\n", | |
" keys_sum[s] = []\n", | |
" keys_sum[s].append(ans)\n", | |
" if c not in [a,b]:\n", | |
" ANS.append(ans)\n", | |
" \n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[[2, 2, 9],\n", | |
" [2, 3, 6],\n", | |
" [3, 3, 4],\n", | |
" [1, 2, 18],\n", | |
" [1, 3, 12],\n", | |
" [1, 4, 9],\n", | |
" [1, 1, 36]]" | |
] | |
}, | |
"execution_count": 36, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"ANS" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{10: [[3, 3, 4]],\n", | |
" 11: [[2, 3, 6]],\n", | |
" 13: [[2, 2, 9], [1, 6, 6]],\n", | |
" 14: [[1, 4, 9]],\n", | |
" 16: [[1, 3, 12]],\n", | |
" 21: [[1, 2, 18]],\n", | |
" 38: [[1, 1, 36]]}" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"keys_sum" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment