Skip to content

Commit 2b84479

Browse files
part 6.1 some exercises uploaded + aditional files
1 parent 274aa1a commit 2b84479

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+110287
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# This program works with two CSV files.
2+
# One of them contains information about some students on a course:
3+
4+
# id;first;last
5+
# 12345678;peter;pythons
6+
# 12345687;jean;javanese
7+
# 12345699;alice;adder
8+
9+
# The other contains the number of exercises each student has completed each week:
10+
11+
# id;e1;e2;e3;e4;e5;e6;e7
12+
# 12345678;4;1;1;4;5;2;4
13+
# 12345687;3;5;3;1;5;4;6
14+
# 12345699;10;2;2;7;10;2;2
15+
16+
# As you can see above, both CSV files also have a header row, which tells you what each column contains.
17+
18+
# Please write a program which asks the user for the names of these two files, reads the files, and then prints out the total number of exercises completed by each student.
19+
# If the files have the contents in the examples above, the program should print out the following:
20+
21+
22+
# Student information: students1.csv
23+
# Exercises completed: exercises1.csv
24+
# pekka peloton 21
25+
# jaana javanainen 27
26+
# liisa virtanen 35
27+
28+
# Hint: while testing your program, you may quickly run out of patience if you always have to type in the file names at the prompt.
29+
# You might want to hard-code the user input, like so:
30+
31+
# if False:
32+
# # this is never executed
33+
# student_info = input("Student information: ")
34+
# exercise_data = input("Exercises completed: ")
35+
# else:
36+
# # hard-coded input
37+
# student_info = "students1.csv"
38+
# exercise_data = "exercises1.csv"
39+
40+
# The actual functionality of the program is now "hidden" in the False branch of an if statement. It will never be executed.
41+
42+
# Now, if you want to quickly verify the program works correctly also with user input, you can just replace False with True:
43+
44+
45+
# if True:
46+
# student_info = input("Student information: ")
47+
# exercise_data = input("Exercises completed: ")
48+
# else:
49+
# # now this is the False branch, and is never executed
50+
# student_info = "students1.csv"
51+
# exercise_data = "exercises1.csv"
52+
53+
# When you have verified your program works correctly, you can remove the if structure, keeping the commands asking for input.
54+
55+
student_info = input("Student information: ")
56+
exercise_data = input("Exercises completed: ")
57+
58+
def create_dict(file):
59+
new_dict = {}
60+
with open(file) as new_file:
61+
for line in new_file:
62+
line = line.strip()
63+
parts = line.split(";")
64+
if parts[0] == "id":
65+
continue
66+
new_dict[parts[0]] = []
67+
for item in parts[1:]:
68+
if item.isdigit():
69+
new_dict[parts[0]].append(int(item))
70+
else:
71+
new_dict[parts[0]].append(item)
72+
return new_dict
73+
74+
def comb_dict(student_names, student_exercises):
75+
for id, name in student_names.items():
76+
if id in student_exercises:
77+
print(f"{name[0]} {name[1]} {sum(student_exercises[id])}")
78+
79+
student_names = create_dict(student_info)
80+
student_exercises = create_dict(exercise_data)
81+
comb_dict(student_names, student_exercises)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id;e1;e2;e3;e4;e5;e6;e7
2+
12345678;4;1;1;4;5;2;4
3+
12345687;3;5;3;1;5;4;6
4+
12345699;10;2;2;7;10;2;2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id;e1;e2;e3;e4;e5;e6;e7
2+
12345678;4;1;4;4;6;2;4
3+
12345687;3;5;3;1;5;4;6
4+
12345699;10;2;2;7;10;2;2
5+
12345688;0;0;0;0;0;0;0
6+
12345698;4;1;5;5;3;7;3
7+
12345700;3;3;7;7;7;3;2
8+
12345701;1;4;5;6;2;6;6
9+
12345702;3;3;5;2;6;7;2
10+
12345704;3;1;1;1;3;4;4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id;e1;e2;e3;e4;e5;e6;e7
2+
12345678;4;1;1;10;5;0;4
3+
12345687;0;5;3;7;5;4;6
4+
12345699;2;2;9;7;10;2;2
5+
12345688;10;10;10;0;0;0;10
6+
12345698;5;3;6;2;8;9;3
7+
12345700;1;1;2;3;3;2;4
8+
12345701;6;3;2;1;2;6;6
9+
12345702;6;2;6;2;1;1;6
10+
12345704;3;5;2;6;7;2;1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id;e1;e2;e3;e4;e5;e6;e7
2+
12345678;4;1;1;10;5;0;4
3+
12345687;0;5;3;7;5;4;6
4+
12345699;2;1;9;7;10;2;2
5+
12345688;10;1;6;2;10;6;0
6+
12345698;5;3;6;2;8;9;3
7+
12345700;1;2;1;3;3;2;4
8+
12345701;6;3;2;4;2;6;6
9+
12345702;6;2;0;2;1;1;6
10+
12345704;3;5;2;6;7;2;1
11+
12345709;2;5;4;6;1;2;1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id;first;last
2+
12345678;pekka;peloton
3+
12345687;jaana;javanainen
4+
12345699;liisa;virtanen
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id;first;last
2+
12345678;pekka;peloton
3+
12345687;jaana;javanainen
4+
12345699;liisa;virtanen
5+
12345688;donald;frump
6+
12345698;john;doe
7+
12345700;angela;tarkel
8+
12345701;karkki;eila
9+
12345702;alan;turing
10+
12345704;ada;lovelace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id;first;last
2+
12345678;pekka;peloton
3+
12345687;jaana;javanainen
4+
12345699;liisa;virtanen
5+
12345688;donald;frump
6+
12345698;john;doe
7+
12345700;angela;tarkel
8+
12345701;karkki;eila
9+
12345702;alan;turing
10+
12345704;ada;lovelace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id;first;last
2+
12345678;pekka;pelokas
3+
12345687;mirja;virtanen
4+
12345699;jane;doe
5+
12345688;donald;frump
6+
12345698;john;doe
7+
12345700;kalle;paakkola
8+
12345701;eila;kaisla
9+
12345702;antti;tuuri
10+
12345704;leena;lempinen
11+
12345709;eero;honkela
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Let's expand the program created in the previous exercise.
2+
# Now also the exam points awarded to each student are contained in a CSV file.
3+
# The contents of the file follow this format:
4+
5+
# id;e1;e2;e3
6+
# 12345678;4;1;4
7+
# 12345687;3;5;3
8+
# 12345699;10;2;2
9+
10+
# In the above example the student whose student number is 12345678 was awarded 4+1+4 points in the exam, which equals a total of 9 points.
11+
12+
# The program should again ask the user for the names of the files.
13+
# Then the program should process the files and print out a grade for each student.
14+
15+
# Student information: students1.csv
16+
# Exercises completed: exercises1.csv
17+
# Exam points: exam_points1.csv
18+
# pekka peloton 0
19+
# jaana javanainen 1
20+
# liisa virtanen 3
21+
22+
# Each completed exercise is counted towards exercise points, so that completing at least 10 % of the total exercices awards 1 point,
23+
# completing at least 20 % awards 2 points, etc. Completing all 40 exercises awards 10 points. The number of points awarded is always an integer number.
24+
25+
# The final grade for the course is determined based on the sum of exam and exercise points according to the following table:
26+
27+
# exam points + exercise points grade
28+
# 0-14 0 (fail)
29+
# 15-17 1
30+
# 18-20 2
31+
# 21-23 3
32+
# 24-27 4
33+
# 28- 5
34+
35+
36+
student_info = input("Student information: ")
37+
exercise_data = input("Exercises completed: ")
38+
exam_points = input("Exam points: ")
39+
40+
41+
def create_dict(file):
42+
new_dict = {}
43+
with open(file) as new_file:
44+
for line in new_file:
45+
line = line.strip()
46+
parts = line.split(";")
47+
if parts[0] == "id":
48+
continue
49+
new_dict[parts[0]] = []
50+
for item in parts[1:]:
51+
if item.isdigit():
52+
new_dict[parts[0]].append(int(item))
53+
else:
54+
new_dict[parts[0]].append(item)
55+
return new_dict
56+
57+
def comb_dict(student_names, student_exercises, student_exams):
58+
for id, name in student_names.items():
59+
if id in student_exercises:
60+
grade = calculate_grade(sum(student_exercises[id]), sum(student_exams[id]))
61+
print(f"{name[0]} {name[1]} {grade}")
62+
63+
def calculate_grade(exercises, exam_points):
64+
point_exerc = int((exercises/40) * 10)
65+
total_points = point_exerc + exam_points
66+
grade = 5
67+
if total_points <= 14:
68+
grade = 0
69+
elif total_points <= 17:
70+
grade = 1
71+
elif total_points <= 20:
72+
grade = 2
73+
elif total_points <= 23:
74+
grade = 3
75+
elif total_points <= 27:
76+
grade = 4
77+
return grade
78+
79+
student_names = create_dict(student_info)
80+
student_exercises = create_dict(exercise_data)
81+
student_exams = create_dict(exam_points)
82+
comb_dict(student_names, student_exercises, student_exams)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id;e1;e2;e3
2+
12345678;4;1;4
3+
12345687;3;5;3
4+
12345699;10;2;2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id;e1;e2;e3
2+
12345678;3;5;3
3+
12345687;4;1;5
4+
12345699;2;2;2
5+
12345688;5;5;5
6+
12345698;4;2;10
7+
12345700;3;3;7
8+
12345701;1;4;2
9+
12345702;5;9;5
10+
12345704;9;9;9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id;e1;e2;e3
2+
12345678;4;1;4
3+
12345687;3;5;3
4+
12345699;10;2;2
5+
12345688;0;0;0
6+
12345698;4;1;5
7+
12345700;3;3;7
8+
12345701;1;4;5
9+
12345702;3;9;5
10+
12345704;4;10;10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id;e1;e2;e3
2+
12345678;4;1;1
3+
12345687;0;5;3
4+
12345699;10;2;2
5+
12345688;10;6;0
6+
12345698;8;9;3
7+
12345700;3;2;4
8+
12345701;6;7;6
9+
12345702;1;1;6
10+
12345704;3;5;2
11+
12345709;2;5;4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id;e1;e2;e3;e4;e5;e6;e7
2+
12345678;4;1;1;4;5;2;4
3+
12345687;3;5;3;1;5;4;6
4+
12345699;10;2;2;7;10;2;2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id;e1;e2;e3;e4;e5;e6;e7
2+
12345678;4;1;4;4;6;2;4
3+
12345687;3;5;3;1;5;4;6
4+
12345699;10;2;2;7;10;2;2
5+
12345688;0;0;0;0;0;0;0
6+
12345698;4;1;5;5;3;7;3
7+
12345700;3;3;7;7;7;3;2
8+
12345701;1;4;5;6;2;6;6
9+
12345702;3;3;5;2;6;7;2
10+
12345704;3;1;1;1;3;4;4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id;e1;e2;e3;e4;e5;e6;e7
2+
12345678;4;1;1;10;5;0;4
3+
12345687;0;5;3;7;5;4;6
4+
12345699;2;2;9;7;10;2;2
5+
12345688;10;10;10;0;0;0;10
6+
12345698;5;3;6;2;8;9;3
7+
12345700;1;1;2;3;3;2;4
8+
12345701;6;3;2;1;2;6;6
9+
12345702;6;2;6;2;1;1;6
10+
12345704;3;5;2;6;7;2;1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id;e1;e2;e3;e4;e5;e6;e7
2+
12345678;4;1;1;10;5;0;4
3+
12345687;0;5;3;7;5;4;6
4+
12345699;2;1;9;7;10;2;2
5+
12345688;10;1;6;2;10;6;0
6+
12345698;5;3;6;2;8;9;3
7+
12345700;1;2;1;3;3;2;4
8+
12345701;6;3;2;4;2;6;6
9+
12345702;6;2;0;2;1;1;6
10+
12345704;3;5;2;6;7;2;1
11+
12345709;2;5;4;6;1;2;1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id;first;last
2+
12345678;pekka;peloton
3+
12345687;jaana;javanainen
4+
12345699;liisa;virtanen
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id;first;last
2+
12345678;pekka;peloton
3+
12345687;jaana;javanainen
4+
12345699;liisa;virtanen
5+
12345688;donald;frump
6+
12345698;john;doe
7+
12345700;angela;tarkel
8+
12345701;karkki;eila
9+
12345702;alan;turing
10+
12345704;ada;lovelace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
id;first;last
2+
12345678;pekka;peloton
3+
12345687;jaana;javanainen
4+
12345699;liisa;virtanen
5+
12345688;donald;frump
6+
12345698;john;doe
7+
12345700;angela;tarkel
8+
12345701;karkki;eila
9+
12345702;alan;turing
10+
12345704;ada;lovelace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
id;first;last
2+
12345678;pekka;pelokas
3+
12345687;mirja;virtanen
4+
12345699;jane;doe
5+
12345688;donald;frump
6+
12345698;john;doe
7+
12345700;kalle;paakkola
8+
12345701;eila;kaisla
9+
12345702;antti;tuuri
10+
12345704;leena;lempinen
11+
12345709;eero;honkela

0 commit comments

Comments
 (0)