Top Students
Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 48 of 78.
Challenge
EasyLast, find the top students. Keep everything you wrote in the previous steps.
1. The function. Below the other functions, write filter_top_students(threshold: float) -> Array. It returns an array with the names of the students whose average grade is greater than the threshold, in the order they were added. Get each average with calculate_average_grade(); a student with no grades has the average 0.0.
2. The command. Add a branch for top <threshold>, such as top 80, above ["show"]. It converts the threshold with float() and prints the array that filter_top_students() returns. For the commands
add Alice 20 Math Physics
add Bob 22 Math Biology
add Diana 23 Chemistry Physics
grade Alice 90
grade Alice 85
grade Bob 75
grade Diana 95
top 80
top 90
top 100
exitthe output is:
Student 'Alice' added successfully.
Student 'Bob' added successfully.
Student 'Diana' added successfully.
Grade 90 added for student 'Alice'.
Grade 85 added for student 'Alice'.
Grade 75 added for student 'Bob'.
Grade 95 added for student 'Diana'.
["Alice", "Diana"]
["Diana"]
[]
Goodbye!Your Student Records Manager now combines dictionaries, sets, arrays, functions and match in one program.
Try it yourself
extends Node
# Every student: name -> {"age": int, "grades": Array, "courses": a set of course names}
var student_records := {}
func _ready():
while true:
var parts := Array(OS.read_string_from_stdin().split(" ", false))
match parts:
["add", var student, var age, ..]:
add_student(student, int(age), parts.slice(3))
["grade", var student, var grade]:
add_grade(student, int(grade))
["enrolled", var student, var course]:
print(is_enrolled(student, course))
["average", var student]:
var average := calculate_average_grade(student)
if average >= 0:
print("Average grade of '%s': %.2f" % [student, average])
["course", var course]:
print(list_students_by_course(course))
["show"]:
print(student_records)
["exit"]:
print("Goodbye!")
break
func add_student(student_name: String, age: int, courses: Array) -> void:
if student_records.has(student_name):
print("Student '%s' already exists." % student_name)
return
var course_set := {}
for course in courses:
course_set[course] = true
student_records[student_name] = {"age": age, "grades": [], "courses": course_set}
print("Student '%s' added successfully." % student_name)
func add_grade(student_name: String, grade: int) -> void:
if not student_records.has(student_name):
print("Student '%s' not found." % student_name)
return
student_records[student_name]["grades"].append(grade)
print("Grade %d added for student '%s'." % [grade, student_name])
func is_enrolled(student_name: String, course: String) -> bool:
if not student_records.has(student_name):
print("Student '%s' not found." % student_name)
return false
return student_records[student_name]["courses"].has(course)
func calculate_average_grade(student_name: String) -> float:
if not student_records.has(student_name):
print("Student '%s' not found." % student_name)
return -1.0
var grades: Array = student_records[student_name]["grades"]
if grades.is_empty():
return 0.0
var total := 0
for grade in grades:
total += grade
return float(total) / grades.size()
func list_students_by_course(course: String) -> Array:
var students_in_course := []
for student_name in student_records:
if student_records[student_name]["courses"].has(course):
students_in_course.append(student_name)
return students_in_course
All lessons in Logic & Flow
1Strings In Depth
String MethodsSearching in StringsReplace and TrimSplit and JoinRecap - Username Check4Dictionaries Part 2
Nested DictionariesArrays of DictionariesMerging DictionariesCopying DictionariesRecap - Frequency Counter2Advanced Decision Making
Match With VariablesMatch GuardsArray PatternsDictionary PatternsMembership ChecksRecap - Command Parser5Contact Book Application
Display MenuAdd Contact8Student Records Manager
Project OverviewAdd Student3Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysJagged ArraysCommon 2D Array PatternsRecap - Game Board6Sets with Dictionaries
What is a Set?Dictionary as a SetAdding and RemovingUnion and IntersectionSet DifferenceRecap - Remove Duplicates9Advanced Data Aggregation
Summing ValuesFinding Minimum and MaximumCustom SortingRecap - Dictionary SorterPractice on your own: Online GDScript compiler