Introduction to Programming for Business Analytics - Exercise 4: Functions
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
Title of Series | ||
Number of Parts | 22 | |
Author | ||
License | CC Attribution 4.0 International: You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor. | |
Identifiers | 10.5446/65108 (DOI) | |
Publisher | ||
Release Date | ||
Language | ||
Producer |
Content Metadata
Subject Area | ||
Genre | ||
Keywords |
00:00
Computer animationLecture/Conference
09:27
Computer animation
18:26
Computer animation
27:24
Computer animation
36:23
Computer animation
45:21
Computer animation
54:20
Computer animation
01:03:18
Computer animation
01:12:17
Computer animation
01:21:15
Lecture/ConferenceComputer animation
Transcript: English(auto-generated)
00:10
Hi, welcome to the fourth exercise video for the Introduction to Programming for Business Analytics class. This video will be all about functions.
00:23
And let's get started with the first exercise, which is classification of functions. For each of the following Julia functions, answer each of the following questions and the first question is how many parameters does the function have? Okay, let's quickly go through the examples here.
00:41
So the first function is called fun, and it has no parameters. Second function, f of x has one parameter called x. The third function is one parameter called m. The next function has three parameters, they're called ra, i and j respectively.
01:02
And the next function is one parameter, it's called text. And this function has one parameter called n. And that's all of them. Next question is, is the function void or is it fruitful? As you know from the lecture, a fruitful function returns a value while a void function
01:22
does not. And the first function does not return anything, so it's void. And the second function returns x squared, so it's fruitful. And the third function returns the result of the last statement, and such, yeah, because
01:41
of this it is fruitful. The function swap also returns the result of the last statement, which is in this case the value of the variable temp here, so it's also fruitful. This function is its last statement, a call to a non fruitful function, in this case printlin.
02:06
So it's also not fruitful. And in other words, it's void. So print with emphasis is void, and printlin is also void. The function count down here, that is also void because it doesn't have a last statement
02:25
which has a value. Next question is, is the function recursive? As you know from the lecture, a recursive function is a function that calls itself. And this is not true for the function fun here, also not for f of x, not for add2.
02:44
It's not true for swap, so these are all what we call iterative functions. And it's also not true for print with emphasis, but it is true for count down. And as you can see, count down includes a call to itself here in line 4. So this is a recursive function.
03:02
The fourth question is, what types of values can be supplied as arguments if calling the function should not result in an error? And for every argument type, say whether it is a mutable type. So for the first function, this question doesn't really make sense because there are no arguments.
03:21
For the second function, we have one argument, one parameter called x, which means we have also one argument in the function. And the argument stands squared with this exponentization operator here and the two as the exponent. And this means we can pass any number into the function.
03:43
But we can also pass a string because squaring a string is just, just means that the string will be, will be doubled. I can, I can quickly show you this by just creating a new cell, running the function and now calling the function with a string.
04:04
And we receive twice the string. Okay, and for the next function, we have one parameter called n and inside of the function, we add one to n twice using these update operators here.
04:22
So that means we can only use types on which the plus operator is defined. And these are only numeric types. So only int and float can be supplied here. And of course, this is not, these are not mutable types and also the strings or
04:41
numbers we can supply to f are also not mutable types. And for the function swap, we have three parameters. And we use two of them, i and j as indices into this parameter called ra.
05:02
And this means that i and j have to be values that can be used as arguments to the bracket operator of ra. And in turn, ra obviously has to be some type on which we can use the bracket operator. And we have seen two types on which we can do this, namely vector and string.
05:28
And we know it's not a string because ra is also modified in line three and line four of this function. So we know it has to be among the types we have seen so far in this class,
05:42
it has to be vector. And as a result, we also know that i and j have to be integer numbers because they are used as index values into this vector ra here. For the next and also ra is a mutable.
06:03
Before I forget that ra is a mutable type because vectors are mutable, right? And i and j are immutable because they are numbers. And next function print with emphasis, we have one parameter, it's called text. And we concatenate two exclamation marks to text using this asterisk operator here.
06:23
So we know that text must be a string, which is a non mutable type. And for the function countdown, we are using the parameter n with a comparison operator here, comparing it to zero, we're printing it and then we're subtracting one from it.
06:44
So we know that it has to be a number. And either it can be int or float. And both of these are also immutable types, so not mutable. And the last question of the task is does calling the function change the mutable arguments
07:01
that are provided to the function? Okay, we've seen that only one of the functions has mutable arguments in the first place, which is swap. And we can see that these mutable arguments, the one mutable argument ra is changed in lines three and four. And we change it by assigning a new value to an a position in the vector.
07:29
So yes, the answer is yes here for swap. In fact, calling the function changes the mutable arguments in this case, the argument is called ra that are provided to the function.
07:42
For task two, translate between function syntaxes, we have this example of the function f of x, which we have given in two syntaxes once in the assignment form, and once in what we call the standard form. And both of these obviously semantically equivalent, right, they just return the return x squared.
08:08
So the only the only argument squared is then being returned. And the task asks us to give semantically equivalent functions for each of the following given functions.
08:20
So let's get started with that. Our first function is given the in the assignment form. So we have to provide a function in the standard form now, which means we start with the keyword function, then the name of the function, in this case, f underscore prime.
08:41
And then we have the parameter list inside of the parenthesis. Just one parameter here, it's called x. And now in the body of the function, we have to ensure that the function returns two times x. So we can just write the return keyword, we could also omit the return keyword here,
09:02
but I like to write it because I think it makes the code more clear. So return two times x, and the end keyword to end the function. That's it for f prime. Next function is called g and has three arguments and the function is given in the
09:22
standard syntax. So let's translate it to the assignment form now. As we know, we can omit the keyword function here, just start with the name, parameter list inside of the parenthesis, then the assignment operator.
09:41
Now we have to provide an expression that evaluates to exactly the same as this function returns. And we can start from the bottom by just taking, taking this part here from the function g.
10:01
And then we have two variables here, which are right now not defined in in this function down here, we can just take the definitions of the variables from the function and insert them down here, like this.
10:23
And like this. And now we've provided a semantically equivalent function, just like that.
10:40
Next function is called h, it's again given in the standard form, and we have to provide it in the assignment form now. So let's do that. h of a and b assignment operator. And now we have to look at the body of this function here and see what it actually
11:02
computes. Okay, so if a is smaller than b, it returns true. If b is smaller than a, it returns false. And if none of these, if branches have been entered, then it returns it just returns true.
11:20
Okay, so this is actually the same as a less than or equal operator. Like this, a less than or equal b. Because if a is smaller than b, we return true. And if b is smaller than a, which is the same as to say a is larger than b, we return
11:47
false. And the only remaining case here is that a and b are exactly equal. In this case, we return true. So it's just like the less than or equal operator. All right.
12:00
And with that, we're done implementing the function a in the assignment syntax. For task three, we have to implement a Julia function that checks if a letter is in a string. And the task says, write a Julia function with two parameters, word and letter. Okay, with that, we can already get started.
12:23
Let's start with the function keyword, then we have to think of a name for our function. Maybe we can call it letter in string, right? And the function should have two parameters, word and letter.
12:41
Okay, let's not forget about the end keyword. And with that the frame, also known as the header of our function definition is already done. All right, so what should the function do? Suppose that when the function is called a string is supplied as the first argument and a character supplied as the second argument.
13:02
Okay, so string would be supplied for the word parameter and character for the letter parameter. The function should return true if there is a letter in word which is equal to the given letter. Otherwise, the function should return false. To write the function use a loop in every iteration, compare one character of the given word to the given letter, okay.
13:23
So basically, our loop has to look at one letter in word at a time and check if the letter is equal to the given letter. All right, we can do this with a for loop. For instance, so for i in one to length of word.
13:47
So that means that we will have an index i that runs from one to the number that is equal to the length of word. And let's just let's just look what this what this does.
14:02
We can test our function that we have written so far by calling it with some example arguments for instance, for our word, we could use hello. And for the letter, we can provide Oh, maybe if we were done with the function, then
14:22
this should return true now, but of course, we're not done, it will just print some numbers, like we can see here. So now for each of these numbers, we have to retrieve the character from word which
14:42
is at this place in word. And we can do this by using the bracket operator on word. So with this we get in every iteration of our loop here, we retrieve one letter from
15:00
the word, word. And we can now use the equality operator to compare this to the to the argument that is provided for the parameter letter. So just like that.
15:21
Okay. I didn't provide a character, I provided a string which is wrong. Okay, and now it the output makes a lot more sense. We can see that it checks whether Oh, the character Oh, is equal to the first
15:45
character in Hello, which is age, which is obviously not true. So the result of the equality comparison is false, which is then printed out. And this goes on for every of the characters until the last character and for the last
16:01
character, the result is true. All right. But we don't want our function to print all of these results. Instead, we want the function to return true if any character in the word is equal to the given letter. So we can achieve this using an if by using an if branch.
16:24
So basically, what we want to do is to return true if this comparison is true, right. And if not, we want to continue with our search.
16:45
And now the function works. If some character in the word is equal to the given letter, right, it will return it will just return sorry, it will just return true now.
17:05
But if this is not the case, then it will not return anything. And to fix this, we can just add the line return false down here.
17:22
And with this, the function will provide a sensible output in every case. Right. And with that, we're done with task three. For task four, we have to write another Julia function with two parameters word and letter as above this time, we have to count the occurrences of letter in word and return the
17:44
count. Okay, maybe we can just start with what we have written here and see if we can adapt it to the to task four. So, letter and string is obviously a bad name for such a function that counts the occurrences
18:05
of letter in a string. So, why don't we call it count for currencies. Right now it will return true if any letter is equal to the given letter, but we wanted to return a count.
18:21
So, we can achieve this by creating a variable, which always contains the current result. And if we have found a letter that is equal to the given letter, we add one to the variable. And in the end, we just return the variable.
18:45
All right.
19:09
Okay. And it seems to work. Let's try it with another example. Very good.
19:20
Okay, so it seems we're done with task four as well. Task five contains a little reminder about the dot operator that I will not read out in the video. And it asks us to write a function called dot add, which basically implements the dot
19:41
add operator. Okay. Every time you see a sentence like this, write a Julia function, which has two parameters, you can immediately get started, right? Just put the function header, two parameters.
20:01
Yeah, let's just call them A and B maybe. And the end keyword. And with that, we have already written a function. It doesn't do anything yet, but the function is already there. All right. So, what should the function do? Suppose when calling the function, two vectors of numbers with equal length are supplied
20:21
as arguments. Okay, we have called them A and B now. So, write the function such a way that it creates and returns a new vector, which is the sum of the two input vectors. In our case, these are A and B, just like the dot plus operator does. All right. The task says we should create and return a new vector.
20:43
So, why don't we get started with that? New vector is very simple. We just initialize it as empty, right? Assign it to a variable, and in the end, we return the variable that we have created. Now, all that remains is for us to put the values in our vector that we want inside of the vector.
21:10
Right now, the vector will be returned as empty. Maybe we can already demonstrate that once. So, if we provide two arguments, let's say 123 and 456, it will just return an empty
21:31
vector. Very boring, but let's make it more useful. So, what we want to do is our vector, what we want to have in our output vector is
21:46
it should have three elements, right? Because we want to add the two input vectors. And the first element should be the sum of the two elements in the first place of the input
22:01
vectors. So, the sum of one and four. So, the first element should be five. Second element should be seven, right? The sum of five and two. And the third element should then be nine. Okay. And one way we can achieve this is by using a loop that goes through all the indices
22:21
into these vectors. And for every iteration, it adds the two items that are at the respective index. And we can just do this using a for loop and a range expression. So, we use the colon to make the range.
22:51
And this will now go through all the different indexes. And we, for every iteration, we want to put a new sum into our vector called result,
23:05
right? For this, we can use the push function. First argument is always the vector we want to push into. And the second argument is the thing we want to push into the vector.
23:22
Let's just see if this already works by just pushing the index i. And yes, it works. It gives us a vector of one, two, three. So, we can see that i, the first iteration of our loop, i takes the value one. Second iteration, i takes the value two.
23:41
And in the third iteration, i takes the value three. Let me quickly change the example argument here to just make it crystal clear that it has nothing to do with the content of this vector. So, maybe we can go seven, eight, nine here.
24:01
And as you can see, the result is still exactly the same. So, it has nothing to do with the content of the vector, a, just with its length. All right. What we actually want to do in the function is not to put the index into the result vector. Instead, we want to put the sum of the element at the index in both of the input vectors.
24:25
We can calculate this by using the bracket operator on our input vectors. So, a at the index i, that would be seven in the first iteration, eight in the second iteration,
24:42
and nine in the third iteration, right? And to this, we add the element in b at the index i, which will be four in the first iteration, five in the second iteration, and six in the third iteration. And this result, we push into the vector.
25:08
Let's see what we get. Okay, 11, 13, 15. So, 11 is the result of seven plus four. That looks good. 13 is the result of eight plus five. Also makes sense.
25:22
And 15 is the result of nine plus six. Also makes sense. Okay. And with that, we're done with this task. Task six starts with a quick reminder about string slicing. And then the function, the task asks us to write a function, a Julia function called slice,
25:45
which has three parameters, word slice begin and slice end. Okay, let's do that. Function slice, right, and a parameter list, word slice begin, slice end.
26:04
And the end keyword, done. We have written a function, which has all the properties listed in the first sentence. Okay. Suppose that when calling the function, a string is applied as the first argument, and an int64 number is applied as the second and third argument respectively.
26:23
Let's write down such a function call. So, we can actually use the example here.
26:56
Okay, as we can see, our function does absolutely nothing, which is what we also would have expected for a function that has no body.
27:04
All right. Write the function in such a way that it creates and returns a new string, which is the slice of word from slice begin to slice end. All right. The task says creates and returns a new string.
27:24
So, let's start with that. We create a new string, we assign it to the variable called result, and in the end we return the value of the variable result. All right. And let's see what this does.
27:41
Okay, it just gives us the empty string as expected. And the string shouldn't be the empty string. Instead, the task says the string should be the slice of word from slice begin to slice end. All right. And also the task says in your function to not use a for loop,
28:02
a range expression or string slicing, instead use a while loop. Okay. So, let's do that. We use a while loop. We know that a while loop needs some condition and then a body. And what do we want our loop to do?
28:21
Well, the loop should go through word, essentially, and put the characters we want from word, which we want to slice from word. These characters should be put into result one by one.
28:40
That's the basic idea. And we can achieve this by using a variable, which we increase in every iteration of the loop. Let's call this variable i for index. And we want to start at slice begin, right? Because the first character from word that we want is the character at the index slice begin,
29:08
because that's the beginning of the thing we want to slice out of the string. And we want our index to iterate up until slice end,
29:22
because slice end is the index of the last character we want to slice out of word. So, we can achieve this by giving the condition while i is less than or equal slice end. And we must not forget to add one to i in every iteration of our while loop.
29:48
And with this, we have essentially now created the equivalent of a for loop, which we were asked not to use in the task. The equivalent for loop would be for i in slice begin colon slice end, right?
30:09
So, this would be how you do it with a for loop. With this range expression here, slice begin slice end. And this would be exactly the same as the while loop.
30:23
But the task asks us to not use a for loop, so we won't. And now we have to fill the body of the loop. And what we want to do here is to take the character from word at the position i
30:42
and put this into a result. And we can just do that by replacing result with the result of concatenating the character at the index i to the variable called result. The result of this concatenation will then assign to the variable result.
31:05
Alright, so just as simple as using the bracket operator on word here. Maybe this is still a little unclear, so why don't we progress a little slower here.
31:25
Let's see first what our loop actually does. Okay, so we can see in the first iteration of our loop, i has the value 5, right? We print it in line 6. Second iteration value 6 and all the way up to 8.
31:43
So, basically the loop goes from 5 to 8, which is what we provided here. If we provide a 9, it will go up to 9. And we can also have a look at what this expression evaluates to, word bracket operator i.
32:20
As we can see, it evaluates to the fifth character of our input string we call word,
32:30
then to the sixth character, which is just a space, seventh character and eighth character here. Alright, and now we want to basically put all of these into our variable we call result.
32:47
So, we just concatenate the character to result. Yeah, I already explained this. Okay, I have a parenthesis too much here. Alright, and now the resulting string is exactly what we wanted.
33:04
We can write this a little bit more compact if we use an update operator. And with that, we are done with task 6. In task 7, we have to write two Julia functions that are able to detect palindromes.
33:26
And as the task description says, a palindrome is a word that reads the same for what the spec wants. So, we can see that the word madam reads madam when we read it like we normally do read an English word, but when we read it backwards, it also says madam, right?
33:44
And it's the same for race, kind for many other words as well. The task says write a Julia function that receives a string as input and checks if the string is a palindrome. Write the function in an iterative style using a loop. In each iteration of your loop, compare two characters in the string.
34:03
Okay, let's get started. First sentence of the task description is write a Julia function that receives a string as input. So, it's just writing a function. Maybe we can call the function isPalindrome iteratively.
34:31
Now our string parameter here and the end keyword. So, with that, we are already done with the function. And it should check if the string is a palindrome using a loop.
34:45
And we want to compare two characters in each iteration. And, yeah, the idea is to start with the outermost characters of the word. So, for instance, for madam, we would start with the two m's, compare if they are equal, which is the case,
35:02
and then we would progress from the m's of the given word, we would progress towards the middle, right? And if at any point in this progression two of the characters are not equal, we know that the word is not a palindrome.
35:20
And if our comparison places have reached the middle, then we know that the word is in fact a palindrome. All right, so we can do this comparison by using two index variables, and let's call them i and j.
35:42
And the idea is that we use a loop to compare the letter that is at the index i to the letter that is in the index j in the string word. The first indices we want to compare are the outermost indices, so basically one and length of word, right?
36:11
Okay, now we want to use a while loop, and let's worry about the condition, let's worry about that later,
36:23
let's first see what the while loop is supposed to do. What the while loop is supposed to do is to compare the letter at index i to the letter at index j. And if these are equal, we can continue with the comparison, but if they are not equal, we know that we don't have a palindrome.
36:47
So if they are not equal, we know that we can return false. And if the loop has progressed until the end, and we have never returned false, then we know that all of the letter pairs have been equals.
37:07
And this way we know that we have in fact a palindrome at our hand, so we can return true. And now for the condition, we basically have to run the loop as long as i is smaller than j, right?
37:25
We want to stop as soon as i and j are at the same position. So we can write a condition like this, if i is smaller than j.
37:42
Let's think about when this will stop. So what if we have a word that has four letters, for example, then i will be 1 in the first iteration and j will be 4. And then in the second iteration, oh right, we forgot to move our index variables.
38:07
We have to add 1 to them in every iteration, of course. Okay, so first iteration, i will be 1 and j will be 4 if we provide a four-letter word.
38:21
And the second iteration, i will be 2, j will be 3, right? So the condition still holds. And after that iteration, i will be 3, j will be 2. Sorry, we have to subtract from j, not add to it. i will be 3 and j will be 2.
38:42
So that means that the condition will evaluate to false and this way we will leave the loop. Okay, and what about if we have a word that has three letters, for example, then i will be 1, j will be 3. And then the second iteration, i will be 2, j will also be 2.
39:06
And that means that we will not compare the middle letter to itself. But that's also unnecessary because we know that the letter is equal to itself. So the iteration will already stop there.
39:24
Yeah, so i think this should work. Let's test the function that we have written.
39:42
Okay, it returns true for madam, that's good. Let's see, okay, it returns false for sir, which is not a parent, that's also good. And it's always a good practice to test your function with inputs that are edge cases, like the empty string to see if it still works.
40:02
Okay, the empty string is a palindrome, that seems reasonable. And just the letter a or any letter for that matter is also a palindrome. So that seems to work perfectly well. Alright, the task goes on by asking us to write another Julia function that checks if its string parameter is a palindrome.
40:27
But this time, we should write the function in a recursive style. Instead of using a loop, we should have the function call itself and make use of the fact that a string of length three or more is a palindrome,
40:40
if and only if first the two outermost characters are equal, and second the string between them is also a palindrome. Alright, so we can get started by following what the first sentence tells us and write a Julia function.
41:01
Again, we start with the function keyword. We want to have a string parameter, but first we have to choose a name. Let's go with the name as is palindrome recursively. Let's call the parameter word again, why not.
41:20
And that's the end keyword. So we're done with the function header. Now for the body. The task description tells us to make use of the fact that a string of length three or more is a palindrome if and only if the two outermost characters are equal and the string between them is a palindrome.
41:42
Alright, and we already know from the previous task that a string of length zero or one is always a palindrome. And for a string of length two, it's a palindrome if the outermost letters are equal.
42:03
Alright, so we want to write a recursive function. That means we always, always, always need a recursion anchor, which is some case in which the function does not call itself. The recursion anchor is almost always kind of a trivial version of the problem we're trying to solve, right?
42:23
So if you want to decide if a word is a palindrome, then the most trivial instance of this problem is to have a word that has zero letters, right? That's trivial. The word is a palindrome because if you read it from the front, it's the same as if you read it from the back. And the same is true for a word that has only one character.
42:44
And let's check for precisely this condition. So if the length of our word is smaller than two, which means that the length is zero or one, then we want to return true, right? Because then we obviously have a palindrome.
43:06
And what do we do if the length is two or more? Well, if the length is two, we want to compare the outermost characters.
43:22
And if the length is even more than such as three or more, we want to compare the outermost characters and we want to compare the string between them. But we can implement the function in the simplest way if we also compare the string between the two outermost characters for a word that has only two characters.
43:44
Because then we just have the empty string, which will be true. And this way we can implement the function in a concise way. So what we want to check is are the two outermost characters equal, right?
44:03
That's the first part of the condition. So let's write an expression that evaluates to true if the outermost characters are equal. It's as simple as word one equals word end, right?
44:22
Instead of end, we could also write length of word. All right. And now we want to check if the string between them is also a palindrome. And we can do that by just calling the function isPalindrome recursively, right?
44:41
Because this function will tell us if the string between them is a palindrome. And how do we get the string between the first and last character of word? Well, we can slice from two until end minus one of our called word.
45:07
And this is then the result. The result of this end expression here is what we will return in the non-trivial case, which is the case where word has a length of two or more.
45:25
All right. Let's see if our function works. Yeah, let's actually start with the trivial case.
45:40
So for the trivial case, it returns true. Okay. For the other trivial case, also true. Start with a not so trivial problem. It also returns true. Returns true for race car very well. And for so it returns false.
46:02
All right. Let's look more closely at what actually happens under the hood to really get a good feeling for this. And I think one good way to to do this is to just put a print line statement as the first part of our function.
46:21
And we can see that when we put the word sir into the function, then it prints. Then it checks if the length of the smaller than two, which is not the case. So it jumps to this line and then checks. And is the first character of the word, which is as in this case, is this equal
46:41
to the last character of the word, which is our this case, the characters are not equal. Which is why this condition here is evaluates to false. And with that, the entire function just returns false.
47:03
If we change this to R, then we can see that the check is again made and the two Rs are now equal to one another. And then the function calls itself with I as the word. And in this case, we go into the if branch and return true because the length of word is now smaller than two.
47:30
And let's look at a more complicated example, which was race car. And we can see that the function calls itself multiple times.
47:41
First time it checks the outermost Rs, then checks the outermost As, outermost Cs, and then it reaches the trivial case and it returns true. And then this true value is then propagated up the call chain. And in the end, the outermost call returns true.
48:04
OK, and with that, we're done with the palindrome task. In task eight, we are introduced to the Fibonacci sequence, which is just a sequence of numbers. And the current number in the sequence is always equal to the sum of the last two numbers.
48:25
So we can see that the second number in the sequence is one, which is equal to the two preceding numbers zero and one. The third number in the sequence is the sum of one and one, so two and so on. And our task is to write a function that computes the nth value in the Fibonacci sequence.
48:47
So a function with one parameter called n and the function should return the nth value in the Fibonacci sequence. OK, so let's get started by working the function keyword. Choose a name for a function, how about fib iteratively, and then the parameter, let's call that n.
49:09
And the task is to implement the function of a vector to store the sequence and a loop to append new elements to the vector until the nth element is reached. OK, let's, yeah, let's, let's use a vector.
49:27
And we can initialize the vector with the with the trivial start of the Fibonacci sequence, right? And the first two elements of the Fibonacci sequence are zero and one.
49:41
Yeah, so let's just put zero and one. And now we want to have a loop that appends an element to the vector if the vector is not long enough to retrieve the desired nth element from it. We can do this using a while loop.
50:03
And we run the while loop while the length of our sequence is smaller than n, right? If n is one, we want to get the first character from the sequence.
50:20
If n is two, we want to get the second character from the sequence, and so on. What we return in the end is a sequence of n, right? That's, that's kind of the idea. And now we have to make sure that sequence is long enough to make this indexing operation possible.
50:42
And because in Julia, our vectors are indexed with beginning from one, this table is actually not too helpful, which is why I will, I will just quickly edit the table to make it more to make it more useful for our case.
51:02
So with that table, as our as our basically as our example for how the function should behave, we can just immediately implement it in Julia. And in fact, we can try it already with with a with simple arguments for n.
51:29
So if we provide one as our argument, then it will just index into a sequence at the first position, right?
51:42
And the while loop will be never entered because the length of the sequence is two already at the start. And n is one in this case. So, yeah, the while loop is just is just never entered. And we should have zero as our result.
52:01
Let's see. Yep, that works. And it also works with two, but for three, we have to do some more work in our function now, because if we put in three, then the length of the sequence is not sufficient to retrieve the third element
52:20
of the Fibonacci sequence. So how do we compute another value of the Fibonacci sequence? We just add the two previous values, right? So the two previous values in this case are the value at the end of the sequence
52:41
and the value before that. Okay, and this is the value we want to have as the new last element in the sequence. So we push this into the vector sequence.
53:03
Okay, let's execute it with three now. Okay, and it gives us one as s is correct from the definition because the third element in the sequence is one. Let's try with a higher number, maybe the 10th element of the sequence should be 34.
53:26
All right, that works as well. 11th element of the sequence, 55. Okay, why don't we try the 100th element? Okay, that's too large for an N64.
53:42
Maybe let's go with the 50th element. Okay, that's already some very large number, but we can see that the computation works. To make it a little more clear what's happening, maybe we can actually print what the content of sequences after,
54:02
or maybe we just print it inside of the loop. Maybe not with 50, let's go with 10 maybe. Okay, and you can see that after the first iteration of the loop, we have 011 as our vector sequence,
54:22
and then the length of the vector increases with every iteration. And this goes until the vector has the length we need to evaluate the Nth element in the sequence, in this case the 10th element, which is 34, and we just then return.
54:41
All right. The next part of this task is to write the same function but recursively. And yeah, we can already start with the function header. Let's call the function fib recursively.
55:04
And again, we want to test the function. Okay, to write this in a recursive style,
55:24
we have to think about what is a trivial instance of this problem, right? What's the trivial instance of the Fibonacci sequence problem? And the trivial instance is if we are at the beginning of the sequence, right?
55:41
Because if the element of the sequence we want to retrieve is the first element or the second element, then we can just immediately give the result. So the first element would be 0, and the second element would be 1. Okay.
56:04
Let's use this as our recursion anchor. As you know, the recursion anchor is always kind of the trivial instance of the problem. So if N is equal to 1, we return 0.
56:22
And if N is equal to 2, we return 1, right? That's our recursion anchor right there. Again, it's the trivial instance of the problem in which we can give the result without actually using recursion. All right.
56:40
And now for the general case. In the general case, we know that every element in the Fibonacci sequence is just the sum of the two elements preceding it. If we just imagine for a second that we would already have some magic oracle
57:01
that tells us our two preceding elements of the sequence, right? If we have this magic oracle, then we can immediately write an expression for the current element of the sequence, for the nth element. And now let's pretend the magic oracle is called Fib recursively. Then we just have to add the two preceding values in the sequence, right?
57:29
So n minus 1 and n minus 2. This is all we have to do. With that, we have basically implemented our own magic oracle, which tells us the nth element in the Fibonacci sequence.
57:42
And if we call it with 10, it will just give us the result, which is 34. And if we call it with another example, for instance 1, it will give us 0, 2, 1, 4 will give us 2, and so on. All right. So with that, we have implemented the Fibonacci sequence function
58:05
in a recursive style. For task 9, we take the role of the chief baking officer of the local bakery and have to keep track of the bakery's inventory, which we represent in Julia.
58:22
Okay, let's see what we have here. We can see that we have five items in the inventory, flour, sugar, eggs, spices, and printen. And they also have a current stock. So we currently have 50 kilograms of sugar, 5 kilograms of eggs, 1 kilogram of spices, and 20 kilograms of printen.
58:42
And also every item has a price. All right. The task is to write three Julia functions, each representing a transaction of our bakery. And the function should be called buy, bake, and sell respectively. Okay, we can already get started with this information.
59:03
So function buy, function bake, and function sell, right? And each function should have as parameters the current inventory as a vector and the number of items to buy, bake, or sell, okay?
59:24
So inventory, let's call this inf maybe, and then the count, we want to buy, bake, or sell, okay? Furthermore, buy should receive the item to buy as a string parameter, okay?
59:44
So buy should receive the item as a string parameter. Moreover, both buy and sell should receive the current balance of the bakery's bank account as an int64 parameter.
01:00:02
okay so balance perfect and on the other hand okay and they should return the new balance after the transaction and on the other hand bake should be void okay yeah maybe we can we can already put that here so buy should
01:00:28
return the new balance we still have to compute this obviously and bake should be void okay so what's more each function must update the given inventory
01:00:50
appropriately and print a message describing the transaction indicating the new account balance for example given the above initial inventory executing
01:01:01
the following transactions should give the following output okay so if we buy invent with the inventory and the balance six items of sugar okay this tells us in which order our parameters should be so balance should be the second argument of by let's shuffle that around it's balance and then by
01:01:34
receives the item and then the count so again the other way around bake
01:01:47
receives the inventory and the count that looks good and sell receives the inventory the count and then the balance okay that's also that also looks good and now when we execute by we should print a message like bought
01:02:03
six kilograms of sugar for euro six new stock fifty six kilograms new balance euro nine nine four and obviously we also have to update the inventory so let's get started the first part of the message bought six kilograms of sugar and that's just we can just start printing this so bought
01:02:27
then the count right everything is in kilograms so and we can just put a here bought six kilograms of then comes the item right okay item four and now
01:02:48
we have to put in the price and the price is the amount times the price per kilogram and how can we calculate the price well we multiply the
01:03:12
count with the price per kilogram and that we receive from this array price euro per kilogram okay but we have to now we have to check we want to buy
01:03:32
sugar right for instance we want to buy sugar but now we have to check at which index in this array the price for sugar actually is and we know that
01:03:45
from the position of sugar in this in this array or vector here called items okay so we first have to search sugar in items and then use the position at which we have found it as an index into price euro per kilograms okay to
01:04:06
search for the item we can use a loop while while the the place we're
01:04:22
looking is not the place we we're looking for right while this is the case we keep searching and we start at the beginning obviously all right this should do the trick this should increment I which is to say it should
01:04:41
it should add one to I as long as we have not found the correct index yet and then we can use I as the index into the price vector all right let's continue with the message so we already have printed bought six
01:05:01
kilograms of sugar for six euro oh we haven't introduced the unit yet so euro six okay full stop mm-hmm new stock column 56 kilograms hmm okay oh yeah
01:05:37
this is getting a little long maybe we can can stop this here continue with
01:05:48
new stock down here don't forget the space okay new stock okay we still have to calculate that the stock and the new balance new balance euro okay this
01:06:19
should do the trick and we return the new balance that that checks out
01:06:26
okay what's the stock well the stock is obviously the current stock plus the amount we bought so the current stock what's that the current stock is given
01:06:41
in the inventory right so inventory at the index I which is the index we already searched plus count right that's the that's the new stock we must not forget to actually update the inventory as well so instead of the variable
01:07:07
stock maybe we can just use the inventory instead of this assignment statement we can use an update operator so inventory it's changed here and when we can put use this when we print the message later okay and the new
01:07:25
balance is obviously the old balance so balance minus the price of minus the price of the item which we buy which we've already calculated okay
01:07:52
think we're done here with the buy function let's see if it works by plugging in the examples from the from the task description and what do
01:08:14
we have Oh bounce ever attempt to access five element vector at index six hmm that's not good that is not good at all we are in line which line this is
01:08:38
in 93 mmm it says we're in line three but that oh in 93 oh yeah okay
01:08:50
okay so we're in this call and in 92 and 92 is our function and there the error occurs in line four which is this line okay so that means we access
01:09:07
the five element vector in 64 at index six so that means I is six here but I shouldn't be higher than five okay what are we looking for we're looking for the
01:09:23
item sugar which is at index two in the list hmm well oh right I'm looking oh man I'm looking in the price array but I have
01:09:43
to look in the items array yeah it's obviously obviously wrong okay so we tried I tried to search the the array of prices for the item sugar and which is obviously not a member of that so let's see if it works now okay we
01:10:02
forgot to put a new line yes okay that looks that looks more like it what six kilograms of sugar for euro six new stock sixty two kilograms it says 56 is that's because that's because we have already updated the
01:10:22
stock maybe we can pull this down here no that's not that's not the best spot maybe down here okay 56 kilograms seven five sixty two yeah that looks
01:10:42
that looks good okay yeah I think we're done with the with the by function let's have a look at the next function which is called bake bake should print a log message like bake 30 kilograms of print and new inventory 74 kilograms of flour and so on okay so it starts with baked count
01:11:17
kilograms of content and then the new inventory new inventory and then we
01:11:30
print every item in the inventory separated by a comma okay and also we have to update the inventory obviously because we have used we have used all
01:11:43
the ingredients and created new printer okay so let's use a loop to go through the inventory and update all the update all the the counts how much
01:12:08
of the various items are we using we know this from the recipe we know from the recipe that for 10 kilograms of printer we use 5 kilograms flour 3
01:12:20
kilograms sugar 1 kilogram X and 1 kilogram spices and okay we also see from the example that the that the count is actually in in 10 tens of kilograms so if we receive a 3 here we bake 30 kilograms of print and that
01:12:43
means we execute the recipe three times okay so let's represent the recipe in our function as an array we know that the recipe is 5 3 1 1 ok and then
01:13:11
also we the recipe creates Clinton but uses the other ingredients so maybe we can represent this by using a negative amount for the Clinton I guess it would
01:13:24
be minus 10 because we are baking 10 Clinton and we execute the recipe count times so we can just multiply every element in the recipe with count okay
01:13:43
and now we have to update the inventory so inventory at the current index is the old value of the inventory minus the the amount we have used right so
01:14:05
that would be a recipe at the index I we can we can write this with an update operator okay and also we have to append to our log message new
01:14:25
inventory then we want to print the kilograms and we also want to print a comma right so to to receive such a message but not of the last element
01:14:46
so if I is smaller than length of inventory and all right and then at
01:15:00
the end we have to also print the new line let's let's try it with the example from the task description ah okay we forgot a couple of things we
01:15:25
forgot the item that we are that we are saying the new stock of and also we forgot us we forgot to put a space after the comma okay yeah dollar items
01:15:46
not gonna work we have to use items I right because items is our array here yeah items that is the one okay that looks better but we still forgot a space
01:16:04
here okay now we have we have baked so much print that the inventory is running negative let's let's get rid of that yeah 70 47 kilograms 37
01:16:22
kilograms yeah that looks just like in the example okay looks like we are done with the bake function and now for the cell function the cell function has a message that looks like this let's just quickly copy that down here okay
01:16:54
so we start with saying sold count kilogram of winter for euro price
01:17:17
which is the thing we have to calculate and remaining stock
01:17:39
kilogram new balance euro new balance all right and we know that we can
01:18:09
calculate the new balance as balance minus price right okay and the price we
01:18:25
can calculate this as the count that we're selling times the price per unit what's the price per unit of Clinton it's given in this array so it's the last element of the array okay that's our price and the new
01:18:51
stock that would be an inventory and minus the amount we sold so count we
01:19:05
actually want to update this as well so yeah let's let's just use inventory and instead of stock it's exactly the same all right that should work no
01:19:21
missing comma or parenthesis an argument list where up okay it's here all righty let's check sorry oh oh the example is still wrong we're actually
01:19:47
selling one kilogram and then two okay yeah the example is wrong sorry about that will be corrected in the version of the exercise sheet that you
01:20:01
receive but let's execute it sold one for 20 euros remaining stock 69 kilograms new balance 794 euros yeah no it's not exactly the
01:20:21
same as in the example why is it not why is the example 100 euros more oh right I subtracted from the balance but I have to add to the balance because we're selling something we're making money not spending money right
01:20:44
okay but yeah that was kind of embarrassing it should work now yay okay 834 that that checks out all right 834 874 yeah like the example so
01:21:05
I hope I'm correct I hope I'm correct this time okay that was it for the last task as always if any questions remain about the contents of the fourth exercise sheet please reach out to us via Moodle or you can also write us an
01:21:25
email and we will be happy to answer your questions and in any case thank you for listening and and looking to the looking at the video and have a nice
01:21:41
time
Recommendations
Series of 22 media