-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.py
More file actions
353 lines (245 loc) · 7.16 KB
/
final.py
File metadata and controls
353 lines (245 loc) · 7.16 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
def print_name(name):
"""prints the name input provided
Args:
name (string): the name to be printed
"""
print("my name is", name)
# print_name("nela")
def area_of_triangle(base, height):
"""calculates the area of a triangle
Args:
base (float): length of base
height (float): length of height
Returns:
float: the total area
"""
return base * height / 2
# print(area_of_triangle(4, 5))
import math
def hypotenuse(a, b):
"""finds the hypotenuse of a right triangle
Args:
a (float): the first leg
b (float): the second leg
Returns:
float: the hypotenuse
"""
return math.sqrt(a**2 + b**2)
# print(hypotenuse(3, 4))
def print_name_and_age(name, age):
"""prints both the name and age
Args:
name (string): the name to print
age (string): the age to print
"""
print("my name is", name, "and i am", age)
# print_name_and_age("nela", 123)
def area_of_circle(r):
"""calculates the area of a circle
Args:
r (float): radius of the circle
Returns:
float: the total area
"""
return math.pi * r**2
# print(area_of_circle(4))
from datetime import datetime
def time_since(date_day, date_month, date_year):
"""calculates the time from a date until now
Args:
date_day (int): the day of the date
date_month (int): the month of the date
date_year (int): the year of the date
Returns:
datetime: a datetime representing the time gap
"""
current_date = datetime.now()
provided_date = datetime(date_year, date_month, date_day)
time_difference = current_date - provided_date
return time_difference
# print(time_since(12, 5, 2002))
def factorial(n):
"""finds the factorial of a number
Args:
n (int): number to find factorial of
Returns:
int: the factorial
"""
if n == 1:
return 1
return n * factorial(n - 1)
# print(factorial(5))
def print_names(names):
"""prints each name in a list of names
Args:
names (list): a list of names
"""
for name in names:
print_name(name)
print_names(["nela", "stuti", "dikshita"])
import random
def approximate_pi(n):
"""a monte carlo approximation of pi
Args:
n (int): number of iterations
"""
count = 0
for i in range(n):
x = random.random()
y = random.random()
z = hypotenuse(x, y)
if z < 1:
count += 1
return count / n * 4
# print(approximate_pi(1000000))
def nato_translation(name):
"""finds a word's spelling in the NATO alphabet
Args:
name (string): any word
Returns:
string: the NATO translation
"""
dictionary = {
"A": "Alpha",
"B": "Bravo",
"C": "Charlie",
"D": "Delta",
"E": "Echo",
"F": "Foxtrot",
"G": "Golf",
"I": "India",
"J": "Juliet",
"K": "Kilo",
"L": "Lima",
"M": "Mike",
"N": "November",
"O": "Oscar",
"P": "Papa",
"Q": "Quebec",
"R": "Romeo",
"S": "Sierra",
"T": "Tango",
"U": "Uniform",
"V": "Victor",
"W": "Whiskey",
"X": "Xray",
"Y": "Yankee",
"Z": "Zulu",
}
output = ""
for char in name:
output += dictionary[char.upper()]
# same as output = output + dictionary[char]
return output
# print(nato_translation("Nela"))
def reverse(name):
"""reverses a string
Args:
name (string): string to be reversed
Returns:
string: the reverse of the input string
"""
output = ""
for char in name:
output = char + output
return output
# print(reverse("women who code"))
# make sure you do pip install requests and pip install beautifulsoup4 in terminal
import requests
from bs4 import BeautifulSoup
def national_debt():
"""finds the current national debt
Returns:
string: the national debt, in dollar format
"""
url = "https://www.pgpf.org/national-debt-clock"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
span_element = soup.find("span", class_="ticker")
ticker_text = span_element.text
return ticker_text
# print(national_debt())
def per_person_debt():
"""finds the current national debt per person
Returns:
string: the national debt per person
"""
url = "https://www.pgpf.org/national-debt-clock"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
span_element = soup.find("div", class_="per-person-debt-num")
ticker_text = span_element.text
return ticker_text
# print(per_person_debt())
def dollars_to_int(dollars):
"""converts a string-formatted dollar amount to int
Args:
dollars (string): a string containing a dollar amount, possibly with $ and ,
Returns:
int: the dollar amount in integer format
"""
output = ""
for char in dollars:
if not char == "$" and not char == ",":
output += char
return int(output)
# print(dollars_to_int(per_person_debt()))
# print(dollars_to_int(national_debt()) / dollars_to_int(per_person_debt()))
def word_counts():
"""finds the counts of each word in a file
Returns:
dict: a dictionary containing word:count
"""
counts = {}
with open("iu.txt", "r", encoding="utf-8") as file:
for line in file:
for word in line.split():
if word in counts:
counts[word] += 1
else:
counts[word] = 1
return counts
def bigram_counts():
"""finds the counts of each bigram in a file
Returns:
dict: a dictionary containing bigram:count
"""
counts = {}
with open("iu.txt", "r", encoding="utf-8") as file:
for line in file:
words = line.split()
for i in range(len(words) - 1):
bigram = words[i] + " " + words[i + 1]
if bigram in counts:
counts[bigram] += 1
else:
counts[bigram] = 1
return counts
def trigram_counts():
"""finds the counts of each trigram in a file
Returns:
dict: a dictionary containing trigram:count
"""
counts = {}
with open("iu.txt", "r", encoding="utf8") as file:
for line in file:
words = line.split()
for i in range(len(words) - 2):
trigram = words[i] + " " + words[i + 1] + " " + words[i + 2]
if trigram in counts:
counts[trigram] += 1
else:
counts[trigram] = 1
return counts
import csv
def write_word_counts(counts):
"""takes a dictionary of counts (outputted from before) and writes to a csv
Args:
counts (dict): a dictionary of containing string:count
"""
with open("counts.csv", "w", encoding="utf-8") as file:
writer = csv.writer(file)
for key, value in counts.items():
writer.writerow([key, value])
# words = trigram_counts()
# write_word_counts(words)