x
1from pathlib import Path2
3path = Path('Harry_Potter.txt')4contents = path.read_text()5
6words = contents.split()7num_words = len(words)8x=f"The file {path} has about {num_words} words."9
10path = Path('Count_Words.txt')11path.write_text(x)
x
1import matplotlib.pyplot as plt2
3def threepoints(z):4 x1=z[0][0]5 y1=z[0][1]6 x2=z[1][0]7 y2=z[1][1]8 y=z[2][1]9 x=z[2][0]10 if (x2-x1)*y-(y2-y1)*x-y1*x2+y2*x1==0:11 print('They lie on a straight line.')12 else:13 fig, ax= plt.subplots()14 ax.plot([z[0][0],z[1][0]],[z[0][1],z[1][1]])15 ax.plot([z[1][0],z[2][0]],[z[1][1],z[2][1]])16 ax.plot([z[0][0],z[2][0]],[z[0][1],z[2][1]])17 plt.show()18 19n=[[1,1],[1,2],[1,3]]20m=[[1,1],[1,3],[2,2]]21threepoints(n)22threepoints(m)
xxxxxxxxxx1from pathlib import Path2import csv3
4path = Path('midterm.csv')5lines = path.read_text().splitlines()6
7reader = csv.reader(lines)8header_row = next(reader)9
10rows = []11for row in reader:12 rows.append(row[1])13
14def transform(score):15 if score<=100 and score>=90:16 return 'A'17 elif score<90 and score>=80:18 return 'B'19 elif score<80 and score>=70:20 return 'C'21 elif score<70 and score>=60:22 return 'D'23 elif score<60:24 return 'F'25t = []26for yy in rows:27 t.append(transform(float(yy)))28c=[t.count('A'),t.count('B'),t.count('C'),t.count('D'),t.count('F')]29# Visualize the results.30import plotly.express as px31
32data = ['A', 'B', 'C', 'D', 'F']33fig = px.bar(x = data, y=c)34fig.write_html('midterm.html')4.
x
1from pathlib import Path2import json3
4path = Path('data.json')5contents = path.read_text(encoding='utf-8')6data = json.loads(contents)7
8fre = dict()9for cx in data:10 if cx[1] not in fre:11 fre[cx[1]] = 112 else:13 fre[cx[1]]+=114name = []15count =[]16for x, y in fre.items():17 name.append(x)18 count.append(y)19
20# Visualize the results.21import plotly.express as px22
23fig = px.bar(x=name,y=count)24fig.write_html('canteen.html')5.
xxxxxxxxxx1import json2import matplotlib.pyplot as plt3from datetime import datetime4from pathlib import Path5
6path = Path('data.json')7contents = path.read_text(encoding='utf-8')8data = json.loads(contents)9
10fre=dict()11for cx in data:12 if cx[0] not in fre:13 if float(cx[2])<0:14 fre[cx[0]] = -float(cx[2])15 else: 16 if float(cx[2])<0:17 fre[cx[0]]=fre[cx[0]]-float(cx[2])18dates = [] 19consumption=[]20for x, y in fre.items():21 current_date = datetime.strptime(x, "%m/%d/%Y")22 dates.append(current_date.strftime('%m-%d-%Y'))23 consumption.append(y)24
25# Visualize the results.26plt.style.use('seaborn')27fig, ax = plt.subplots()28ax.plot(dates, consumption, c='red')29ax.set_title("Daily Consumption", fontsize=24)30ax.set_xlabel('dates', fontsize=16)31fig.autofmt_xdate()32ax.set_ylabel("Consumption", fontsize=16)33ax.tick_params(labelsize=16)34
35plt.show()
xxxxxxxxxx1import csv2from pathlib import Path3from matplotlib import pyplot as plt4
5path = Path('athlete_events.csv')6lines = path.read_text().splitlines()7
8reader = csv.reader(lines)9header_row = next(reader)10 11year=[]12for row in reader:13 if row[7]=="CHN" and row[10]=="Summer":14 year.append(row[9])15
16from itertools import groupby17
18year.sort()19count={}20for key, value in groupby(year):21 count[key]=len(list(value))22c=[];labels=[]23for x,y in count.items():24 c.append(x)25 labels.append(y)26 27# Visualize the results.28plt.style.use('seaborn')29fig, ax = plt.subplots()30ax.plot(c,labels, c='red')31ax.set_title("Number of Athletes in Summer Games (China)", fontsize=24)32ax.set_xlabel('year', fontsize=16)33fig.autofmt_xdate()34ax.set_ylabel("number", fontsize=16)35ax.tick_params(labelsize=16)36
37plt.show()