Last active
May 30, 2020 02:49
-
-
Save derekkenney/e3d82dc0ba4313fc7ae37558c598fde9 to your computer and use it in GitHub Desktop.
Example of Determining Quadratic Runtime of a Go Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "log" | |
var planets1 = []string{"Tattooine", "Dantooine", "Bespin", "Yavin4"} | |
var planets2 = []string{"Exegol", "Jeda", "Jakku", "Mustafa", "Yavin4"} | |
var searches = 0 | |
func main() { | |
// Create a function that compares the names of planets in two arrays | |
// If same planet is found in both arrays, return true | |
comparePlanets(planets1, planets2) | |
} | |
func comparePlanets(planets []string, planets2 []string) { | |
for i := 0; i < len(planets1); i++ { | |
for j := 0; j < len(planets2); j++ { | |
searches++ | |
if planets[i] == planets2[j] { | |
log.Printf("Found Yoda on the planet %s\n", planets[i]) | |
log.Printf("The rebels searched %v times looking for Yoda", searches) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment