<commit_before>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第0000题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": <KEY>",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from PIL import Image, ImageDraw, ImageFont\n",
    "from IPython.display import Image as Ipython_img\n",
    "img_path = r\"C:\\Users\\jiang\\Desktop\\pic.jpg\"\n",
    "font_path = \"c:/Windows/fonts/SIMHEI.ttf\"\n",
    "picture = Image.open(img_path, mode='r')\n",
    "fnt = ImageFont.truetype(font_path, 60)\n",
    "draw = ImageDraw.Draw(picture)\n",
    "draw.text((picture.size[0] - 40, 0), \"4\",font=fnt, fill=(255,0,0,255))\n",
    "# picture.save('test.jpg', 'jpeg')\n",
    "Ipython_img(filename=r\"C:\\Users\\jiang\\test.jpg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 第0001题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['<KEY>', '<KEY>', '<KEY>', '<KEY>', 'VJDKKBVDETJXUEASFGUPENEVCNMTKLXYSTMBNQWKIOAPFLGAKHVYGLVRULEVLBPFTJUQDMDNQJROKBEPQSOXJCUHDDIXBBSQHXIO']\n"
     ]
    }
   ],
   "source": [
    "import numpy as np, string\n",
    "def gen_tickets(tickets_num, tickets_len):\n",
    "    idx = np.random.randint(0, 25, tickets_num * tickets_len)\n",
    "    letters = np.array(list(string.ascii_uppercase))\n",
    "    s = letters[idx].tostring()\n",
    "    return [s[i*tickets_len : (i+1)*tickets_len] for i in range(tickets_num)]\n",
    "\n",
    "## 测试\n",
    "print gen_tickets(5, 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第0002题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np, string\n",
    "def gen_a_ticket(tickets_len):\n",
    "    idx = np.random.randint(0, 25, tickets_len)\n",
    "    letters = np.array(list(string.ascii_uppercase))\n",
    "    s = letters[idx].tostring()\n",
    "    yield s\n",
    "\n",
    "import MySQLdb\n",
    "db = MySQLdb.connect(\"localhost\", \"root\", \"******\", \"database_for_python\")\n",
    "cursor = db.cursor()\n",
    "cursor.execute(\"DROP TABLE IF EXISTS python_tickets\")\n",
    "sql = \"\"\"CREATE TABLE python_tickets(\n",
    "            num INT,\n",
    "            ticket_code CHAR(100))\"\"\"\n",
    "cursor.execute(sql)\n",
    "\n",
    "for i in range(1, 101):\n",
    "    ticket = gen_a_ticket(100).next()\n",
    "    sql = \"INSERT INTO python_tickets VALUES ('%d', '%s')\" % (i, ticket)\n",
    "    cursor.execute(sql)\n",
    "    db.commit()\n",
    "db.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {<commit_msg>complete 0003<commit_after>{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第0000题\n",
    "将你的 QQ 头像（或者微博头像）右上角加上红色的数字，类似于微信未读信息数量那种提示效果。 类似于图中效果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/jpeg": <KEY>GO\nwj/QM/8Abb/7Giit/qtLt+ZxrEVe5ZHjzAH/ABLf/I//ANjRRRU/VKP8v5l/Wq3f8j//2Q==\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from PIL import Image, ImageDraw, ImageFont\n",
    "from IPython.display import Image as Ipython_img\n",
    "img_path = r\"C:\\Users\\jiang\\Desktop\\pic.jpg\"\n",
    "font_path = \"c:/Windows/fonts/SIMHEI.ttf\"\n",
    "picture = Image.open(img_path, mode='r')\n",
    "fnt = ImageFont.truetype(font_path, 60)\n",
    "draw = ImageDraw.Draw(picture)\n",
    "draw.text((picture.size[0] - 40, 0), \"4\",font=fnt, fill=(255,0,0,255))\n",
    "# picture.save('test.jpg', 'jpeg')\n",
    "Ipython_img(filename=r\"C:\\Users\\jiang\\test.jpg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 第0001题\n",
    "做为 Apple Store App 独立开发者，你要搞限时促销，为你的应用生成激活码（或者优惠券），使用 Python 如何生成 200 个激活码（或者优惠券）？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['DXJWBMUBRUJJEUWVYSXIQRAEKYKLC<KEY>', '<KEY>', '<KEY>', '<KEY>', '<KEY>']\n"
     ]
    }
   ],
   "source": [
    "import numpy as np, string\n",
    "def gen_tickets(tickets_num, tickets_len):\n",
    "    idx = np.random.randint(0, 25, tickets_num * tickets_len)\n",
    "    letters = np.array(list(string.ascii_uppercase))\n",
    "    s = letters[idx].tostring()\n",
    "    return [s[i*tickets_len : (i+1)*tickets_len] for i in range(tickets_num)]\n",
    "\n",
    "## 测试\n",
    "print gen_tickets(5, 100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第0002题\n",
    "将 0001 题生成的 200 个激活码（或者优惠券）保存到 MySQL 关系型数据库中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np, string\n",
    "def gen_a_ticket(tickets_len):\n",
    "    idx = np.random.randint(0, 25, tickets_len)\n",
    "    letters = np.array(list(string.ascii_uppercase))\n",
    "    s = letters[idx].tostring()\n",
    "    yield s\n",
    "\n",
    "import MySQLdb\n",
    "db = MySQLdb.connect(\"localhost\", \"root\", \"******\", \"database_for_python\")\n",
    "cursor = db.cursor()\n",
    "cursor.execute(\"DROP TABLE IF EXISTS python_tickets\")\n",
    "sql = \"\"\"CREATE TABLE python_tickets(\n",
    "            num INT,\n",
    "            ticket_code CHAR(100))\"\"\"\n",
    "cursor.execute(sql)\n",
    "\n",
    "for i in range(1, 201):\n",
    "    ticket = gen_a_ticket(100).next()\n",
    "    sql = \"INSERT INTO python_tickets VALUES ('%d', '%s')\" % (i, ticket)\n",
    "    cursor.execute(sql)\n",
    "    db.commit()\n",
    "db.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 第0003题\n",
    "将 0001 题生成的 200 个激活码（或者优惠券）保存到 Redis 非关系型数据库中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BXDOGQPIPQPMEDPLOJRAOAFUOFJPMBMWANNVFDYMFGXFKRQKWBYQOOJRQGTIJYEQKOKBYTSXILDFULVNHYAKQYRKMILWMCPNXAKQ\n"
     ]
    }
   ],
   "source": [
    "import numpy as np, string\n",
    "def gen_a_ticket(tickets_len):\n",
    "    idx = np.random.randint(0, 25, tickets_len)\n",
    "    letters = np.array(list(string.ascii_uppercase))\n",
    "    s = letters[idx].tostring()\n",
    "    yield s\n",
    "\n",
    "import redis\n",
    "rd = redis.Redis(host='localhost', port=6379, db=0)\n",
    "for i in range(1, 201):\n",
    "    ticket = gen_a_ticket(100).next()\n",
    "    rd.set(i, ticket)\n",
    "rd.save()\n",
    "\n",
    "# test\n",
    "print rd.get(55)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 第0004题\n",
    "任一个英文的纯文本文件，统计其中的单词出现的个数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "you 20\n",
      "the 18\n",
      "to 17\n",
      "if 11\n",
      "your 11\n",
      "and 10\n",
      "file 10\n",
      "or 10\n",
      "pages 9\n",
      "as 8\n",
      "github 8\n",
      "with 8\n",
      "in 7\n",
      "that 7\n",
      "have 6\n",
      "jekyll 6\n",
      "layout 6\n",
      "page 6\n",
      "these 6\n",
      "by 5\n",
      "can 5\n",
      "don 5\n",
      "ll 5\n",
      "markdown 5\n",
      "now 5\n",
      "repository 5\n",
      "site 5\n",
      "we 5\n",
      "all 4\n",
      "an 4\n",
      "are 4\n",
      "be 4\n",
      "default 4\n",
      "for 4\n",
      "from 4\n",
      "index 4\n",
      "of 4\n",
      "on 4\n",
      "any 3\n",
      "build 3\n",
      "config 3\n",
      "doesn 3\n",
      "existing 3\n",
      "features 3\n",
      "files 3\n",
      "may 3\n",
      "publishing 3\n",
      "readme 3\n",
      "theme 3\n",
      "title 3\n",
      "use 3\n",
      "website 3\n",
      "adding 2\n",
      "additional 2\n",
      "affect 2\n",
      "at 2\n",
      "changes 2\n",
      "content 2\n",
      "elsewhere 2\n",
      "exclude 2\n",
      "expect 2\n",
      "explicitly 2\n",
      "feel 2\n",
      "few 2\n",
      "front 2\n",
      "get 2\n",
      "how 2\n",
      "it 2\n",
      "just 2\n",
      "like 2\n",
      "look 2\n",
      "matter 2\n",
      "more 2\n",
      "most 2\n",
      "need 2\n",
      "re 2\n",
      "set 2\n",
      "shouldn 2\n",
      "sites 2\n",
      "software 2\n",
      "specify 2\n",
      "such 2\n",
      "there 2\n",
      "three 2\n",
      "via 2\n",
      "100th 1\n",
      "above 1\n",
      "activate 1\n",
      "add 1\n",
      "advanced 1\n",
      "again 1\n",
      "allow 1\n",
      "already 1\n",
      "although 1\n",
      "appears 1\n",
      "assign 1\n",
      "authoring 1\n",
      "automatically 1\n",
      "based 1\n",
      "begin 1\n",
      "begins 1\n",
      "behind 1\n",
      "bring 1\n",
      "browse 1\n",
      "browser 1\n",
      "but 1\n",
      "clicks 1\n",
      "commit 1\n",
      "context 1\n",
      "continue 1\n",
      "control 1\n",
      "course 1\n",
      "create 1\n",
      "customizable 1\n",
      "customizations 1\n",
      "dependency 1\n",
      "directive 1\n",
      "directory 1\n",
      "disable 1\n",
      "dissimilar 1\n",
      "docs 1\n",
      "document 1\n",
      "documentation 1\n",
      "doing 1\n",
      "each 1\n",
      "easily 1\n",
      "either 1\n",
      "exact 1\n",
      "example 1\n",
      "exist 1\n",
      "experience 1\n",
      "explicit 1\n",
      "familiar 1\n",
      "far 1\n",
      "fewer 1\n",
      "find 1\n",
      "first 1\n",
      "folder 1\n",
      "given 1\n",
      "gotchas 1\n",
      "h1 1\n",
      "h2 1\n",
      "h3 1\n",
      "happy 1\n",
      "heading 1\n",
      "headings 1\n",
      "html 1\n",
      "implemented 1\n",
      "improvements 1\n",
      "interested 1\n",
      "interface 1\n",
      "iterates 1\n",
      "its 1\n",
      "know 1\n",
      "layouts 1\n",
      "line 1\n",
      "list 1\n",
      "matches 1\n",
      "md 1\n",
      "metadata 1\n",
      "minimal 1\n",
      "namely 1\n",
      "navigate 1\n",
      "needing 1\n",
      "nojekyll 1\n",
      "not 1\n",
      "null 1\n",
      "one 1\n",
      "open 1\n",
      "opting 1\n",
      "optional 1\n",
      "other 1\n",
      "overriding 1\n",
      "own 1\n",
      "places 1\n",
      "please 1\n",
      "plugins 1\n",
      "possible 1\n",
      "potential 1\n",
      "process 1\n",
      "project 1\n",
      "publish 1\n",
      "questions 1\n",
      "quickly 1\n",
      "reason 1\n",
      "rendered 1\n",
      "requires 1\n",
      "root 1\n",
      "safely 1\n",
      "saving 1\n",
      "scenes 1\n",
      "separated 1\n",
      "serve 1\n",
      "settings 1\n",
      "should 1\n",
      "simplify 1\n",
      "simply 1\n",
      "so 1\n",
      "some 1\n",
      "source 1\n",
      "specified 1\n",
      "step 1\n",
      "steps 1\n",
      "styles 1\n",
      "tabs 1\n",
      "them 1\n",
      "things 1\n",
      "those 1\n",
      "through 1\n",
      "titles 1\n",
      "top 1\n",
      "touch 1\n",
      "transparent 1\n",
      "two 1\n",
      "unset 1\n",
      "unstyled 1\n",
      "us 1\n",
      "users 1\n",
      "values 1\n",
      "vendored 1\n",
      "want 1\n",
      "web 1\n",
      "what 1\n",
      "when 1\n",
      "which 1\n",
      "while 1\n",
      "will 1\n",
      "within 1\n",
      "would 1\n",
      "yaml 1\n"
     ]
    }
   ],
   "source": [
    "#coding=utf-8\n",
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "import numpy as np\n",
    "vectorizer = CountVectorizer()\n",
    "text_path = r\"C:\\Users\\jiang\\Desktop\\Github_Pages.txt\"  #utf-8 without BOM\n",
    "with open(text_path, 'r') as f:\n",
    "    s = f.readlines()\n",
    "    X = vectorizer.fit_transform(s)\n",
    "words = vectorizer.get_feature_names()\n",
    "counts = np.sum(X.toarray(), 0).tolist()\n",
    "results = sorted(zip(words, counts), key=lambda x:x[1], reverse=True)\n",
    "for word, count in results: # print results\n",
    "    print word, count"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
{'fraction_non_alphanumeric': 0.21384030418250952, 'fraction_numerical': 0.0311787072243346, 'mean_word_length': 1.9230940208935319, 'pattern_counts': {'":': 111, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '730372', 'n_tokens_mistral': 5992, 'n_tokens_neox': 5547, 'n_words': 1003}