{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## 06-1 내가 프로그램을 만들 수 있을까?\n", "\n", "> 프로그램을 만들려면 가장 먼저 \"입력\"과 \"출력\"을 생각하라.\n", "\n", "### 구구단 프로그램 작성하기\n", "\n", "- 함수 이름은 GuGu\n", "- 프로그램 실행 시 몇단을 출력할지 입력을 받는다.\n", "- 출력형태: 2 * 1 = 2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def GuGu():\n", " n = int(input(\"몇 단을 출력하시겠습니까? \"))\n", " \n", " for i in range(1, 10):\n", " print(\"%d * %d = %d\" % (n, i, n*i))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "몇 단을 출력하시겠습니까? 3\n", "3 * 1 = 3\n", "3 * 2 = 6\n", "3 * 3 = 9\n", "3 * 4 = 12\n", "3 * 5 = 15\n", "3 * 6 = 18\n", "3 * 7 = 21\n", "3 * 8 = 24\n", "3 * 9 = 27\n" ] } ], "source": [ "GuGu()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "몇 단을 출력하시겠습니까? 9\n", "9 * 1 = 9\n", "9 * 2 = 18\n", "9 * 3 = 27\n", "9 * 4 = 36\n", "9 * 5 = 45\n", "9 * 6 = 54\n", "9 * 7 = 63\n", "9 * 8 = 72\n", "9 * 9 = 81\n" ] } ], "source": [ "GuGu()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## 06-2 3과 5의 배수 합하기\n", "\n", ">- 10 미만의 자연수에서 3과 5의 배수를 구하면 3, 5, 6, 9이다. 이들의 총합은 23이다.\n", ">- 1000 미만의 자연수에서 3의 배수와 5의 배수의 총합을 구하라." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "233168\n" ] } ], "source": [ "sum = 0\n", "\n", "for n in range(1, 1000):\n", " if n%3 == 0 or n%5 == 0:\n", " sum += n\n", " \n", "print(sum)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## 06-3 게시판 페이징하기\n", "\n", "- 함수 이름은? getTotalPage\n", "- 입력 받는 값은? 게시물의 총 건수(m), 한 페이지에 보여줄 게시물 수(n)\n", "- 출력하는 값은? 총 페이지수" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n", "2\n", "3\n", "3\n" ] } ], "source": [ "def getTotalPage(m, n):\n", " if m % n == 0:\n", " return m // n\n", " else:\n", " return m // n + 1\n", " \n", "print(getTotalPage(5, 10))\n", "print(getTotalPage(15, 10))\n", "print(getTotalPage(25, 10))\n", "print(getTotalPage(30, 10))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## 06-4 간단한 메모장 만들기\n", "\n", "원하는 메모를 파일에 저장하고 추가 및 조회가 가능한 간단한 메모장을 만들어 보자.\n", "\n", "- 필요한 기능은? 메모 추가하기, 메모 조회하기\n", "- 입력 받는 값은? 메모 내용, 프로그램 실행 옵션\n", "- 출력하는 값은? memo.txt\n", "\n", "\n", "- 다음과 같은 명령을 실행했을 때 메모를 추가할 수 있도록 만들어 보자.\n", "\n", "> python memo.py -a \"Life is too short\"" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing memo.py\n" ] } ], "source": [ "%%writefile memo.py\n", "import sys\n", "\n", "option = sys.argv[1]\n", "memo = sys.argv[2]\n", "\n", "if option == \"-a\":\n", " f = open(\"memo.txt\", \"a\")\n", " f.write(memo)\n", " f.write(\"\\n\")\n", " f.close()\n", "elif option == \"-v\":\n", " f = open(\"memo.txt\", \"r\")\n", " memo = f.read()\n", " f.close()\n", " print(memo)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%run memo.py -a \"Life is too short\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "import sys\r\n", "\r\n", "option = sys.argv[1]\r\n", "memo = sys.argv[2]\r\n", "\r\n", "if option == \"-a\":\r\n", " f = open(\"memo.txt\", \"a\")\r\n", " f.write(memo)\r\n", " f.write(\"\\n\")\r\n", " f.close()\r\n", "elif option == \"-v\":\r\n", " f = open(\"memo.txt\", \"r\")\r\n", " memo = f.read()\r\n", " f.close()\r\n", " print(memo)\r\n" ] } ], "source": [ "!more memo.py" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Life is too short\r\n" ] } ], "source": [ "!more memo.txt" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%run memo.py -a \"You need python\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Life is too short\r\n", "You need python\r\n" ] } ], "source": [ "!more memo.txt" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m/workspace/myNotebook/점프 투 파이썬/memo.py\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0moption\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margv\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mmemo\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margv\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0moption\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"-a\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "%run memo.py -v" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting memo.py\n" ] } ], "source": [ "%%writefile memo.py\n", "import sys\n", "\n", "option = sys.argv[1]\n", "\n", "if option == \"-a\":\n", " memo = sys.argv[2]\n", " f = open(\"memo.txt\", \"a\")\n", " f.write(memo)\n", " f.write(\"\\n\")\n", " f.close()\n", "elif option == \"-v\":\n", " f = open(\"memo.txt\", \"r\")\n", " memo = f.read()\n", " f.close()\n", " print(memo)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Life is too short\n", "You need python\n", "\n" ] } ], "source": [ "%run memo.py -v" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "---\n", "## 06-5 탭을 4개의 공백으로 바꾸기\n", "\n", "문서 파일을 읽어서 그 문서 파일 내에 있는 탭(tab)을 공백(space) 4개로 바꾸어주는 스크립트를 작성해 보자.\n", "\n", "- 필요한 기능은? 문서 파일 읽어 들이기, 문자열 변경하기\n", "- 입력 받는 값은? 탭을 포함한 문서 파일\n", "- 출력하는 값은? 탭이 공백으로 수정된 문서 파일\n", "\n", "\n", "- 다음과 같은 형식으로 프로그램이 수행되도록 만들 것이다.\n", "\n", "> python tabto4.py src dst" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing a.txt\n" ] } ], "source": [ "%%writefile a.txt\n", "Life\tis \ttoo\tshort\n", "You\tneed\tPython" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Life\tis \ttoo\tshort\r\n", "You\tneed\tPython\r\n" ] } ], "source": [ "!more a.txt" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Writing tabto2.py\n" ] } ], "source": [ "%%writefile tabto2.py\n", "import sys\n", "\n", "src = sys.argv[1]\n", "dst = sys.argv[2]\n", "\n", "f = open(src, \"r\")\n", "tab = f.read()\n", "f.close()\n", "\n", "f = open(dst, \"w\")\n", "f.write(tab.replace(\"\\t\", \" \"*2))\n", "f.close()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%run tabto2.py a.txt b.txt" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Life is too short\r\n", "You need Python\r\n" ] } ], "source": [ "!more b.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## 06-6 하위 디렉터리 검색하기\n", "\n", "- 특정 디렉터리부터 시작해서 그 하위의 모든 파일 중 파이썬 파일만 출력" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['04-sys2.py', 'a.txt', '__pycache__', '06장 파이썬 프로그래밍, 어떻게 시작해야 할까?.ipynb', '05장 파이썬 날개달기.ipynb', '04장 프로그램의 입력과 출력은 어떻게 해야 할까?.ipynb', 'argv_test.py', '03장 프로그램의 구조를 쌓는다! 제어문.ipynb', 'foo.txt', 'argv_test2.py', 'memo.txt', 'mod1.py', 'memo.py', 'test.txt', 'tabto2.py', '.ipynb_checkpoints', '04-sys1.py', 'b.txt', 'test', '02장 파이썬 프로그래밍의 기초, 자료형.ipynb', '04-새파일.txt', 'game', '01장 파이썬이란 무엇인가?.ipynb']\n" ] } ], "source": [ "import os\n", "\n", "print(os.listdir(\"./\"))" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "./04-sys2.py\n", "./a.txt\n", "./__pycache__\n", "./06장 파이썬 프로그래밍, 어떻게 시작해야 할까?.ipynb\n", "./05장 파이썬 날개달기.ipynb\n", "./04장 프로그램의 입력과 출력은 어떻게 해야 할까?.ipynb\n", "./argv_test.py\n", "./03장 프로그램의 구조를 쌓는다! 제어문.ipynb\n", "./foo.txt\n", "./argv_test2.py\n", "./memo.txt\n", "./mod1.py\n", "./memo.py\n", "./test.txt\n", "./tabto2.py\n", "./.ipynb_checkpoints\n", "./04-sys1.py\n", "./b.txt\n", "./test\n", "./02장 파이썬 프로그래밍의 기초, 자료형.ipynb\n", "./04-새파일.txt\n", "./game\n", "./01장 파이썬이란 무엇인가?.ipynb\n" ] } ], "source": [ "filenames = os.listdir(\"./\")\n", "\n", "for filename in filenames:\n", " fullname = os.path.join(\"./\", filename)\n", " print(fullname)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def search(dirname):\n", " filenames = os.listdir(dirname)\n", " for filename in filenames:\n", " fullname = os.path.join(dirname, filename)\n", " ext = os.path.splitext(fullname)[-1]\n", " if ext == '.py':\n", " print(fullname)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "./04-sys2.py\n", "./argv_test.py\n", "./argv_test2.py\n", "./mod1.py\n", "./memo.py\n", "./tabto2.py\n", "./04-sys1.py\n" ] } ], "source": [ "search('./')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def search(dirname):\n", " try:\n", " filenames = os.listdir(dirname)\n", " for filename in filenames:\n", " fullname = os.path.join(dirname, filename)\n", " if os.path.isdir(fullname):\n", " search(fullname)\n", " else:\n", " ext = os.path.splitext(fullname)[-1]\n", " if ext == '.py':\n", " print(fullname)\n", " except PermissionError:\n", " pass" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "./04-sys2.py\n", "./argv_test.py\n", "./argv_test2.py\n", "./mod1.py\n", "./memo.py\n", "./tabto2.py\n", "./04-sys1.py\n", "./game/graphic/render.py\n", "./game/graphic/__init__.py\n", "./game/__init__.py\n", "./game/sound/echo.py\n", "./game/sound/__init__.py\n" ] } ], "source": [ "search(\"./\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [하위 디렉터리 검색을 쉽게 해주는 os.walk]\n", "\n", "- 시작 디렉터리부터 시작하여 그 하위의 모든 디렉터리를 차례대로 방문하게 해주는 함수" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ ".//04-sys2.py\n", ".//argv_test.py\n", ".//argv_test2.py\n", ".//mod1.py\n", ".//memo.py\n", ".//tabto2.py\n", ".//04-sys1.py\n", "./game/__init__.py\n", "./game/graphic/render.py\n", "./game/graphic/__init__.py\n", "./game/sound/echo.py\n", "./game/sound/__init__.py\n" ] } ], "source": [ "import os\n", "\n", "for (path, dir, files) in os.walk(\"./\"):\n", " for filename in files:\n", " ext = os.path.splitext(filename)[-1]\n", " if ext == '.py':\n", " print(\"%s/%s\" % (path, filename))" ] } ], "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.4.3" } }, "nbformat": 4, "nbformat_minor": 2 }