move master to main #1

Merged
erius merged 18 commits from master into main 2024-06-05 17:39:20 +00:00
Showing only changes of commit d8ddcbdd21 - Show all commits

View file

@ -2,6 +2,39 @@
package hogwarts
func GetCourseList(prereqs map[string][]string) []string {
return []string{}
import (
"log"
"slices"
)
func singleCourseList(
course string, prereqs map[string][]string, list []string, traversed []string,
) []string {
if slices.Contains(traversed, course) {
log.Panic("Recursive prereqs, aborting...")
}
traversed = append(traversed, course)
for _, prereqCourse := range prereqs[course] {
if !slices.Contains(list, prereqCourse) {
i := slices.Index(list, course)
if i == -1 {
list = append(list, prereqCourse)
} else {
list = slices.Insert(list, i, prereqCourse)
}
}
list = singleCourseList(prereqCourse, prereqs, list, traversed)
}
return list
}
func GetCourseList(prereqs map[string][]string) []string {
list := make([]string, 0)
for course := range prereqs {
if !slices.Contains(list, course) {
list = append(list, course)
}
list = singleCourseList(course, prereqs, list, []string{})
}
return list
}