Skip to content

Instantly share code, notes, and snippets.

@kanjirz50
Created June 9, 2017 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanjirz50/88d8479b2120de13c4f03a117633f039 to your computer and use it in GitHub Desktop.
Save kanjirz50/88d8479b2120de13c4f03a117633f039 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 第1章:準備運動"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 00.文字列の逆順"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:11.855920Z",
"start_time": "2017-06-09T04:25:11.849857Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"def q00():\n",
" string = \"stressed\"\n",
" return list(reversed(string))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:12.269393Z",
"start_time": "2017-06-09T04:25:12.265296Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"assert q00(), \"desserts\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 01. 「パタトクカシーー」"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:13.016422Z",
"start_time": "2017-06-09T04:25:13.008446Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"def q01():\n",
" string = \"パタトクカシーー\"\n",
" chars = [c for i, c in enumerate(string, start=1) if i in (1, 3, 5, 7)]\n",
" return \"\".join(chars)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:13.340412Z",
"start_time": "2017-06-09T04:25:13.336288Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"assert q01(), \"パトカー\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 02. 「パトカー」+「タクシー」=「パタトクカシーー」"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:13.943490Z",
"start_time": "2017-06-09T04:25:13.936597Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"def q02():\n",
" string1 = \"パトカー\"\n",
" string2 = \"タクシー\"\n",
" return \"\".join([c1 + c2 for c1, c2 in zip(string1, string2)])"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:14.205836Z",
"start_time": "2017-06-09T04:25:14.201759Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"assert q02(), \"パタトクカシーー\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 03. 円周率"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:14.790748Z",
"start_time": "2017-06-09T04:25:14.777150Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"symbols = (\",\", \".\")\n",
"def delete_symbol(c):\n",
" for symbol in symbols:\n",
" c = c.replace(symbol, \"\")\n",
" return c\n",
" \n",
"def q03():\n",
" sentence = \"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.\"\n",
" return [len(delete_symbol(word)) for word in sentence.split()]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:15.077034Z",
"start_time": "2017-06-09T04:25:15.072288Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"assert q03(), [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 04. 元素記号"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:15.725014Z",
"start_time": "2017-06-09T04:25:15.704742Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"def q04():\n",
" sentence = \"Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.\"\n",
" word_position = {}\n",
" \n",
" symbol_deleted_sentence = delete_symbol(sentence)\n",
"\n",
" for i, word in enumerate(symbol_deleted_sentence.split(), start=1):\n",
" if i in (1, 5, 6, 7, 8, 9, 15, 16, 19):\n",
" char = word[0]\n",
" else:\n",
" char = word[:2]\n",
" word_position[char] = i\n",
" return word_position"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:16.757877Z",
"start_time": "2017-06-09T04:25:16.752537Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'H': 1, 'He': 2, 'Li': 3, 'Be': 4, 'B': 5, 'C': 6, 'N': 7, 'O': 8, 'F': 9, 'Ne': 10, 'Na': 11, 'Mi': 12, 'Al': 13, 'Si': 14, 'P': 15, 'S': 16, 'Cl': 17, 'Ar': 18, 'K': 19, 'Ca': 20}\n"
]
}
],
"source": [
"print(q04())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 05. n-gram"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:18.374561Z",
"start_time": "2017-06-09T04:25:18.368842Z"
},
"collapsed": true
},
"outputs": [],
"source": [
"def q05(words, n=2):\n",
" return [words[i:i+n] for i in range(len(words)-n+1)]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2017-06-09T04:25:18.751725Z",
"start_time": "2017-06-09T04:25:18.745007Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er']\n",
"[['I', 'am'], ['am', 'an'], ['an', 'NLPer']]\n"
]
}
],
"source": [
"sentence = \"I am an NLPer\"\n",
"print(q05(sentence))\n",
"print(q05(sentence.split()))"
]
},
{
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment