-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCS231n.py
More file actions
227 lines (148 loc) · 3.86 KB
/
Copy pathCS231n.py
File metadata and controls
227 lines (148 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# coding: utf-8
# In[1]:
def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)
print(quicksort([3,6,8,10,1,2,1]))
# In[2]:
x = 3
print(type(x)) # Prints "<class 'int'>"
print(x) # Prints "3"
print(x + 1) # Addition; prints "4"
print(x - 1) # Subtraction; prints "2"
print(x * 2) # Multiplication; prints "6"
print(x ** 2) # Exponentiation; prints "9"
x += 1
print(x) # Prints "4"
x *= 2
print(x) # Prints "8"
y = 2.5
print(type(y)) # Prints "<class 'float'>"
print(y, y + 1, y * 2, y ** 2) # Prints "2.5 3.5 5.0 6.25"
# In[3]:
t = True
f = False
print (type(t))
print (t and f)
print (t or f)
print (not t)
print (t!= f)
# In[4]:
hello = 'hello'
world = 'world'
print (hello)
print (world)
print (len(hello))
print (hello + ' ' + world)
hw = '%s %s %d' %(hello, world, 12)
print hw
# In[9]:
s = "hello"
print (s.capitalize())
print (s.upper())
print (s.rjust(7))
print (s.ljust(7))
print (s.center(7))
print (s.replace('l', 'o'))
print (' world '.strip())
# In[12]:
xs = [3, 1, 2]
print (xs, xs[2])
print (xs[-1])
xs[2] = 'foo'
print (xs)
xs.append('bar')
print (xs)
# In[14]:
nums = list(range(5))
print (nums)
print (nums[2:4])
print (nums[2:])
print (nums[:2])
print (nums[:])
print (nums[:-1])
nums[2:4] = [8,9]
print (nums)
# In[19]:
animals = ['cat', 'dog', 'monkey']
for animal in animals:
print (animal)
# In[20]:
nums = [0, 1,2,3,4]
square = []
for x in nums:
square.append(x**2)
print (square)
# In[21]:
nums = [0, 1, 2, 3, 4]
square = [x ** 2 for x in nums]
print (square)
# In[2]:
d = {'cat': 'cute', 'dog': 'furry'} # Create a new dictionary with some data
print(d['cat']) # Get an entry from a dictionary; prints "cute"
print('cat' in d) # Check if a dictionary has a given key; prints "True"
d['fish'] = 'wet' # Set an entry in a dictionary
print d
print(d['fish']) # Prints "wet"
# print(d['monkey']) # KeyError: 'monkey' not a key of d
print(d.get('monkey', 'N/A')) # Get an element with a default; prints "N/A"
print(d.get('fish', 'N/A')) # Get an element with a default; prints "wet"
del d['fish'] # Remove an element from a dictionary
print(d.get('fish', 'N/A')) # "fish" is no longer a key; prints "N/A"
# In[10]:
d = {'person': 2, 'cat': 4, 'spider':8}
for animal, leg in d.items():
print ('%s %d' %(animal, leg))
# In[9]:
nums = [0, 1, 2, 3, 4]
even_num_to_square = {x: x ** 2 for x in nums if x % 2 == 0}
print(even_num_to_square) # Prints "{0: 0, 2: 4, 4: 16}"
# In[7]:
animals = {'cat', 'dog'}
print('cat' in animals) # Check if an element is in a set; prints "True"
print('fish' in animals) # prints "False"
animals.add('fish') # Add an element to a set
print('fish' in animals) # Prints "True"
print(len(animals)) # Number of elements in a set; prints "3"
animals.add('cat') # Adding an element that is already in the set does nothing
print(len(animals)) # Prints "3"
animals.remove('cat') # Remove an element from a set
print(len(animals)) # Prints "2"
# In[11]:
animals = ['cat', 'dog', 'fish']
for idx, animal in enumerate(animals):
print ('#%d: %s' %(idx, animal))
# In[14]:
from math import sqrt
nums = {int(sqrt(x)) for x in range(30)}
print (nums)
# In[18]:
d = {(x, x+1): x for x in range(10)}
t = (5,6)
print (type(t))
print (d[t])
print (d[1,2])
print d
# In[19]:
def sign(x):
if x > 0:
return 'positive'
elif x < 0:
return 'negative'
else:
return 'zero'
for x in [-1, 0, 1]:
print(sign(x))
# In[21]:
def hello(name, loud = False):
if loud:
print ('HELLO, %s!' %name.upper())
else:
print ('Hello, %s' %name)
hello('Bob')
hello('Fred', loud = True)