{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 14wk-1: 퀴즈9\n", "\n", "최규빈 \n", "2024-06-05\n", "\n", "\n", "\n", "> **Caution**\n", ">\n", "> - 전북대 학생들을 시험당일 학생증을 지참할 것. (출석체크 및\n", "> 본인확인) 학생증 외에 신분증 여권등도 가능.\n", "> - 부정행위 (카카오톡 채팅을 통한 코드공유, 생성형모델 사용, 대리시험\n", "> 등) 적발시 F 처리함.\n", "> - 퀴즈 중 지각할 경우 지각사실을 기록함. 하지만 별 다른 감점은 하지\n", "> 않음.\n", "> - `.ipynb` 파일 형태로 제출된 답안지만 채점하며 그 외의 형식\n", "> (`.hwp`, `.py` 등)은 채점하지 않음. 즉 0점 처리함." ], "id": "bc994972-4241-4165-b790-8806fd055569" }, { "cell_type": "code", "execution_count": 230, "metadata": { "tags": [] }, "outputs": [], "source": [ "import numpy as np" ], "id": "e9cf844f-35fd-415c-b75a-8cde7d8fff15" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. – 30점\n", "\n", "아래와 같은 기능을 하도록 클래스 `Coin`을 설계하라.\n", "\n", "`# 예시1`" ], "id": "0a45cb7c-c569-46c8-8ded-30fe9cc3e7a9" }, { "cell_type": "code", "execution_count": 110, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins = Coin()" ], "id": "73f09d96-cced-4b6c-9983-3ee3aba1647d" }, { "cell_type": "code", "execution_count": 111, "metadata": { "tags": [] }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "동전을 던져서 앞면이 나왔습니다." ] } ], "source": [ "ins.toss()" ], "id": "5be400fc-d196-4a15-956c-81d93c277a05" }, { "cell_type": "code", "execution_count": 112, "metadata": { "tags": [] }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "동전을 던져서 뒷면이 나왔습니다." ] } ], "source": [ "ins.toss()" ], "id": "d968129f-bd6d-4425-88b1-9e030f6865fd" }, { "cell_type": "code", "execution_count": 113, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins" ], "id": "4dd9b5ef-2632-4c04-84f1-902cb8a102c7" }, { "cell_type": "code", "execution_count": 114, "metadata": { "tags": [] }, "outputs": [], "source": [ "len(ins)" ], "id": "5e09632c-e514-45fe-aaa1-d78a6c25b2e3" }, { "cell_type": "code", "execution_count": 115, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins[0]" ], "id": "02c5155e-c466-46a7-a1ef-9696d8bae48c" }, { "cell_type": "code", "execution_count": 116, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins[1]" ], "id": "5c78b793-da09-4efe-bf3e-b062ef3bb3a7" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "`# 예시2`" ], "id": "d7d79cf3-047c-4525-97a4-bf4a9f4b0d48" }, { "cell_type": "code", "execution_count": 117, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins = Coin()" ], "id": "68cbaf61-7551-454a-8e0d-7ea020129c57" }, { "cell_type": "code", "execution_count": 118, "metadata": { "tags": [] }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "동전을 던져서 앞면이 나왔습니다.\n", "동전을 던져서 앞면이 나왔습니다.\n", "동전을 던져서 뒷면이 나왔습니다.\n", "동전을 던져서 뒷면이 나왔습니다.\n", "동전을 던져서 앞면이 나왔습니다.\n", "동전을 던져서 뒷면이 나왔습니다.\n", "동전을 던져서 앞면이 나왔습니다.\n", "동전을 던져서 뒷면이 나왔습니다.\n", "동전을 던져서 앞면이 나왔습니다.\n", "동전을 던져서 뒷면이 나왔습니다." ] } ], "source": [ "for i in range(10):\n", " ins.toss()" ], "id": "4aec821b-15c9-4057-b51f-f2ddf7a6a99a" }, { "cell_type": "code", "execution_count": 119, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins" ], "id": "e1dc581f-095a-4ca2-8390-839210442d17" }, { "cell_type": "code", "execution_count": 120, "metadata": { "tags": [] }, "outputs": [], "source": [ "len(ins)" ], "id": "15528a1a-9ad6-4d92-9eee-a1c21589e6d1" }, { "cell_type": "code", "execution_count": 121, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins[-1]" ], "id": "dfedb004-dac1-48cb-b4a2-a8ebb2dc2b1d" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "(풀이)" ], "id": "d55a6e0c-6e47-4cee-8102-1a046161bbc3" }, { "cell_type": "code", "execution_count": 109, "metadata": { "tags": [] }, "outputs": [], "source": [ "class Coin:\n", " def __init__(self):\n", " self.results = []\n", " def toss(self):\n", " result = np.random.choice(['앞면','뒷면'])\n", " self.results.append(result)\n", " print(f\"동전을 던져서 {result}이 나왔습니다.\")\n", " def __repr__(self):\n", " return f\"결과: {self.results}\"\n", " def __getitem__(self,idx): \n", " return self.results[idx]\n", " def __len__(self):\n", " return len(self.results)" ], "id": "40eadb35-f70f-4f53-a5ec-8d44b31ff4a2" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. – 30점\n", "\n", "아래와 같은 기능을 하도록 클래스 `Container`을 설계하라.\n", "\n", "`# 예시`" ], "id": "a9db325c-0de3-47f3-a1be-1f4a61d67c2b" }, { "cell_type": "code", "execution_count": 147, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins = Container()\n", "ins" ], "id": "4dfea133-1891-456d-9107-7dbdf35b6d49" }, { "cell_type": "code", "execution_count": 148, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins + 1 + 1 + 2 " ], "id": "291b07b9-5d44-4b6d-98a6-d71074c8c767" }, { "cell_type": "code", "execution_count": 149, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins + 2 + 2 + 2 " ], "id": "d3ba141e-600f-4f2c-afa7-58f9bd8fc397" }, { "cell_type": "code", "execution_count": 150, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins + 2 + 2 + 2 + 4 " ], "id": "54cb90db-2d2e-461b-a5d9-54a959022db2" }, { "cell_type": "code", "execution_count": 151, "metadata": { "tags": [] }, "outputs": [], "source": [ "ins.freq()" ], "id": "94aeccbf-4b10-4de7-b75c-0c7d1a7efda6" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "(풀이)" ], "id": "95bfe2d6-2939-4ad5-89f1-818e8b7ac5f4" }, { "cell_type": "code", "execution_count": 133, "metadata": { "tags": [] }, "outputs": [], "source": [ "class Container:\n", " def __init__(self):\n", " self.items = []\n", " def __add__(self,v):\n", " self.items.append(v)\n", " return self\n", " def __repr__(self):\n", " return f\"아이템: {self.items}\"\n", " def freq(self):\n", " return {s:self.items.count(s) for s in set(self.items)}" ], "id": "f7245d0a-1512-4094-ab3a-66b98db60134" }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. – 40점\n", "\n", "아래와 같은 기능을 하도록 `Quiz` 클래스를 만들어라.\n", "\n", "`# 예시`" ], "id": "a03ace0a-db51-4bc6-9378-087f619c3276" }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [], "source": [ "guebin = Quiz()\n", "daho = Quiz()" ], "id": "70d5ebb7-9ce4-4ba0-bca4-5e0bdf08eaa4" }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [], "source": [ "guebin.save_score(10)\n", "guebin" ], "id": "31c19307-9867-45c6-9bfb-1cddd07b224c" }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [] }, "outputs": [], "source": [ "daho.save_score(8)\n", "daho" ], "id": "1f6d51c4-cea5-40fe-836e-3688fb70f4df" }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [] }, "outputs": [], "source": [ "guebin == daho, guebin > daho # 퀴즈의 총합비교" ], "id": "6170ab7e-55df-4cc1-812b-ab5ab16a0e28" }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [] }, "outputs": [], "source": [ "guebin.save_score(8)\n", "guebin" ], "id": "c3a082b7-c897-41ca-bf3b-60a0555933a6" }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [] }, "outputs": [], "source": [ "daho.save_score(10)\n", "daho" ], "id": "17b664b7-a216-4204-b0e6-8e2c036b8400" }, { "cell_type": "code", "execution_count": 8, "metadata": { "tags": [] }, "outputs": [], "source": [ "guebin == daho, guebin > daho # 퀴즈의 총합비교" ], "id": "fd35bf16-5735-4642-953a-3025aa27fd00" }, { "cell_type": "code", "execution_count": 9, "metadata": { "tags": [] }, "outputs": [], "source": [ "guebin[-1] == daho[-1], guebin[-1] > daho[-1] # 최근 퀴즈점수 비교 " ], "id": "b75ab13b-aaa1-45cc-bc2b-f9c6ad43a435" }, { "cell_type": "markdown", "metadata": {}, "source": [ "`#`\n", "\n", "(풀이)" ], "id": "8d0cfbda-f46b-4511-b2bc-dbfef4ef50ca" }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [], "source": [ "class Quiz:\n", " def __init__(self):\n", " self.scores = [] \n", " self.n = 0 \n", " self.text = \"\"\n", " self.score = None\n", " def save_score(self,score):\n", " self.score = score\n", " self.n = self.n +1 \n", " self.scores.append(self.score)\n", " def __getitem__(self,idx):\n", " return self.scores[-idx]\n", " def __repr__(self):\n", " self.text = self.text + f\"퀴즈{self.n}: {self.score}\\n\"\n", " return self.text\n", " def __eq__(self,other):\n", " return sum(self.scores) == sum(other.scores)\n", " def __gt__(self,other):\n", " return sum(self.scores) > sum(other.scores)" ], "id": "20f41080-575c-45f0-896d-da63c7309357" } ], "nbformat": 4, "nbformat_minor": 5, "metadata": { "kernelspec": { "name": "python3", "display_name": "Python 3 (ipykernel)", "language": "python" }, "language_info": { "name": "python", "codemirror_mode": { "name": "ipython", "version": "3" }, "file_extension": ".py", "mimetype": "text/x-python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.8" } } }