Skip to content

Commit 4ee4266

Browse files
authored
Late night commit
1 parent c791fd2 commit 4ee4266

File tree

8 files changed

+1877
-0
lines changed

8 files changed

+1877
-0
lines changed

Clothings.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
class Clothing:
3+
stock={ 'name': [],'material' :[], 'amount':[]}
4+
def __init__(self,name):
5+
material = ""
6+
self.name = name
7+
def add_item(self, name, material, amount):
8+
'''Add items to stock of clothing'''
9+
Clothing.stock['name'].append(self.name)
10+
Clothing.stock['material'].append(self.material)
11+
Clothing.stock['amount'].append(amount)
12+
def Stock_by_Material(self, material):
13+
'''iterate over the amount of each item of a given material that is in stock'''
14+
count=0
15+
n=0
16+
for item in Clothing.stock['material']:
17+
if item == material:
18+
count += Clothing.stock['amount'][n]
19+
n+=1
20+
return count
21+
22+
23+
class shirt(Clothing):
24+
material="Cotton"
25+
class pants(Clothing):
26+
material="Cotton"
27+
28+
polo = shirt("Polo")
29+
sweatpants = pants("Sweatpants")
30+
polo.add_item(polo.name, polo.material, 4)
31+
sweatpants.add_item(sweatpants.name, sweatpants.material, 6)
32+
current_stock = polo.Stock_by_Material("Cotton")
33+
print(current_stock)
34+

