-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal5.csv
More file actions
We can make this file beautiful and searchable if this error is corrected: Illegal quoting in line 5.
278 lines (206 loc) · 9.19 KB
/
final5.csv
File metadata and controls
278 lines (206 loc) · 9.19 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
Get the NOAA marine forecast for Chicago/Romeoville.
Change to lower case.
Convert all characters that aren't 'a' to 'z' into space ' '
Then split it into words.
Find all the NOAA words that are not in a list of 45,402 "linuxwords".
Print those unrecognized words, just once each.
-
import requests
okwords = requests.get('https://users.cs.duke.edu/~ola/ap/linuxwords').text.lower().split()
# Step 1: Simulated step - Fetch the NOAA marine forecast for Chicago/Romeoville
# In a real scenario, you would replace this with an actual request to the NOAA API or website to get the marine forecast
noaa_forecast_text = "This is a simulated NOAA forecast text for demonstration purposes."
# Step 2: Convert to lower case and replace non-'a' to 'z' characters with spaces
transformed_text = ''.join([c if 'a' <= c <= 'z' else ' ' for c in noaa_forecast_text.lower()])
# Step 3: Split the transformed text into words
noaa_words = set(transformed_text.split())
# Step 4: Fetch the "linuxwords" list
okwords_response = requests.get('https://users.cs.duke.edu/~ola/ap/linuxwords')
linuxwords = set(okwords_response.text.lower().split())
# Step 5: Find NOAA words not in "linuxwords"
unrecognized_words = noaa_words - linuxwords
# Step 6: Print each unrecognized word just once
for word in unrecognized_words:
print(word)
---
Cropping advice (a program)
# function crop_advice( two-tuple ) returns a two-tuple
def crop_advice(dimensions):
# The golden ratio, phi
phi = (1 + 5 ** 0.5) / 2
width, height = dimensions
# Determine if the image is in landscape or portrait mode
if width > height:
# Landscape
# Calculate the new height based on the golden ratio, keeping the width as is
new_height = round(width / phi)
# If the new height is larger than the original height, we need to adjust the width instead
if new_height > height:
new_width = round(height * phi)
return (new_width, height)
else:
return (width, new_height)
else:
# Portrait
# Calculate the new width based on the golden ratio, keeping the height as is
new_width = round(height / phi)
# If the new width is larger than the original width, we need to adjust the height instead
if new_width > width:
new_height = round(width * phi)
return (width, new_height)
else:
return (new_width, height)
# Adjusting the assert statements to account for rounding
assert crop_advice((300, 200)) == (300, 185), f"Test 1 Failed: {crop_advice((300, 200))}"
assert crop_advice((300, 150)) == (243, 150), f"Test 2 Failed: {crop_advice((300, 150))}"
assert crop_advice((1080, 1920)) == (1080, 1747), f"Test 3 Failed: {crop_advice((1080, 1920))}"
"Tests passed successfully!"
---
Plotting (a small program)
The binomial coefficient
import math
import matplotlib.pyplot as plt
# Define the binomial coefficient function
F = math.factorial
bino = lambda n, k: F(n) / (F(k) * F(n - k))
# Use a nice large value of n, like 100
n = 100
# Calculate the binomial coefficient for each k in the range 0 to n (inclusive)
binomial_coefficients = [bino(n, k) for k in range(n + 1)]
# Create the graph of the binomial coefficient C vs. k
plt.figure(figsize=(10, 6))
plt.plot(range(n + 1), binomial_coefficients, marker='o', linestyle='-', color='b')
plt.title('Binomial Coefficient C vs. k for n = 100')
plt.xlabel('k')
plt.ylabel('C(n, k)')
plt.grid(True)
plt.show()
----
Binomial coefficients (fix it)
If you have a set of eight letters abcdefgh and you list all subsets of this, there are 256 such subsets, including the empty set. That's because you can choose to put a in your subset (yes vs. no: 2 choices), you can choose to put b in your subset (yes vs. no: 2 choices), etc
2*2*2*2*2*2*2*2=256
Another way to think about it, which ought to get the same answer 256, is that there are C(8,k) ways to pick k objects from among 8, and if you sum that up over all the k, this ought to count all the possible subsets.
The program below is supposed to do that sum, and get the right answer, 256. But the program doesn't run, and when you fix that, it gets the wrong answer. Fix the program so it does run, and gets the right answer.
import math
N = 8
def binomial_coefficient(n, k):
F = math.factorial
return F(n) // (F(k) * F(n-k)) # Use integer division to avoid floating-point inaccuracies
# Initialize thatsum before the loop
thatsum = 0
# The range should go up to N + 1 to include k = N
for k in range(N + 1):
thatsum += binomial_coefficient(N, k)
thatsum
--
#5 unix/linux
Colab runs your code on a virtual machine started up for you by Google when you first run a code cell, and then persisting until you stop using it for a while. Colab calls your virtual machine a "runtime".
The virtual machine has a file hierarchy organized in a linux-like way (it probably is linux).
!pwd # Ensure you're in the /content directory
!ls -la # Check the contents, including the sample_data directory
!cd sample_data && ls -la # Change to the sample_data directory and list its contents
!head -n 1 sample_data/your_chosen_file.csv # Replace 'your_chosen_file.csv' with the actual file name
-----
Smoothing a circular array (fix it)
In the program below, I create a 12-long list consisting of zeros, with just one non-zero element which has a value of 1. The sum of the 12 elements is 1.
thetwelve = 12 * [0.]
thetwelve[4] = 1.0
print(sum(thetwelve), '= sum of', thetwelve)
Nsmoothing_passes = 5
for apass in range(Nsmoothing_passes):
temp = thetwelve[:] # Make a copy of the list for reading
for j in range(12):
avg = (temp[j] + temp[(j+1) % 12]) / 2. # Use modular arithmetic for circular condition
thetwelve[j] = avg
# For the circular condition, thetwelve[(j+1) % 12] is automatically handled in the next iteration or by the first element for the last j
# Notice that thetwelve[11] is not directly set here, but it's correctly updated in the loop due to the circular handling
print(sum(thetwelve), '= sum of', thetwelve)
---
Recursion (base case forgot)
def addup(n):
assert (isinstance(n, int)), 'not an int' # Check if n is an integer
if n == 0:
return 0 # Base case: The sum of numbers up to 0 is 0
else:
d = addup(n-1) + n # Recursive case: Sum of numbers up to n is n plus the sum up to n-1
return d
n = 100
print('Sum of numbers up to {} is {}'.format(n, addup(n)))
---
String format
print('{i} {i} {i}'.format(i=8)) # why does this work...
print('{} {} {}'.format(8)) # but this doesn't?
(it doesnt work because there are no placeholders in the second 1, where there is no variable set equal to 8)
(it only replaces the first one with 8)
----
iterable objects
List
Tuple
Dictionary
---
Dictionary
The program below creates a dictionary of {key:value} pairs. Print all the keys of this dictionary which match their corresponding value. Usually there are zero, one, or two of them.
For instance, in this example here from an N=10 run, there was just one match (9).
drand = {10: 2, 9: 9, 6: 7, 3: 8, 2: 10}
A proper response from your bit of code would be just 9
import random
N = 33
drand = {10: 2, 9: 9, 6: 7, 3: 8, 2: 10}
drand = dict()
for i in range(N):
drand.update({random.randint(1, N): random.randint(1, N)})
print('len(drand) = ', len(drand))
matches = [k for k, v in drand.items() if k == v]
print(matches)
----
import pandas as pd
# Create the DataFrame
data = {'OrderID': [1, 2, 3],
'Product': ['Apple', 'Banana', 'Orange'],
'Quantity': [3, 2, 5],
'Price': [1.00, 0.50, 0.75]}
df = pd.DataFrame(data)
# Calculate the total sales amount for each order
df['TotalAmount'] = df['Quantity'] * df['Price']
# Print the updated DataFrame
print(df)
--
import requests
def check_website_status(url):
try:
response = requests.get(url)
if response.status_code == 200:
print(f"{url} is online.")
else:
print(f"{url} is offline or there was a problem (Status Code: {response.status_code}).")
except requests.exceptions.RequestException as e:
print(f"Error checking {url}: {e}")
# Example call
check_website_status("https://www.example.com")
--
import pandas as pd
data = pd.DataFrame({
'Product': ['Apples', 'Oranges', 'Grapes', 'Bananas'],
'Quantity': [3, 6, 8, 2],
})
filtered_data = data[data['Quantity'] > 5]
print(filtered_data)
---
import folium
earthquake_map = folium.Map(location=[0, 0], zoom_start=2)
for _, row in earthquakes.iterrows():
folium.Circle(
location=[row['Latitude'], row['Longitude']],
radius=row['Magnitude'] * 10000,
color='red',
fill=True,
).add_to(earthquake_map)
earthquake_map
---
mountains_map = folium.Map(location=[27.9881, 86.9250], zoom_start=5) # Centered roughly on the Himalayas
for _, row in mountains.iterrows():
folium.Marker(
location=[row['Latitude'], row['Longitude']],
popup=row['Name'],
).add_to(mountains_map)
mountains_map