{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1번 문제. Lambda operator를 이용한 소수 구하기 구현\n",
    "* List Comprehension 형태로 구현된 소수 구하기 스크립트를 Lambda Operator를 이용하여 구현하시오.(map, reduce, filter)\n",
    "* 한 줄로 구현할 필요는 없습니다. lambda operator를 이용만 하시면 됩니다."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "#List Comprehension 형태\n",
    "MAX = 1000\n",
    "noprimes = [\n",
    "    j\n",
    "    for i in range(2, int(MAX ** 0.5) + 1)\n",
    "    for j in range(i*2, MAX, i)\n",
    "]\n",
    "\n",
    "primes = [\n",
    "    x for x in range(2, MAX) if x not in noprimes\n",
    "]\n",
    "print(primes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "primes = list(filter(lambda x: x not in noprimes, range(2, MAX)))\n",
    "print(primes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "import functools\n",
    "MAX = 1000\n",
    "\n",
    "noprimes = list(map(\n",
    "    lambda i: list(map(\n",
    "            lambda j: j,\n",
    "            range(i*2, MAX, i))),\n",
    "    range(2, int(MAX**0.5) + 1)\n",
    "    ))\n",
    "\n",
    "noprimes = functools.reduce(\n",
    "    lambda x, y: x+y,\n",
    "    noprimes,\n",
    ")\n",
    "\n",
    "primes = list(filter(lambda x: x not in noprimes, range(2, MAX)))\n",
    "print(primes)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2번 문제. replace함수의 구현\n",
    "* 문자열을 치환하는 Python 내장 함수인 replace를 직접 구현하세요."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Fast캠퍼스'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"패스트캠퍼스\".replace(\"패스트\",\"Fast\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'웹프로그래밍 SCHOOL과 데이터 사이언스 SCHOOL'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"웹프로그래밍 스쿨과 데이터 사이언스 스쿨\".replace(\"스쿨\", \"SCHOOL\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* sentence 라는 문자열에서 find_word 를 찾아서 replace_word 로 변환해주는 함수"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def word_replace(sentence, find_word, replace_word):\n",
    "    result = \"\"\n",
    "    i = 0\n",
    "    \n",
    "    while i < len(sentence):\n",
    "        is_exist = True\n",
    "        for j in range(len(find_word)):\n",
    "            if sentence[i+j] != find_word[j]:\n",
    "                is_exist = False\n",
    "                break\n",
    "        if is_exist:\n",
    "            result += replace_word\n",
    "            i += len(find_word)\n",
    "            \n",
    "        else:\n",
    "            result += sentence[i]\n",
    "            i += 1\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'슬로slow캠퍼스slow'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "word_replace(\"슬로슬로우캠퍼스슬로우\",\"슬로우\",\"slow\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "#3번 문제...? 모르겠습니다.\n",
    "def change_weekday(num):\n",
    "    change_dic = {\n",
    "        \"0\": \"월요일\",\n",
    "        \"1\": \"화요일\",\n",
    "        \"2\": \"수요일\",\n",
    "        \"3\": \"목요일\",\n",
    "        \"4\": \"금요일\",\n",
    "        \"5\": \"토요일\",\n",
    "        \"6\": \"일요일\",\n",
    "    }\n",
    "    for key, value in change_dic.items():\n",
    "        num = num.replace(key, str(value))\n",
    "\n",
    "    return num\n",
    "\n",
    "num = int(calendar.weekday(2016, 5, 8))\n",
    "change_weekday(\"3\")\n",
    "\n",
    "\n",
    "#4번 문제...? 모르겠습니다.\n",
    "\n",
    "histogram = input(\"문자열을 입력하세요: \")\n",
    "\n",
    "word_count={}\n",
    "for word in histogram.replace(',','').split():\n",
    "    if word in word_count:\n",
    "        word_count[word]+=1\n",
    "    else:\n",
    "        word_count[word]=1\n",
    "    \n",
    "print(word_count)\n",
    "\n",
    "#5번 문제\n",
    "how_much = int(input())\n",
    "def fibonacci(how_much):\n",
    "    result = \"\"\n",
    "    for i in range(how_much):\n",
    "        result += how_much\n",
    "    return result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "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.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
