Skip to content

Commit 8b22567

Browse files
committed
4주차 문제 등록
1 parent 2c2691e commit 8b22567

File tree

15 files changed

+759
-77
lines changed

15 files changed

+759
-77
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 34,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"\n",
10+
"import sys\n",
11+
"from typing import Tuple\n",
12+
"\n",
13+
"\n",
14+
"def input(): return sys.stdin.readline().rstrip()\n",
15+
"\n",
16+
"\n",
17+
"TOP = 1\n",
18+
"DOWN = 2\n",
19+
"RIGHT = 3\n",
20+
"LEFT = 4\n",
21+
"\n",
22+
"direction_reverse = {TOP: DOWN,\n",
23+
" DOWN: TOP,\n",
24+
" RIGHT: LEFT,\n",
25+
" LEFT: RIGHT}\n",
26+
"\n",
27+
"\n",
28+
"class Shark:\n",
29+
" def __init__(self, x, y, s, d, z) -> None:\n",
30+
" self.x = x\n",
31+
" self.y = y\n",
32+
" self.speed = s\n",
33+
" self.direction = d\n",
34+
" self.size = z\n",
35+
"\n",
36+
" def reverse_direction(self) -> None:\n",
37+
" self.direction = direction_reverse[self.direction]\n",
38+
"\n",
39+
" def __move(self, x, y) -> Tuple:\n",
40+
" self.x = x\n",
41+
" self.y = y\n",
42+
" return x, y\n",
43+
"\n",
44+
" def __check_boundry(self, width, hegiht) -> Tuple[bool, int, int]:\n",
45+
"\n",
46+
" if self.x > width or self.x < 0:\n",
47+
" self.reverse_direction()\n",
48+
" dis = abs(self.x-width)\n",
49+
"\n",
50+
" return (False, -dis if self.direction == LEFT else dis, 0)\n",
51+
"\n",
52+
" if self.y > hegiht or self.y < 0:\n",
53+
" self.reverse_direction()\n",
54+
" dis = abs(self.x-hegiht)-1\n",
55+
"\n",
56+
" return (False, 0, -dis if self.direction == TOP else dis)\n",
57+
"\n",
58+
" return (True, 0)\n",
59+
"\n",
60+
" def moveVec(self, width, hegiht) -> Tuple[int, int]:\n",
61+
" \"\"\"\n",
62+
" @return: 새로운 좌표 (x, y)\n",
63+
" \"\"\"\n",
64+
" speed = self.speed\n",
65+
" direction = self.direction\n",
66+
" cur_x, cur_y = self.x, self.y\n",
67+
" path = {\n",
68+
" TOP: (0, -speed),\n",
69+
" DOWN: (0, speed),\n",
70+
" LEFT: (-speed, 0),\n",
71+
" RIGHT: (speed, 0)\n",
72+
" }\n",
73+
" dx, dy = path[self.direction]\n",
74+
" print(dx, dy)\n",
75+
" self.__move(cur_x + dx, cur_y + dy)\n",
76+
" check = self.__check_boundry(width, hegiht)\n",
77+
" if check[0]:\n",
78+
" self.__move(self.x+check[1], self.y+check[2])\n",
79+
" \n",
80+
" return self.x, self.y\n",
81+
"\n",
82+
" def __str__(self) -> str:\n",
83+
" direction_text = {TOP: \"\", DOWN: \"아래\", RIGHT: \"우측\", LEFT: \"좌측\"}\n",
84+
" return f\"{self.x}|{self.y}|{self.speed}|{direction_text[self.direction]}\"\n",
85+
"\n",
86+
"\n",
87+
"class Grid:\n",
88+
" def __init__(self, width, height):\n",
89+
" self.__grid = [[None]*width for _ in range(height)]\n",
90+
" self.sharks: list[Shark] = []\n",
91+
" self.width = width\n",
92+
" self.height = height\n",
93+
"\n",
94+
" def add_shark(self, s: Shark) -> None:\n",
95+
" self.__grid[s.y][s.x] = s\n",
96+
" self.sharks.append(s)\n",
97+
"\n",
98+
" def move_sharks(self):\n",
99+
"\n",
100+
" for shark in self.sharks:\n",
101+
" before_x, before_y = shark.x, shark.y\n",
102+
" new_pos = shark.moveVec(self.width, self.height)\n",
103+
"\n",
104+
" self.__grid[before_y][before_x] = None\n",
105+
" self.__grid[new_pos[1]][new_pos[0]] = shark\n",
106+
"\n",
107+
" def print_grid(self):\n",
108+
" for y in range(self.height):\n",
109+
" print(f\"{', '.join(map(str,self.__grid[y]))}\")\n"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 35,
115+
"metadata": {},
116+
"outputs": [
117+
{
118+
"name": "stdout",
119+
"output_type": "stream",
120+
"text": [
121+
"None, None, 2|0|5|아래, None, None, None\n",
122+
"None, None, None, None, None, None\n",
123+
"None, None, None, None, None, None\n",
124+
"None, None, None, None, None, None\n",
125+
"0 5\n"
126+
]
127+
},
128+
{
129+
"ename": "IndexError",
130+
"evalue": "list index out of range",
131+
"output_type": "error",
132+
"traceback": [
133+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
134+
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
135+
"\u001b[1;32m<ipython-input-35-66e6b788104a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_shark\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mShark\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mDOWN\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprint_grid\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmove_sharks\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprint_grid\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
136+
"\u001b[1;32m<ipython-input-34-20d7229714c8>\u001b[0m in \u001b[0;36mmove_sharks\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 94\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 95\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__grid\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mbefore_y\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mbefore_x\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 96\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__grid\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mnew_pos\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mnew_pos\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mshark\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 97\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 98\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mprint_grid\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
137+
"\u001b[1;31mIndexError\u001b[0m: list index out of range"
138+
]
139+
}
140+
],
141+
"source": [
142+
"\n",
143+
"g = Grid(6, 4)\n",
144+
"\n",
145+
"g.add_shark(Shark(2, 0, 5, DOWN, 1))\n",
146+
"g.print_grid()\n",
147+
"g.move_sharks()\n",
148+
"print(\"\")\n",
149+
"g.print_grid() \n",
150+
"g.move_sharks()\n",
151+
"print(\"\")\n",
152+
"g.print_grid() \n",
153+
"\n",
154+
"# width, height, shark_count = map(int, input().split())\n",
155+
"\n",
156+
"# make grid"
157+
]
158+
}
159+
],
160+
"metadata": {
161+
"interpreter": {
162+
"hash": "4f221edd86a3c0a758cbf649fdbcce1e88b3b16e50fd875d2f96ea0bd0362236"
163+
},
164+
"kernelspec": {
165+
"display_name": "Python 3.8.8 64-bit ('base': conda)",
166+
"language": "python",
167+
"name": "python3"
168+
},
169+
"language_info": {
170+
"codemirror_mode": {
171+
"name": "ipython",
172+
"version": 3
173+
},
174+
"file_extension": ".py",
175+
"mimetype": "text/x-python",
176+
"name": "python",
177+
"nbconvert_exporter": "python",
178+
"pygments_lexer": "ipython3",
179+
"version": "3.8.8"
180+
},
181+
"orig_nbformat": 4
182+
},
183+
"nbformat": 4,
184+
"nbformat_minor": 2
185+
}

0 commit comments

Comments
 (0)