Transcript from the "Complex Structures Solution" Lesson
[00:00:00]
>> So the first thing I'm going to do is create a file in our code directory for exercise for C. And if we're talking about part one, the first thing we wanted to do is switch back to the markdown file, is instantiate an array of scores. The array should have at least five elements of type float 64, and then write a function that calculates that average.
[00:00:21] So in the solution here, my function of average is going to take a series of scores. And that type signature indicates that it's supposed to accept an array of five elements, all of which are float 64, and return a float 64 at the end. In order to calculate the average, I'm instantiating a total initial value of 0.0 since we're working with a float.
[00:00:41] And then we're using the range keyword to iterate over all of the scores we get in, add them to that total number. And then at the end of the day, we wanna return total divided by a float 64 version of however many scores we passed in. So using that len operator on the scores argument.
[00:01:07] So on the main function here, I'm just gonna print line scores. And then if we run our exercise so far. We get an array of five scores, which is because I am simply printing out those scores on line 15. So instead of doing that, let's actually call the function which is going to be average.
[00:01:42] And then pass in that array of scores. Run this one more time and you get that result of 5.4. Cool, so let's look at part two. In part two, the first thing we wanted to do was set up a map that contains a set of pet names and their corresponding animal type.
[00:02:12] For instance, if we have a pet name of Fido, it's set to a value of dog, both of which are strings. I'm gonna create a second function underneath our average. And this can keep everything in the same file for this example, and I'm gonna call it. We'll hold off on that.
[00:02:36] According to the actual exercise file, the first thing we're trying to do is define an initial map. So let's do that first. So I'm gonna keep this outside of my main function, and use the var keyword and say, initial pets. So I'm gonna set this equal to, it's gonna be a map type.
[00:02:55] The key is a string. The value is also a string. And that is going to be equal to a map type with a key of string, value of string, and then the curly braces to insert some initial values here. So let's throw in a few pets, we've got Fido, that's a dog.
[00:03:14] We've got Penelope, can be a horse. And Nancy will be a cat. [LAUGH] Cool, so we have our initial pets array, map, excuse me. Back in exercise 4C, we wanna write a function that takes a string argument and returns a Boolean indicating whether or not that key exists in our map of pets.
[00:03:45] So, all right, let's create a function. I'm gonna call it, doesPetExist. According to the instructions, it's gonna take some sort of pet name, that's a string type. And it's gonna return a Boolean for whether or not that pet exists within our map. So if you recall from the lesson plan earlier, looking up a key value pair in a map will return two different values.
[00:04:11] It will return the value if it exists. And then that okay Boolean for whether or not, I'm sorry, the value of the actual string if it exists. And then an okay Boolean for whether or not it's successfully found that element within the map. So I'm gonna set those two values on the left-hand side here.
[00:04:28] And then I'm gonna grab my initial pets map, and check to see if whatever pet name we pass into this function exists within our map. We'll return okay here. And then because I'm not using this value here, so I don't get an error, I'll replace that with an underscore.
[00:04:54] I'm gonna comment out the two lines within our main that were from the average array exercise. And then we're going to say, okay, let's say our pet name here is Fido. And then let's check to see if pet exists. I'm gonna set that as a variable and assign it to a value of does pet exist, and then pass it, the pet name that we came up with for now.
[00:05:19] And then let's just PrintLinePetExists. Cool. So we're getting true back because we passed in Fido. If I change this to Spot, we'll get false. And then last but not least here, we'll look at that third part that's looking at slices. So part one of part three is to instantiate a slice that has an initial value of a collection of groceries.
[00:06:00] So back in our Go file. I'm going to establish a new set of groceries up here. We're gonna call it initial groceries. And it's going to be a slice of strings. And we can do the inference bit here. Because we are passing in a series of strings, Go is going to let us define the initial values without having a specific number.
[00:06:33] Because even though we're still talking about a slice, it can't instantiate an initial array behind the scenes of the length that we're passing it to begin with. So here we have initial groceries which is a slice set to a few groceries. And then the second part here is to write a function that takes one or more groceries as strings.
[00:06:55] And then append them to the slice, printing out the resulting list of groceries. So this guy gets a tiny bit more complicated. We're not just passing in one thing, we could potentially be passing in another list of groceries like three or four groceries. So we're gonna have to do some iterating to make sure that each of the groceries gets appended to our final groceries array.
[00:07:19] So I'm gonna create a function here called addGroceryToList. It's gonna need two things. Well, just gonna need one thing, we're gonna need the new grocery that we wanna add to our list which is a set of an unknown number of strings. So let's modify this variable to indicate that it could be potentially more than one thing.
[00:07:41] So new groceries using the variadic to indicate one or more strings. And then at the end of the day, we do want to return that slice of strings as our final value. So within addGrocery, we need to do a couple things. First I'm gonna establish that right now our starting food variable is going to be our initial groceries.
[00:08:10] And then I'm gonna use the for loop, and iterate over all of the groceries we just passed in. So for each new grocery, we're missing the range keyword here. For each new grocery I wanna reassign what foods is and establish a new array, so we're going to append whatever's from that initial array of foods.
[00:08:40] And then add on each individual new grocery that we just want to put in our slice. And then because my function signature here indicates that I do want to return that whole slice of strings, I will return foods. In our main function, we can comment out the existing code, set a new variable to groceryList Which is gonna be equal to addGroceryToList, and lets say beets.
[00:09:23] If I PrintLine. You'll see that that single element beets has been added to our list, just to make sure it's behaving correctly. What if we want beets and then maybe some chocolate, maybe some wine. You'll see that each of those things has also been appended to our final array.