Skip to content

Commit d51e228

Browse files
committed
update
1 parent fbf6b61 commit d51e228

File tree

737 files changed

+670318
-146
lines changed

Some content is hidden

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

737 files changed

+670318
-146
lines changed

Diff for: 0 - Introduction to Programming Notes/Part 6 - Handling errors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def number_error(lottery_numbers):
310310
return False
311311

312312
def filter_incorrect():
313-
with open("Exercise Files\lottery_numbers.csv") as main, open("Exercise Files\correct_numbers.csv", "w") as correct:
313+
with open("Data-Files\lottery_numbers.csv") as main, open("Data-Files\correct_numbers.csv", "w") as correct:
314314
for line in main:
315315
line = line.strip()
316316
parts = line.split(";")

Diff for: 0 - Introduction to Programming Notes/Part 6 - Reading Files.py

+25-25
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
# The header line opens the file, and the block where the file can be accessed follows.
44
# After the block the file is automatically closed, and can no longer be accessed.
55

6-
with open("Exercise Files\example.txt") as new_file:
6+
with open("Data-Files\example.txt") as new_file:
77
contents = new_file.read()
88
print(contents)
99

1010
# Going through the contents of a file
11-
with open("Exercise Files\example.txt") as new_file:
11+
with open("Data-Files\example.txt") as new_file:
1212
count = 0
1313
total_length = 0
1414

@@ -55,7 +55,7 @@ def largest():
5555
print(word)
5656

5757
# Reading CSV files
58-
with open("Exercise Files\grades.csv") as new_file:
58+
with open("Data-Files\grades.csv") as new_file:
5959
for line in new_file:
6060
line = line.replace("\n", "")
6161
parts = line.split(";")
@@ -66,7 +66,7 @@ def largest():
6666

6767
# Fruit market
6868
def read_fruits():
69-
with open('Exercise Files\\fruits.csv') as file:
69+
with open('Data-Files\\fruits.csv') as file:
7070
fruits = {}
7171

7272
for row in file:
@@ -80,7 +80,7 @@ def read_fruits():
8080

8181
# Matrix - Approach 1
8282
def matrix_sum():
83-
with open("Exercise Files\matrix.txt") as matrix:
83+
with open("Data-Files\matrix.txt") as matrix:
8484
sum = 0
8585

8686
for row in matrix:
@@ -94,7 +94,7 @@ def matrix_sum():
9494
matrix_sum()
9595

9696
def matrix_max():
97-
with open("Exercise Files\matrix.txt") as matrix:
97+
with open("Data-Files\matrix.txt") as matrix:
9898
max = 0
9999

100100
for row in matrix:
@@ -109,7 +109,7 @@ def matrix_max():
109109
matrix_max()
110110

111111
def row_sums():
112-
with open("Exercise Files\matrix.txt") as matrix:
112+
with open("Data-Files\matrix.txt") as matrix:
113113
sums = []
114114

115115
for row in matrix:
@@ -126,7 +126,7 @@ def row_sums():
126126

127127
# Matrix - Approach 2
128128
def read_matrix():
129-
with open("Exercise Files\matrix.txt") as file:
129+
with open("Data-Files\matrix.txt") as file:
130130
m = []
131131
for row in file:
132132
mrow = []
@@ -235,7 +235,7 @@ def row_sums():
235235

236236
# More CSV file processing
237237
grades = {}
238-
with open("Exercise Files\grades.csv") as new_file:
238+
with open("Data-Files\grades.csv") as new_file:
239239
for line in new_file:
240240
line = line.replace("\n", "")
241241
parts = line.split(";")
@@ -394,8 +394,8 @@ def row_sums():
394394
exercise_data = input("Exercises completed: ")
395395
else:
396396
# hard-coded input
397-
student_info = "Exercise Files\course-grading-part-1\students1.csv"
398-
exercise_data = "Exercise Files\course-grading-part-1\exercises1.csv"
397+
student_info = "Data-Files\course-grading-part-1\students1.csv"
398+
exercise_data = "Data-Files\course-grading-part-1\exercises1.csv"
399399

