Repository navigation
Expand file tree
/
Copy pathpure_python_zero.py
More file actions
163 lines (135 loc) · 3.39 KB
/
Copy pathpure_python_zero.py
File metadata and controls
163 lines (135 loc) · 3.39 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
f = open("scores.csv", "r",
encoding="utf-8")
lines = f.readlines()
f.close()
#1단계 파일 읽기
#print(len(lines))
#print(lines[0])
#print(lines[1])
'''
#2단계: 헤더 파싱
#header = lines[0].strip().split(",")
#print(header)
#print(first_line)
'''
'''
#3단계: 카테고리별 점수 출력
header = lines[0].strip().split(",")
cat_idx = header.index("category")
score_idx = header.index("score")
for line in lines[1:4]:
parts = line.strip().split(",")
print(parts[cat_idx],
parts[score_idx])
'''
'''
#4단계: 점수 계산(빈줄에서 에러)
header = lines[0].strip().split(",")
cat_idx = header.index("category")
score_idx = header.index("score")
for line in lines[1:]:
parts = line.strip().split(",")
score = float(parts[score_idx])
print(parts[cat_idx], score)
'''
'''
#5단계: 점수 계산(빈칸은 잘 건너갔는데, 미제출이라는 답변이 있어서 에러)
header = lines[0].strip().split(",")
cat_idx = header.index("category")
score_idx = header.index("score")
for line in lines[1:]:
parts = line.strip().split(",")
raw = parts[score_idx].strip()
if raw == "":
continue
score = float(raw)
print(parts[cat_idx], score)
'''
'''
#6단계: 점수 계산(빈칸은 잘 건너갔는데, 미제출이라는 답변이 있어서 에러)
header = lines[0].strip().split(",")
cat_idx = header.index("category")
score_idx = header.index("score")
for line in lines[1:]:
parts = line.strip().split(",")
raw = parts[score_idx].strip()
if raw == "":
continue
score = float(raw)
print(parts[cat_idx], score)
'''
# 1. 에러 발생마다 예외사항 넣어주기인데 이제는 해보고 안되면 뛰어넘는 방식을 할것임
#7단계
'''
header = lines[0].strip().split(",")
cat_idx = header.index("category")
score_idx = header.index("score")
count = 0
for line in lines[1:]:
parts = line.strip().split(",")
raw = parts[score_idx].strip()
if raw == "":
continue
try:
score = float(raw)
except ValueError:
continue
count += 1
print("사용한 행의 수:", count)
'''
#8단계
'''
header = lines[0].strip().split(",")
cat_idx = header.index("category")
score_idx = header.index("score")
count = 0
totals = {}
counts = {}
for line in lines[1:]:
parts = line.strip().split(",")
raw = parts[score_idx].strip()
if raw == "":
continue
try:
score = float(raw)
except ValueError:
continue
category = parts[cat_idx]
if category not in totals:
totals[category] = 0.0
counts[category] = 0
totals[category] += score
counts[category] += 1
count += 1
print(totals)
print(counts)
'''
#9단계
header = lines[0].strip().split(",")
cat_idx = header.index("category")
score_idx = header.index("score")
count = 0
totals = {}
counts = {}
for line in lines[1:]:
parts = line.strip().split(",")
raw = parts[score_idx].strip()
if raw == "":
score = 0.0
else:
try:
score = float(raw)
except ValueError:
continue
category = parts[cat_idx]
if category not in totals:
totals[category] = 0.0
counts[category] = 0
totals[category] += score
counts[category] += 1
count += 1
print(totals)
print(counts)
for c in sorted(totals.keys()):
avg = totals[c] / counts[c]
print(c, round(avg, 2))