Menu
CoddyTech

List by Course

Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 47 of 78.

challenge icon

Challenge

Easy

List the students who take a course. Keep everything you wrote in the previous steps.

1. The function. Below the other functions, write list_students_by_course(course: String) -> Array. It loops over student_records and returns an array with the names of the students whose "courses" set has the course, in the order the students were added. When nobody takes the course, it returns an empty array.

2. The command. Add a branch for course <course>, such as course Math, above ["show"]. It prints the array that list_students_by_course() returns. For the commands

add Alice 20 Math Physics
add Bob 22 Math Biology
add Diana 23 Chemistry Physics
course Math
course Physics
course Biology
course History
exit

the output is:

Student 'Alice' added successfully.
Student 'Bob' added successfully.
Student 'Diana' added successfully.
["Alice", "Bob"]
["Alice", "Diana"]
["Bob"]
[]
Goodbye!

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])
			["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()

All lessons in Logic & Flow

Practice on your own: Online GDScript compiler