generated from nighthawkcoders/student_2025
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emojihack.html
141 lines (118 loc) · 4.16 KB
/
emojihack.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Code Listing</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
line-height: 1.6;
}
pre {
background-color: #f4f4f4;
border: 1px solid #ddd;
padding: 10px;
border-radius: 5px;
overflow-x: auto;
}
code {
font-family: monospace;
white-space: pre-wrap;
}
</style>
</head>
<body>
<h1>Python Code Listing</h1>
<h2>1. Importing Libraries and Initial Code</h2>
<pre><code>from emoji import emojize
print(emojize(":thumbs_up: Python is awesome! :grinning_face:"))
Python is awesome! 👍😀
from newspaper import Article
from IPython.display import display, Markdown
urls = ["http://cnn.com/2023/03/29/entertainment/the-mandalorian-episode-5-recap/index.html",
"https://www.cnn.com/2023/06/09/entertainment/jurassic-park-anniversary/index.html"]
for url in urls:
article = Article(url)
article.download()
article.parse()
# Jupyter Notebook Display
# print(article.title)
display(Markdown(article.title)) # Jupyter display only
display(Markdown(article.text)) # Jupyter display only
print("\n")
import wikipedia
from IPython.display import display, Markdown # add for Jupyter
terms = ["Python (programming language)", "JavaScript"]
for term in terms:
# Search for a page
result = wikipedia.search(term)
# Get the summary of the first result
summary = wikipedia.summary(result[0])
print(term)
# print(summary) # console display
display(Markdown(summary)) # Jupyter display
</code></pre>
<h2>2. Inspecting Code</h2>
<pre><code>import inspect
from newspaper import Article
# inspect newspaper Article function
print(inspect.getsource(Article))
</code></pre>
<h2>3. Mean Calculation Function</h2>
<pre><code>import sys
from typing import Union
# Define types for mean function, trying to analyze input possibilities
Number = Union[int, float] # Number can be either int or float type
Numbers = list[Number] # Numbers is a list of Number types
Scores = Union[Number, Numbers] # Scores can be single or multiple
def mean(scores: Scores, method: int = 1) -> float:
"""
Calculate the mean of a list of scores.
Average and Average2 are hidden functions performing mean algorithm
If a single score is provided in scores, it is returned as the mean.
If a list of scores is provided, the average is calculated and returned.
"""
def average(scores):
"""Calculate the average of a list of scores using a Python for loop with rounding."""
sum = 0
len = 0
for score in scores:
if isinstance(score, Number):
sum += score
len += 1
else:
print("Bad data: " + str(score) + " in " + str(scores))
sys.exit()
return sum / len
def average2(scores):
"""Calculate the average of a list of scores using the built-in sum() function with rounding."""
return sum(scores) / len(scores)
# test to see if scores is a list of numbers
if isinstance(scores, list):
if method == 1:
# long method
result = average(scores)
else:
# built in method
result = average2(scores)
return round(result + 0.005, 2)
return scores # case where scores is a single valu
# try with one number
singleScore = 100
print("Print test data: " + str(singleScore)) # concat data for single line
print("Mean of single number: " + str(mean(singleScore)))
print()
# define a list of numbers
testScores = [90.5, 100, 85.4, 88]
print("Print test data: " + str(testScores))
print("Average score, loop method: " + str(mean(testScores)))
print("Average score, function method: " + str(mean(testScores, 2)))
print()
badData = [100, "NaN", 90]
print("Print test data: " + str(badData))
print("Mean with bad data: " + str(mean(badData)))
</code></pre>
</body>
</html>