Skip to content

Commit 4aabfb2

Browse files
part 5.4 finished
1 parent 0a4f61f commit 4aabfb2

File tree

4 files changed

+189
-1
lines changed

4 files changed

+189
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
1. [More lists](https://github.com/antoniolopez7217/Python_Programming_MOOC/tree/main/part5/1.%20More%20lists)
3232
2. [References](https://github.com/antoniolopez7217/Python_Programming_MOOC/tree/main/part5/2.%20References)
3333
3. [Dictionary](https://github.com/antoniolopez7217/Python_Programming_MOOC/tree/main/part5/3.%20Dictionary)
34-
4. [Tuple]()
34+
4. [Tuple](https://github.com/antoniolopez7217/Python_Programming_MOOC/tree/main/part5/4.%20Tuple)

part5/4. Tuple/letter_square.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This final exercise in this part is a relatively demanding problem solving task.
2+
# It can be solved in many different ways.
3+
# Even though this current section in the material covers tuples, tuples are not necessarily the best way to go about solving this.
4+
5+
# Please write a program which prints out a square of letters as specified in the examples below.
6+
# You may assume there will be at most 26 layers.
7+
8+
# Layers: 3
9+
10+
# CCCCC
11+
# CBBBC
12+
# CBABC
13+
# CBBBC
14+
# CCCCC
15+
16+
# Layers: 4
17+
18+
# DDDDDDD
19+
# DCCCCCD
20+
# DCBBBCD
21+
# DCBABCD
22+
# DCBBBCD
23+
# DCCCCCD
24+
# DDDDDDD
25+
26+
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
27+
layers = int(input("Layers: "))
28+
29+
left = ""
30+
right = ""
31+
next_char = layers - 1
32+
chars_between = 2 * layers - 1
33+
while next_char >= 1:
34+
left = left + alphabet[next_char]
35+
right = alphabet[next_char] + right
36+
chars_between -= 2
37+
print(left+alphabet[next_char] * chars_between + right)
38+
next_char -= 1
39+
while next_char <= layers - 1:
40+
print(left + alphabet[next_char] * chars_between + right)
41+
left = left[:-1]
42+
right = right[1:]
43+
chars_between += 2
44+
next_char += 1

part5/4. Tuple/older_people.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# In this exercise we are handling tuples just like the ones described in the previous exercise.
2+
3+
# Please write a function named older_people(people: list, year: int), which selects all those people on the list who were born before the year given as an argument.
4+
# The function should return the names of these people in a new list.
5+
6+
# An example of its use:
7+
8+
# p1 = ("Adam", 1977)
9+
# p2 = ("Ellen", 1985)
10+
# p3 = ("Mary", 1953)
11+
# p4 = ("Ernest", 1997)
12+
# people = [p1, p2, p3, p4]
13+
14+
# older = older_people(people, 1979)
15+
# print(older)
16+
17+
# [ 'Adam', 'Mary' ]
18+
19+
def older_people(people: list, year: int):
20+
people_list = []
21+
for person in people:
22+
if person[1] < year:
23+
people_list.append(person[0])
24+
return people_list

part5/4. Tuple/student_database.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# In this series of exercises you will create a simple student database.
2+
# Before diving in, please spend a moment reading through the instructions and thinking about what
3+
# sort of data structures are necessary for organising the data stored by your program.
4+
5+
# Part 1: adding students
6+
# First write a function named add_student, which adds a new student to the database.
7+
# Also write a preliminary version of the function print_student, which prints out the information of a single student.
8+
9+
# These function are used as follows:
10+
11+
# students = {}
12+
# add_student(students, "Peter")
13+
# add_student(students, "Eliza")
14+
# print_student(students, "Peter")
15+
# print_student(students, "Eliza")
16+
# print_student(students, "Jack")
17+
18+
# Your program should now print out
19+
20+
# Peter:
21+
# no completed courses
22+
# Eliza:
23+
# no completed courses
24+
# Jack: no such person in the database
25+
26+
# Part 2: adding completed courses
27+
# Please write a function named add_course, which adds a completed course to the information of a specific student in the database.
28+
# The course data is a tuple consisting of the name of the course and the grade:
29+
30+
# students = {}
31+
# add_student(students, "Peter")
32+
# add_course(students, "Peter", ("Introduction to Programming", 3))
33+
# add_course(students, "Peter", ("Advanced Course in Programming", 2))
34+
# print_student(students, "Peter")
35+
36+
# When some courses have been added, the information printed out changes:
37+
38+
# Peter:
39+
# 2 completed courses:
40+
# Introduction to Programming 3
41+
# Advanced Course in Programming 2
42+
# average grade 2.5
43+
44+
# Part 3:brepeating courses
45+
# Courses with grade 0 should be ignored when adding course information. Additionally, if the course is already in the database in that specific
46+
# student's information, the grade recorded in the database should never be lowered if the course is repeated.
47+
48+
# students = {}
49+
# add_student(students, "Peter")
50+
# add_course(students, "Peter", ("Introduction to Programming", 3))
51+
# add_course(students, "Peter", ("Advanced Course in Programming", 2))
52+
# add_course(students, "Peter", ("Data Structures and Algorithms", 0))
53+
# add_course(students, "Peter", ("Introduction to Programming", 2))
54+
# print_student(students, "Peter")
55+
56+
# Peter:
57+
# 2 completed courses:
58+
# Introduction to Programming 3
59+
# Advanced Course in Programming 2
60+
# average grade 2.5
61+
62+
# Part 4: summary of database
63+
# Please write a function named summary, which prints out a summary based on all the information stored in the database.
64+
65+
# students = {}
66+
# add_student(students, "Peter")
67+
# add_student(students, "Eliza")
68+
# add_course(students, "Peter", ("Data Structures and Algorithms", 1))
69+
# add_course(students, "Peter", ("Introduction to Programming", 1))
70+
# add_course(students, "Peter", ("Advanced Course in Programming", 1))
71+
# add_course(students, "Eliza", ("Introduction to Programming", 5))
72+
# add_course(students, "Eliza", ("Introduction to Computer Science", 4))
73+
# summary(students)
74+
75+
# This should print out
76+
77+
# students 2
78+
# most courses completed 3 Peter
79+
# best average grade 4.5 Eliza
80+
81+
def add_student(students, name):
82+
students[name] = {}
83+
84+
def add_course(students, name, course):
85+
if course[1] == 0:
86+
return
87+
if course[0] in students[name]:
88+
if course[1] > students[name][course[0]]:
89+
students[name][course[0]] = course[1]
90+
else:
91+
students[name].update({course[0]:course[1]})
92+
93+
def summary(students):
94+
print(f"students {len(students)}")
95+
most_courses = ["", 0]
96+
best_average = ["", 0]
97+
for key, value in students.items():
98+
if len(value) > most_courses[1]:
99+
most_courses = [key, len(value)]
100+
avg = sum(value.values())/len(value)
101+
if avg > best_average[1]:
102+
best_average = [key, avg]
103+
print(f"most courses completed {most_courses[1]} {most_courses[0]} ")
104+
print(f"best average grade {best_average[1]} {best_average[0]} ")
105+
106+
107+
def print_student(students, name):
108+
if name not in students:
109+
print(f"{name}: no such person in the database")
110+
return
111+
print(f"{name}:")
112+
if students[name] == {}:
113+
print(" no completed courses")
114+
else:
115+
print(f" {len(students[name])} completed courses:")
116+
values_sum = 0
117+
for key, value in students[name].items():
118+
print(f" {key} {value}")
119+
values_sum += value
120+
print(f" average grade {values_sum / len(students[name])}")

0 commit comments

Comments
 (0)