Elevator.ipynb

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"source": [
6+
"# Practice Notebook: Methods and Classes"
7+
],
8+
"metadata": {}
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"source": [
13+
"The code below defines an *Elevator* class. The elevator has a current floor, it also has a top and a bottom floor that are the minimum and maximum floors it can go to. Fill in the blanks to make the elevator go through the floors requested."
14+
],
15+
"metadata": {}
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"source": [
21+
"class Elevator:\r\n",
22+
" def __init__(self, bottom, top, current):\r\n",
23+
" \"\"\"Initializes the Elevator instance.\"\"\"\r\n",
24+
" self.bottom = bottom\r\n",
25+
" self.top = top\r\n",
26+
" self.current = current\r\n",
27+
" \r\n",
28+
" def up(self):\r\n",
29+
" \"\"\"Makes the elevator go up one floor.\"\"\"\r\n",
30+
" if self.current < self.top:\r\n",
31+
" self.current +=1\r\n",
32+
" \r\n",
33+
" else:\r\n",
34+
" self.current = self.top\r\n",
35+
" pass\r\n",
36+
" \r\n",
37+
" def down(self):\r\n",
38+
" \"\"\"Makes the elevator go down one floor.\"\"\"\r\n",
39+
" if self.current > self.bottom:\r\n",
40+
" self.current -= 1\r\n",
41+
" \r\n",
42+
" else:\r\n",
43+
" self.current = self.bottom \r\n",
44+
" pass\r\n",
45+
" \r\n",
46+
" def go_to(self, floor):\r\n",
47+
" \"\"\"Makes the elevator go to the specific floor.\"\"\"\r\n",
48+
" self.current = floor\r\n",
49+
" \r\n",
50+
" def __str__(self):\r\n",
51+
" return \"Current floor {}\".format(self.current)\r\n",
52+
"\r\n",
53+
"elevator = Elevator(-1, 10, 0)"
54+
],
55+
"outputs": [],
56+
"metadata": {}
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"source": [
61+
"This class is pretty empty and doesn't do much. To test whether your *Elevator* class is working correctly, run the code blocks below."
62+
],
63+
"metadata": {}
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 2,
68+
"source": [
69+
"elevator.up()\r\n",
70+
"print(elevator.current) #should output 1\r\n",
71+
"elevator.__str__()"
72+
],
73+
"outputs": [
74+
{
75+
"output_type": "stream",
76+
"name": "stdout",
77+
"text": [
78+
"1\n"
79+
]
80+
},
81+
{
82+
"output_type": "execute_result",
83+
"data": {
84+
"text/plain": [
85+
"'Current floor 1'"
86+
]
87+
},
88+
"metadata": {},
89+
"execution_count": 2
90+
}
91+
],
92+
"metadata": {}
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": 3,
97+
"source": [
98+
"elevator.down() \r\n",
99+
"print(elevator.current) #should output 0\r\n",
100+
"elevator.__str__()"
101+
],
102+
"outputs": [
103+
{
104+
"output_type": "stream",
105+
"name": "stdout",
106+
"text": [
107+
"0\n"
108+
]
109+
},
110+
{
111+
"output_type": "execute_result",
112+
"data": {
113+
"text/plain": [
114+
"'Current floor 0'"
115+
]
116+
},
117+
"metadata": {},
118+
"execution_count": 3
119+
}
120+
],
121+
"metadata": {}
122+
},
123+
{
124+
"cell_type": "code",
125+
"execution_count": 3,
126+
"source": [
127+
"elevator.go_to(10) \r\n",
128+
"print(elevator.current) #should output 10\r\n",
129+
"elevator.__str__()"
130+
],
131+
"outputs": [
132+
{
133+
"output_type": "stream",
134+
"name": "stdout",
135+
"text": [
136+
"10\n"
137+
]
138+
},
139+
{
140+
"output_type": "execute_result",
141+
"data": {
142+
"text/plain": [
143+
"'Current floor 10'"
144+
]
145+
},
146+
"metadata": {},
147+
"execution_count": 3
148+
}
149+
],
150+
"metadata": {}
151+
},
152+
{
153+
"cell_type": "markdown",
154+
"source": [
155+
"If you get a **<font color =red>NameError</font>** message, be sure to run the *Elevator* class definition code block first. If you get an **<font color =red>AttributeError</font>** message, be sure to initialize *self.current* in your *Elevator* class."
156+
],
157+
"metadata": {}
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"source": [
162+
"Once you've made the above methods output 1, 0 and 10, you've successfully coded the *Elevator* class and its methods. Great work!\n",
163+
"<br><br>\n",
164+
"For the up and down methods, did you take into account the top and bottom floors? Keep in mind that the elevator shouldn't go above the top floor or below the bottom floor. To check that out, try the code below and verify if it's working as expected. If it's not, then go back and modify the methods so that this code behaves correctly."
165+
],
166+
"metadata": {}
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": 4,
171+
"source": [
172+
"# Go to the top floor. Try to go up, it should stay. Then go down.\r\n",
173+
"elevator.go_to(10)\r\n",
174+
"elevator.up()\r\n",
175+
"elevator.down()\r\n",
176+
"print(elevator.current) # should be 9\r\n",
177+
"# Go to the bottom floor. Try to go down, it should stay. Then go up.\r\n",
178+
"elevator.go_to(-1)\r\n",
179+
"elevator.down()\r\n",
180+
"elevator.down()\r\n",
181+
"elevator.up()\r\n",
182+
"elevator.up()\r\n",
183+
"print(elevator.current) # should be 1"
184+
],
185+
"outputs": [
186+
{
187+
"output_type": "stream",
188+
"name": "stdout",
189+
"text": [
190+
"9\n",
191+
"1\n"
192+
]
193+
}
194+
],
195+
"metadata": {}
196+
},
197+
{
198+
"cell_type": "markdown",
199+
"source": [
200+
"Now add the __str__ method to your *Elevator* class definition above so that when printing the elevator using the **print( )** method, we get the current floor together with a message. For example, in the 5th floor it should say \"Current floor: 5\""
201+
],
202+
"metadata": {}
203+
},
204+
{
205+
"cell_type": "code",
206+
"execution_count": 5,
207+
"source": [
208+
"elevator.go_to(5)\r\n",
209+
"print(elevator)"
210+
],
211+
"outputs": [
212+
{
213+
"output_type": "stream",
214+
"name": "stdout",
215+
"text": [
216+
"Current floor 5\n"
217+
]
218+
}
219+
],
220+
"metadata": {}
221+
},
222+
{
223+
"cell_type": "markdown",
224+
"source": [
225+
"Remember, Python uses the default method, that prints the position where the object is stored in the computer’s memory. If your output is something like: <br>\n",
226+
"> <__main__.Elevator object at 0x7ff6a9ff3fd0>\n",
227+
"\n",
228+
"Then you will need to add the special __str__ method, which returns the string that you want to print. Try again until you get the desired output, \"Current floor: 5\"."
229+
],
230+
"metadata": {}
231+
},
232+
{
233+
"cell_type": "markdown",
234+
"source": [
235+
"Once you have successfully produced the desired output, you are all done with this practice notebook. Awesome!"
236+
],
237+
"metadata": {}
238+
}
239+
],
240+
"metadata": {
241+
"kernelspec": {
242+
"name": "python3",
243+
"display_name": "Python 3.9.4 64-bit"
244+
},
245+
"language_info": {
246+
"codemirror_mode": {
247+
"name": "ipython",
248+
"version": 3
249+
},
250+
"file_extension": ".py",
251+
"mimetype": "text/x-python",
252+
"name": "python",
253+
"nbconvert_exporter": "python",
254+
"pygments_lexer": "ipython3",
255+
"version": "3.9.4"
256+
},
257+
"widgets": {
258+
"application/vnd.jupyter.widget-state+json": {
259+
"state": {},
260+
"version_major": 2,
261+
"version_minor": 0
262+
}
263+
},
264+
"interpreter": {
265+
"hash": "247ab06e135bb35fa78c5eff31b2a9a0050dcb5fb773c2631d2a29ac689eeccb"
266+
}
267+
},
268+
"nbformat": 4,
269+
"nbformat_minor": 2
270+
}

Repository.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Repository:
2+
... def __init__(self):
3+
... self.packages = {}
4+
... def add_package(self, package):
5+
... self.packages[package.name] = package
6+
... def total_size(self):
7+
... result = 0
8+
... for package in self.packages.values():
9+
... result += package.size
10+
... return result

0 commit comments

Comments
 (0)