400400
students = {}
401401
with open(student_info) as new_file:
@@ -463,9 +463,9 @@ def row_sums():
463463
exam_data = input("Exam points: ")
464464
else:
465465
# hard-coded input
466-
student_info = "Exercise Files\course-grading-part-2\students1.csv"
467-
exercise_data = "Exercise Files\course-grading-part-2\exercises1.csv"
468-
exam_data = "Exercise Files\course-grading-part-2\exam_points1.csv"
466+
student_info = "Data-Files\course-grading-part-2\students1.csv"
467+
exercise_data = "Data-Files\course-grading-part-2\exercises1.csv"
468+
exam_data = "Data-Files\course-grading-part-2\exam_points1.csv"
469469

470470
students = {}
471471
with open(student_info) as new_file:
@@ -577,9 +577,9 @@ def to_points(number):
577577
exam_data = input("Exam points: ")
578578
else:
579579
# hard-coded input
580-
student_info = "Exercise Files\course-grading-part-3\students1.csv"
581-
exercise_data = "Exercise Files\course-grading-part-3\exercises1.csv"
582-
exam_data = "Exercise Files\course-grading-part-3\exam_points1.csv"
580+
student_info = "Data-Files\course-grading-part-3\students1.csv"
581+
exercise_data = "Data-Files\course-grading-part-3\exercises1.csv"
582+
exam_data = "Data-Files\course-grading-part-3\exam_points1.csv"
583583

584584
students = {}
585585
with open(student_info) as new_file:
@@ -705,7 +705,7 @@ def to_points(number):
705705

706706
text = text.split(" ")
707707

708-
with open("Exercise Files\wordlist.txt") as wordlist:
708+
with open("Data-Files\wordlist.txt") as wordlist:
709709
words = []
710710
for line in wordlist:
711711
word = line.strip()
@@ -745,7 +745,7 @@ def wordlist():
745745
# Recipe search - Approach 1
746746

747747
# Part 1 - Search for recipes based on the name of the recipe
748-
with open('Exercise Files/recipes1.txt') as my_recipes:
748+
with open('Data-Files/recipes1.txt') as my_recipes:
749749
recipe_name = []
750750
counter = 0
751751
for recipe in my_recipes:
@@ -764,7 +764,7 @@ def wordlist():
764764
print(recipe)
765765

766766
# Part 2 - Search for recipes based on the preparation time
767-
with open("Exercise Files/recipes1.txt") as new_file:
767+
with open("Data-Files/recipes1.txt") as new_file:
768768
prep_time = {}
769769
recipe = ""
770770
for line in new_file:
@@ -781,7 +781,7 @@ def wordlist():
781781
print(f"{recipe}, preparation time {time} min")
782782

783783
# Part 3 - Search for recipes based on the ingredients
784-
with open("Exercise Files/recipes1.txt") as new_file:
784+
with open("Data-Files/recipes1.txt") as new_file:
785785
prep_time = {}
786786
recipe = {}
787787
ingredients = []
@@ -871,9 +871,9 @@ def search_by_ingredient(filename: str, ingredient: str):
871871
if False:
872872
search_by_name("recipes1.txt", "milk")
873873
else:
874-
search_by_name(r"Exercise Files\\recipes1.txt", "cake")
875-
search_by_time(r"Exercise Files\\recipes1.txt", 30)
876-
search_by_ingredient(r"Exercise Files\\recipes1.txt", "milk")
874+
search_by_name(r"Data-Files\\recipes1.txt", "cake")
875+
search_by_time(r"Data-Files\\recipes1.txt", 30)
876+
search_by_ingredient(r"Data-Files\\recipes1.txt", "milk")
877877

878878

879879
# Recipe search - Approach 2
@@ -1038,7 +1038,7 @@ def greatest_distance(stations: dict):
10381038
station_y = station2
10391039
return station_x, station_y, distance
10401040

1041-
stations = get_station_data(r"Exercise Files\stations1.csv")
1041+
stations = get_station_data(r"Data-Files\stations1.csv")
10421042

10431043
d = distance(stations, "Designmuseo", "Hietalahdentori")
10441044
print(d)

Diff for: 0 - Introduction to Programming Notes/Part 6 - Writing files.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def get_grade(student_name, weekly_points):
271271

272272
# Filtering the contents of a file - Approach 1
273273
def filter_solutions():
274-
with open("Exercise Files/solutions.csv") as calculations:
274+
with open("Data-Files/solutions.csv") as calculations:
275275
correct_list = []
276276
incorrect_list = []
277277
for line in calculations:
@@ -298,15 +298,15 @@ def filter_solutions():
298298
row.append(part)
299299
incorrect_list.append(row)
300300

301-
with open("Exercise Files/correct.csv", "w") as my_file:
301+
with open("Data-Files/correct.csv", "w") as my_file:
302302
for result in correct_list:
303303
line = ""
304304
for value in result:
305305
line += f"{value};"
306306
line = line[:-1]
307307
my_file.write(line+"\n")
308308

309-
with open("Exercise Files/incorrect.csv", "w") as my_file:
309+
with open("Data-Files/incorrect.csv", "w") as my_file:
310310
for result in incorrect_list:
311311
line = ""
312312
for value in result:
@@ -362,7 +362,7 @@ def filter_solutions():
362362

363363
# Store personal data - Approach 1
364364
def store_personal_data(person: tuple):
365-
with open("Exercise Files\people.csv", "a") as database:
365+
with open("Data-Files\people.csv", "a") as database:
366366
line = ""
367367
for data in person:
368368
line += f"{data};"
@@ -480,9 +480,9 @@ def get_grade(student_name, weekly_points):
480480
exam_data = input("Exam points: ")
481481
else:
482482
# hard-coded input
483-
student_info = "Exercise Files\course-grading-part-3\students1.csv"
484-
exercise_data = "Exercise Files\course-grading-part-3\exercises1.csv"
485-
exam_data = "Exercise Files\course-grading-part-3\exam_points1.csv"
483+
student_info = "Data-Files\course-grading-part-3\students1.csv"
484+
exercise_data = "Data-Files\course-grading-part-3\exercises1.csv"
485+
exam_data = "Data-Files\course-grading-part-3\exam_points1.csv"
486486

487487
students = {}
488488
with open(student_info) as new_file:
@@ -646,10 +646,10 @@ def report(students: dict, exercises: dict, exams: dict):
646646
course_info = input("Course information: ")
647647
else:
648648
# hard-coded input
649-
student_info = "Exercise Files\course-grading-part-4\students4.csv"
650-
exercise_data = "Exercise Files\course-grading-part-4\exercises4.csv"
651-
exam_data = "Exercise Files\course-grading-part-4\exam_points4.csv"
652-
course_info = "Exercise Files\course-grading-part-4\course4.txt"
649+
student_info = "Data-Files\course-grading-part-4\students4.csv"
650+
exercise_data = "Data-Files\course-grading-part-4\exercises4.csv"
651+
exam_data = "Data-Files\course-grading-part-4\exam_points4.csv"
652+
course_info = "Data-Files\course-grading-part-4\course4.txt"
653653

654654
students = {}
655655
with open(student_info) as new_file:

Diff for: 0 - Introduction to Programming Notes/Part 7 - Data processing.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def print_persons(filename: str):
124124
for i in data:
125125
print(f"{i["name"]} {i["age"]} years ({", ".join(i["hobbies"])})")
126126

127-
print_persons("Exercise Files\\file2.json")
127+
print_persons("Data-Files\\file2.json")
128128

129129

130130
# Handing JSON files - Approach 2
@@ -376,8 +376,8 @@ def retrieve_course(course_name: str):
376376
}
377377

378378
# Who cheated - review
379-
# Exercise Files\start_times.csv
380-
# Exercise Files\submissions.csv
379+
# Data-Files\start_times.csv
380+
# Data-Files\submissions.csv
381381

382382
# The file 'start_times.csv' contains individual start times for a programming exam, in the format 'name;hh:mm'.
383383
# An example:
@@ -408,14 +408,14 @@ def retrieve_course(course_name: str):
408408

409409
def cheaters():
410410
student_dict = {}
411-
with open("Exercise Files\start_times.csv") as start_file:
411+
with open("Data-Files\start_times.csv") as start_file:
412412
for line in csv.reader(start_file, delimiter=";"):
413413
start_time = datetime.strptime(line[1], "%H:%M")
414414
cutoff_time = start_time + timedelta(hours=3)
415415
student_dict[line[0]] = cutoff_time
416416

417417
cheater_list = []
418-
with open("Exercise Files\submissions.csv") as sub_file:
418+
with open("Data-Files\submissions.csv") as sub_file:
419419
for line in csv.reader(sub_file, delimiter=";"):
420420
end_time = datetime.strptime(line[3], "%H:%M")
421421
if student_dict[line[0]] < end_time and line[0] not in cheater_list:
@@ -526,14 +526,14 @@ def cheaters():
526526
def final_points():
527527
cutoff_dict = {}
528528
points_dict = {}
529-
with open("Exercise Files\start_times.csv") as start_file:
529+
with open("Data-Files\start_times.csv") as start_file:
530530
for line in csv.reader(start_file, delimiter=";"):
531531
start_time = datetime.strptime(line[1], "%H:%M")
532532
cutoff_time = start_time + timedelta(hours=3)
533533
cutoff_dict[line[0]] = cutoff_time
534534
points_dict[line[0]] = {}
535535

536-
with open("Exercise Files\submissions.csv") as sub_file:
536+
with open("Data-Files\submissions.csv") as sub_file:
537537
for line in csv.reader(sub_file, delimiter=";"):
538538
end_time = datetime.strptime(line[3], "%H:%M")
539539
grade = int(line[2])
@@ -626,7 +626,7 @@ def final_points():
626626

627627
def words():
628628
wordlist = []
629-
with open("Exercise Files\words.txt") as wordfile:
629+
with open("Data-Files\words.txt") as wordfile:
630630
for word in wordfile:
631631
wordlist.append(word.strip())
632632
return wordlist

Diff for: 0 - Introduction to Programming Notes/Part 7 - Randomness.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,9 @@ def play(die1: str, die2: str, times: int):
479479
return v1, v2, t
480480

481481
# Random words - Review
482-
# Exercise Files\words.txt
482+
# Data-Files\words.txt
483483

484-
with open("Exercise Files\words.txt") as words:
484+
with open("Data-Files\words.txt") as words:
485485
match = []
486486
for word in words:
487487
word = word.strip()
@@ -500,7 +500,7 @@ def play(die1: str, die2: str, times: int):
500500
from random import sample
501501

502502
def words(n: int, beginning: str) -> list:
503-
with open("Exercise Files\words.txt") as words:
503+
with open("Data-Files\words.txt") as words:
504504
match = []
505505
for word in words:
506506
word = word.strip()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
List comprehension is an elegant way to define and create lists based on existing lists.
2+
List comprehension is generally more compact and faster than normal functions and loops for creating list.
3+
However, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.
4+
Remember, every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Taina;84-24;60
2+
Tony;75-15;60
3+
Pekka;31+95;126
4+
Mike;59-7;52
5+
Kirsi;27+12;39
6+
Toni;70+47;117
7+
Antti;91+84;175
8+
Matti;67-41;26
9+
Antti;65-39;26
10+
Tea;33+71;104
11+
Pekka;97+53;150
12+
Tony;48+66;114
13+
Emilia;23+30;53
14+
Tuula;99-42;57
15+
Arto;26+81;107
16+
Antti;85+38;123
17+
Toni;71-19;52
18+
Pekka;34+67;101
19+
Toni;89-19;70
20+
Tony;62+61;123
21+
Pekka;90-25;65
22+
Emilia;40+17;57
23+
Tanja;92+77;169
24+
Antti;36+95;131
25+
Paula;81-33;48
26+
Kirsi;88-41;47
27+
Emilia;69+74;143
28+
Juha;99-18;81
29+
Antti;68-31;37
30+
Tea;49+3;52
31+
Mia;34+79;113
32+
Paula;94-11;83
33+
Pekka;55-26;29
34+
Pekka;49+1;50
35+
Tiina;68-32;36
36+
Paula;67-10;57
37+
Lauri;86-21;65
38+
Erkki;62-9;53
39+
Matti;61+24;85
40+
Mia;51+36;87
41+
Toni;52-7;45
42+
Tiina;55-50;5

0 commit comments

Comments
 (0)