Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Last active October 8, 2020 03:07
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 takatakamanbou/26aed9fa2071a7b7a8727114c144d5df to your computer and use it in GitHub Desktop.
Save takatakamanbou/26aed9fa2071a7b7a8727114c144d5df to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "qqyvt9JCC8Sc"
},
"source": [
"# AProg2020 ex03 課題A\n",
"\n",
"https://www-tlab.math.ryukoku.ac.jp/wiki/?AProg/2020/ex03#kadaiA"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 準備\n",
"以下のセルの内容を指示通り修正して実行してください.指示に従っていない場合,課題のチェックはされません."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-2-6eb7f89973c6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmyID\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mstr\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmyID\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mdidItMyself\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"myID = '' # この変数に文字列として自分の学籍番号を書く\n",
"# この notebook を自分のPC環境で自分がやったというひとは, \n",
"# 以下の False を True に書き換える\n",
"didItMyself = False\n",
"\n",
"### このセルのここから下はいじってはいけません ###\n",
"\n",
"\n",
"assert type(myID) == str and len(myID) > 0\n",
"assert didItMyself\n",
"\n",
"import sys\n",
"import os\n",
"import getpass\n",
"print(sys.platform)\n",
"print(getpass.getuser())\n",
"print(os.getcwd())\n",
"print(myID)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "psZsyh_gI4Hu"
},
"source": [
"## P1\n",
"\n",
"次のセルを実行して整数を入力すると,その整数が 10 未満か,10以上20未満か,20以上かに応じて,「上旬です」,「中旬です」,「下旬です」と表示するようにしてください."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "psZsyh_gI4Hu"
},
"outputs": [],
"source": [
"# このセルを修正してね\n",
"day = input() # input関数の戻り値は文字列なので,この行も修正が必要\n",
"print(day)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "psZsyh_gI4Hu"
},
"source": [
"条件式の書き方に注意.プログラミングの練習ですので,正しく動作する場合でも,不要な(無駄な)条件判定をしてる場合は減点されます.\n",
"例えば,「変数 `s` の値が文字列 `'ほげ'` か,`'ふが'` か,それ以外か」という条件判定を,以下のように書くのは無駄ですね. `elif` のところに来た時点で, `s == 'ほげ'` は `False` だったことが確定しているわけですから.\n",
"```\n",
"if s == `ほげ`:\n",
" ほげほげ\n",
"elif s != 'ほげ' and s == 'ふが':\n",
" ふが\n",
"else:\n",
"```\n",
"ちなみに, 上記の `and` は,前後の条件判定を「かつ」でつないでます.C言語の `&&` と同じ."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "fBa6JqxdRu4b"
},
"source": [
"## P2\n",
"次の仕様にあった関数の定義を次のセルに書いてください.\n",
"- 関数名は `mysum`\n",
"- 引数は一つだけ受け取る.名前は `x` とする(本来は好きな名前でいいのですが,練習のため指定します)\n",
"- 引数として受け取ったものが数値の格納されたリストと仮定して,その要素の和を求め,その値を返す\n",
"- 引数として受け取ったものがリストではないときや,数値でないものが入っていた場合の処理は考えなくてよい\n",
"- 組み込み関数 `sum` は使ってはいけない"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Z3Pk8TZcC2YS"
},
"outputs": [],
"source": [
"# このセルに上記の関数の定義を書く\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "t5wwjwU3fE7Q"
},
"source": [
"上記の関数の動作確認のため,以下を実行しよう.ただし, `sum` 呼んでるところは適切に修正しないといけません."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L1 = [1, 2, 3.14, 4, 5, 6, 7, 8, 9, 10]\n",
"L2 = [ -5, -11, 247, 16, -58, -49, 376, -5, -67, -88, 129,\n",
" -76, 34, -90, 26, -97, -98, 34, 64, 69, 76, 42,\n",
" 92, 86, -68, -31, -52, -6, 82, 99, 57, -38, -5,\n",
" -30, 68, 29, 28, 54, -68, 74, -46, -34, 74, 40,\n",
" -41, 21, 19, -68, -47, -62, -60, -42, 27, 78, -36,\n",
" 2, 18, 60, 60, 88, -5, -100, 16, -48, 63, 9,\n",
" -31, 53, -70, 70, 89, 1, 16, 90, -95, 28, 128,\n",
" 4649, 10, 66, -4, -79, 92, -88, 36, -59, 7, -30,\n",
" -30, -39, -22, -52, -5, -93, 28, -8, 94, 66, 288,\n",
" 31]\n",
"\n",
"# このセルのここより上はいじってはいけない\n",
"\n",
"print(sum(L1))\n",
"print(sum(L1[8:]))\n",
"print(sum(L2))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "NBxEMSeJF5Y4"
},
"source": [
"## P3\n",
"\n",
"次のセルの関数 myargmax は,「引数で渡されたリストの中から最大値を見つけてその**要素番号**を返す」つもりで定義したものですが,間違いがあります.\n",
"意図どおりに動作するように修正してください.ヒント: 2箇所書き換えるだけでいけるはず."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "9BSAIvLIE9pn"
},
"outputs": [],
"source": [
"def myargmax(X):\n",
" \n",
" imax = X[0]\n",
" for i in range(1, 10):\n",
" if X[imax] < X[i]:\n",
" imax = i\n",
" \n",
" return imax"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"以下のセルを実行して動作確認しましょう."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# このセルの中身はいじってはいけない\n",
"\n",
"def hoge(X):\n",
" i = myargmax(X)\n",
" print('最大値は{0}ほげ({1}番目)'.format(X[i], i))\n",
" \n",
"L3 = [-10, -9, -8, -7, -6, -5]\n",
"hoge(L1)\n",
"hoge(L2)\n",
"hoge(L3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## P4\n",
"\n",
"以下のセルを修正して,キーボードからの入力に応じて次のように分岐する処理のコードを書きなさい.\n",
"- `1` なら `L3` の和を表示\n",
"- `2` なら `L3` の最大値の要素番号を表示\n",
"- それ以外なら `L3` の最大値を表示"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# このセルを修正してね\n",
"x = int(input())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "ex02kadaiA.ipynb",
"provenance": []
},
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment