 recording so welcome everyone also on Moodle and on YouTube do you know that time yes so it's gonna be the 21st at 2 p.m. so it's more or less the standard lecture time so 2 in the afternoon the exam will last I think 90 minutes but we'll have to see how it goes and how many questions I can come up with but should be fine so just block it in your agenda 21st of July 2 p.m. our course exam will you do it online still yeah I have to do it online I'm not allowed to do it in person still so I am not from the Humboldt Uni so do I need to register for the exam on Agnes too well if you're not from the Humboldt you can't register via Agnes I think you should mail the Prüfungsbüro to be on the list because that's normally how I get it so I get a printed or I get a list where everyone has been like registered and then they add the people from external universities by hand so that's just handwritten on the thing so yeah just mail the Prüfungsbüro and they will just put you on the list might be that you get a hard time trying to get into contact with them it also took me like two weeks to set up the exam date but at least Jaja Potts is working so you could if you're if you get a like we're not in the office currently email back tokoforol then just send me an email and I can send you a internal email of one of the persons working there who is pretty quick and responding and not on holiday currently so alright so what about PhD students PhD students should be able to register via Agnes because you're a student you should be able to log into Agnes I actually got my tone code so normally I could not log into Agnes because I don't I don't have a student account but there's something new going on this semester where also teachers can register for Agnes and be able to log in and change some things but yeah I think you should still be able to register if you're trying to register on Agnes and you don't see it then send me an email and also send the Prüfungsbüro an email that you want to register but as a PhD student you theoretically don't need to do the exam although I do like people doing the exam because then I have an idea that I'm not giving you points for free so if you if you're a PhD student and you don't want to do the exam because you don't need the credits but you want to just have an attendant shine that is also a possibility so some people for some people in attendance letter is enough and then the only thing that I do is just look through my administration who was there every lecture and if you attend it I think 10 out of 13 or 10 10 out of 14 lectures then you just get an attendant shine which for some studies is enough but that also depends on the study that you're following and if an attendance letter is good enough but just do the exam it's not that hard and it's fun because it's always after my birthday so there will be a birthday question which is a drawing question so that's one of the things that I can already tell you guys practice your drawing skills because drawing is I think an important part of biology because by drawing things it kind of forces you to look at objects and to see differences sometimes you have to apply for a ton list as a PhD student is just to click in Achnes also yeah yeah so I got my ton list as well because I'm now registered as a lecturer and as a lecturer I also need tons to change stuff but I haven't logged in yet so I will log in this week and then let you know next week because it might be that I actually that I might be able to register you guys on the exam as well so have for example Tokoforil being from an external university it might be that I can put you on the list but I have to check that but in theory if you want to do the exam then you can just send an email to the pre-functs bureau and then they should fix that for you guys so alright any more questions about the exam I don't have a date for the re-exam yet but I will start mailing again to get a date for the re-exam as well but I'm hoping that we don't have to do a re-exam because I'm hoping that everyone will just pass the first time around which should be perfectly possible that I only had like so I've been doing the course now for seven years well how one can hope how do you mean one can hope like I'm making the question so it's up to me if everyone passes or not right but so far from the seven years that I've been doing it only two out of seven years I had to give a re-exam and generally that is someone who just failed to reach the 51% so then they have like half of the questions or I mean it is difficult no the exam is not that difficult at least like I never think it's difficult and I've like it's a master course but or masters I speech the course but I have better students attending in the past who also were able to pass it so the questions are not too hard and of course in the last lecture before the exam I will also have some example questions so that you guys know how I ask my questions so that you don't get confused by these things and generally there's only like one or two trick questions in there like they're very straightforward questions like just knowledge questions about our and then there's one or two trick questions and there's always a birthday question so that you can make a nice drawing impress me with your drawing skills and then that's going to get you some extra points on the exam so if there's not any other questions and I will take a sip of water because it's like sweltering hot here and then we will start with the assignments question to you guys that everyone do the assignments since you now at two weeks to do them that should have been more than enough time to get at least through the first couple of them I know they were hard the assignments this time were really hard and that is because making functions is one of the hardest things to do in our so I would say that in order of like difficulty the the easier things are things like the type system right is this a character and making a vector and then have when you get to making matrices it becomes a little bit harder then after that you have like the four and the while loops which generally are difficult but once you practice them a couple of times and kind of get the feeling or and the fingers bits and good food for it then that's that's perfectly fine but then functions are something that is always really hard and if I look at for example the subreddit for are then there's always questions about people that wrote a function that doesn't do exactly what they wanted to sort like writing functions is kind of the hardest part and then of course writing recursive functions is even harder so but I just wanted to show you guys that there is and that you can use recursion to make really really nice looking nice looking functions so if you did the assignments just throw in chat how far you came like I'm not gonna be mad if you only did the first two although the one that I really like is actually one of the last ones so question number eight that's one of my favorite questions because it's it brings out the create creativity in people so you can be really creative for drawing the tree and making it look really pretty oh it's so hot so hot I hope you guys are watching on a phone and just sitting near a lake somewhere because that's what I would do anyway let's start so question number one stream will start soon no we already started so let me switch this one to the first one so this is what we will do today but we will first do the assignments so these are my answers to the assignments so assignment number one was from project Euler if you if you want to program something and you don't have something that you think oh I want to program this right then project Euler has some really really nifty assignments which go from like relatively easy to really really hard and you can just work through them and it will help you check because you can check the answers right so it's a question and then you can fill in your answer and then it will tell you well this is the right answer so you did everything correctly so the first three assignments are just taken from project Euler because they have some really good assignments so the first question is if we list all the natural numbers below ten that are multiples of three or five we get three five six and nine the sum of these is multiples is 23 right so and then the question was find the sum of all multiples of three or five below a thousand so had the first thing that you want to do is make a vector like I did here and I call this vector x3 and this will hold all the numbers which are well multiples of three from three to nine hundred and ninety nine right because those are the numbers below so I just say make a sequence from three to nine hundred and ninety nine and step by three so this is six three six nine twelve and so forth then I do the same thing for x5 so I'm making a vector called x5 or I say make a sequence from five to nine hundred and ninety nine step by five every time and then there's two ways to then do the sum of these and here you have to realize that if you have these two vectors then some of these numbers will be will be double right because 15 is divisible by three and but it's also divisible by five so you have to get rid of the duplicates so there's two ways and there's a hard way and there's an easy way so let's first look at the easy way so the thing that I'm going to do is I'm just going to combine these two vectors into one big vector so had this will have first all of the multiples of three and then it will have all of the multiples of five in there and then I'm just going to combine these two using the C function and then I'm just going to ask for the unique values and the unique function is a built-in function in R which will get rid of all the duplicate values so if you have 15 in there twice then it will only have 15 in there once after you did unique and then you just take the sum and that this will give you the sum of all the values from 0 to 999 which are divisible by three or by five you can also do it yourself right so instead of using the unique function you can also do a a minus right so throwing away some numbers so the thing that I'm doing here is in from x3 I'm going to remove all of the numbers which are also in x5 right because by removing the duplicates or the numbers from x3 that are also in x5 I am left with a set of numbers which doesn't contain any duplicates so what I'm going to do is say well which of the x3 numbers are located in x5 and then remove them so minus remove them from x3 right so to to unpack this a little bit because this is the kind of core of it so I'm going to say well let's do a which so which numbers of x3 are in x5 and then remove them from x3 so I'm just going to index x3 by using the index brackets and then saying minus which x3 and x5 and that throws them away and then I'm going to combine this together with x5 and now because there are no duplicates right because every number from x5 which was also an x3 is not there anymore I can then just combine them and then sum them up all right so let's go to R and throw it in and see what the answer is and of course these should both give the exact same answer so we can see that the sum of all of these numbers which are divisible by 3 or by 5 is 233 168 and if we use the unique function we get the exact same answer so that means that we did we did the correct thing all right so the next question that's the wrong window so the next question is a little bit harder but I like this a lot because I love the Fibonacci sequence and I love a lot of things about like sequences that are repeating and the Fibonacci sequence comes back in a lot of things related to biology but also to genetics have because the Fibonacci sequence is so each new term in the Fibonacci sequence is generated by adding the previous two terms by starting with 1 and 2 the first 10 terms will be right so I don't have this window here so I can't show you the assignments but I'm guessing that you have the assignments there right so the Fibonacci sequence for the first 10 terms goes 1 2 and then the next number is the sum of the first two so it's 1 plus 2 which means 3 and then the next number is then 3 plus 2 so that's 5 and then the next number is of course 5 plus 3 which is 8 and then the next number is 13 which is then 8 plus 5 and so forth right so the number that you do is so the next number in the row you can find by taking the previous two numbers and adding them together so by considering the terms in the Fibonacci sequence whose values do not exceed a million find the sum of the even value terms right so this is a this is a very layered question right because you have to read the question very carefully so and the first thing that we need is to be able to generate Fibonacci numbers right and after we are able to generate Fibonacci numbers we want to generate Fibonacci numbers which are below a million and we then want to do a sum so we want to add every number together when the number is even and not the odd number so we're leaving out the odd numbers so the first part of course is to find a is to find a number head so to find a function which calculates our Fibonacci numbers unfortunately because of recursion we can write this version or we can write this function very easily right because we have a Fibonacci sequence and the Fibonacci sequence has two initializers right because there are two base cases to the recursion and that is if I ask you for the first Fibonacci number you know that the first Fibonacci number is one right that's that's in the assignment the second Fibonacci number is two because that's in the assignment so these are our two very base cases so when I'm asking so I'm creating a function in called x right and x is the nth Fibonacci number that we want to get so if x is one then we want to have the first Fibonacci number so we just return one when x equals two we want to have the second Fibonacci number so we just return two and then if we want to have Fibonacci number three then what do we do so Fibonacci number three we can calculate Fibonacci number three by taking the Fibonacci number of two so x minus one so three minus one plus the Fibonacci number of three minus two and this is the whole function so you see here that the recursion very neatly describes the structure of the Fibonacci sequence right if I want to have the Fibonacci of x take the Fibonacci of the of the previous one so the previous x so x minus one and the one before that which is x minus two all right so this is my function to compute Fibonacci numbers and then of course I need to test it because I want to make sure that like I throw in the first six Fibonacci numbers just to make sure that the numbers that come out of the function match the numbers that are in the assignment so let's load this function in R and just generate the first six Fibonacci numbers and see if this is what we kind of expect and have what it if it is the same as in the assignment so we go to R we take our Fibonacci so the Fibonacci number of one so the first Fibonacci number is one the second one is two the third one is three and then the fourth one is fifth five the fifth one is eight and the sixth one is 13 and of course we could check the tenth one as well so we can just say Fibonacci of ten and then of course the tenth one is 89 and then if we look at the assignments then this matches up with the list that we saw in the assignment so that's the function that we want to make and I hope that everyone was able to make this function because it is it takes a little bit of thinking work but you can see here that the way that you write it down is very close to the mathematical definition of Fibonacci right because the next Fibonacci number is made by summing up the previous two so that's x minus one and x minus two and of course that's why we have two base cases as well so what's someone able to come up with this or did we all just hang out in the sun and didn't do the assignment but like I'm not like this is hard this is really really hard so hey this is only question two so the questions will become a little bit harder but for this you have to be able to understand the Fibonacci sequence hey you have to recognize that there is a perfect recursion here right because the third number is the sum of the second and the first and the fifth number is the sum of the fourth number and the third number and so this recurses always back to the first two numbers so that's the first part right so to have a function which generates Fibonacci numbers for you then we can look at my answers and then yeah yeah I'm already recording so at least I hope so yeah no I'm recording 19 minutes now so this this this is this is getting recorded yeah we are we are live and recording like for the people in YouTube right you know that so then the second part of the assignment right so the second part of the assignment is to generate all of the Fibonacci numbers which are smaller than a million and then if they are even we want to sum them up right so so we want to keep track of for example the so we want to sum up all the even Fibonacci numbers while they are smaller than a million so I have this this big big big piece of code for this and this is just the way that it is because in this case we don't know when the Fibonacci numbers will be above a million so we can't use a for loop because we have no idea where the boundaries are of our system right so and it's not that we can go from one to the number of rows in a certain column because we have no idea when the first Fibonacci number that we that we calculate will be higher than a million so we have to do a while loop and doing a while loop is harder than doing a for loop because we have to keep track of our own index right so that's the first thing that I do so I define x so x is our index counter so x means the the and so the nth Fibonacci number right then I define something which is called Phypsum filthy casual thank you for following I'm so sorry I I didn't put the desktop audio on so you missed the sound effect like let's do a sound effect as well so alright has so we take another variable to keep track of our sum so these this will contain all of the numbers that we have summed up so far right so the first thing that we are going to do is or the besides that so we have our nth Fibonacci number which acts is keeping track of then we have the Phypsum which is the sum of all the even Fibonacci numbers and then we of course need to calculate the Fibonacci number so I'm just going to say Phybex because we need to test if this number is even or if it's odd right so I'm just going to calculate it and store it before we start the while loop because I know the Fibonacci number of one is one so I could have just initialized it by one doing it like this right but I think it's cleaner to do is just say Phybex because then we're using the nth Fibonacci number variable that we declared right so we need three variables to kind of keep track of the state where we currently are so then what are we going to do so we're going to say while the Fibonacci number variable is smaller than a million what am I going to do well I'm going to check if the Fibonacci number is even right and we already saw this in like one of the first assignments so to check if a number of E number is even we can look at the remainder after dividing it by two right so if I divide a number by two using Euclidean division if there is no remainder that means that the number is divisible by two without any remainder which means that the number is even if I wanted to sum all of the odd numbers so not the even numbers then I would have just said one right because if a Euclidean divide a number by two and there is a remainder then the remainder is always one of course and then it means that the number is odd right because three Euclidean division by by two is one remaining but in this case we want to sum up the even numbers so we just test if the Fibonacci number is divisible or Euclidean divisible by two without having any remainder and if this is the case then the number is even so if the number is even what do we do well I'm going to print the X right because I might be interested to kind of keep track of the algorithm while it's running just to make sure that they don't end up in an infinite loop and but I'm going to say well X I'm going to print so the nth Fibonacci number then I'm doing a space and then I'm printing the Fibonacci number that I just calculated and then I do a new line of course because I now know that the number is even what I can do is I can take the number and add it to the Fibonacci sum that we are keeping Mustafa Mustafa Kang thank you for following so I'm just going to add the Fibonacci number to the Fibonacci sum and then storing it in the Fibonacci sum so that is that is kind of the way that we are doing it right and this is very common this is a very common paradigm to do in our is to define a variable which is keeping track of our running some of course no matter if the number is even or odd I have to go to the next Fibonacci number so I'm going to increase X by one so go from the first one to the second one and then of course I'm going to just say well I'm now having to update the Fibonacci number as well so I'm going to call our function called Fib with the new number and then I'm going to update the Fibonacci number and this will go through all of them and it will stop when the Fibonacci number is not smaller than a million and then of course at the end so when we are done right then we have summed everything up and then I want to know what the sum is of all of these even Fibonacci numbers and then I just type VIP sum in R because that is the thing that is holding our summation so that's it big piece of code very difficult because we have the three variables that we need to keep track of so X is the index the nth Fibonacci number VIP sum is keeping our sum and VIP num is keeping our current Fibonacci number so let's run this in R it will take a little bit of time it not not that much but it will take a little bit of time because it has to calculate Fibonacci numbers using recursion and although recursion is fast it does have to go through all of them so let's do that so we see that the second Fibonacci number is an even number it's two we knew that already the fifth Fibonacci number is also even because it's eight the eighth Fibonacci number is also even and then we end up with the last one the 29th Fibonacci number which is 832,040 and this is also an even number right so and if we add all of these numbers together then we get the value of 1,089,154 so that was the answer is this clear are there any questions about how you should calculate it did anyone calculate it in a different way did anyone make a Fibonacci function which doesn't use recursion because there are options to do that but they are generally more complex because you have to keep track of state yourself right because now the nice thing is is that the number goes into the stack right by calling the function with a number lower and you get this automatic expansion in a way but you can do it with a for loop or with a while loop as well so but this is kind of the way that I did it so if you have any suggestions on how you would have want to do it differently then let me know and we can we can talk about the other ways of answering it all right number three so number three is one of my favorite because I love pollen drones and I don't just love pollen drones but they are actually very important in genetics because all of the or most of the things that define if DNA is being transcribed or if a protein binds there that is generally based on a polynomial sequence and that is that because a lot of proteins they come in two right so you have two times two similar proteins which bind each other and then they bind to the DNA but because they're two similar proteins you generally see that the sequence that they bind is for example TCA ACT because the two proteins come together and then bind the DNA so finding pollen drones is something which is is is done a lot in biology so there's a lot of very smart algorithms to find pollen drones as quickly as possible but of course a pollen drone is just a number or is a is a is a word that when reading from the front to the back is the same as when reading it from the back to the front so a question to anyone who's in chat you can throw in your favorite pollen drone or the longest pollen drone that you know and I will participate so the longest pollen drone that I or the pollen drone that I like the most is of course this one which if you read it from the front it is the same when you read it from the back so how do we check in our if a number is a pollen drone well again there are multiple ways of doing this one of them is just googling right and saying give me a package which allows me to test if something is a pollen drone which is kind of okay but it's kind of cheaty right because we do want to write these things ourselves but this is how I did it so I make a function again because him making functions is the way to make code which you can reuse later on right in theory once I've wrote the Fibonacci function then from now on every time in the future in like five years time if I need Fibonacci numbers I can just use this little piece of code again right so I don't have to rewrite it I can just store it in a library in a package and then next time that I need it I can just call the function and it will just be there so I do the same thing for pollen drone so I first write a function to check if something is a pollen drone right so I call this function is pollen drone so this will tell me if a number is or a number or a character string is a pollen drone so it is of course a function which takes a variable as input so the variable input I call my SDR for my string so or input string so what I do is of course the easiest way is just to take the string and to revert it right to just flip it around and we already learned that there is a ref function in R which can reverse a vector but of course a string is not a vector so the first thing that we need to do and of course I'm going to read this from the inside out again is first thing that we're going to do is the the input string I'm going to split it and I'm going to split it every character and that is what this means so I'm going to split it using an empty string so that means that if I have a vector well we can just do that in R right so in R I can show you how this works so if I have for example a vector like something like this right so my SDR and if I now do a string split on this and I split it by nothing then you see that it will now create a vector which has a length of 4 which is the individual letters and that is what you can do by using this kind of empty string so splitting something by an empty string means that it just takes the letters apart from each other the thing is is that string split doesn't know how many things you're going to input right so that's why it gives you back a list and in this case it's a list of length one because I only input it one string but of course I could have done multiple strings right so I could have done two strings together something like this right and now if I would do a string split then it would give me back a list with two on the first it will split the first one and on the second it will split the second one so that is why when I go to notepad plus plus I do the string split of my string using nothing and then I always take the first element because my is Polyndrome function will only test the first element in or will only test a single string value right and even though I could give it a list of strings I don't want to deal with that I just want to do the first one right so if I input a vector which has two strings in there I'm just going to check the first one and not the second one and so I could write that in the function or in the in the in the comment here but so what I do is I take the first element and then I just reverse it so now I take the character vector and I just flip the character vector so instead of reading it from front to back I'm going to read it from back to front so just reversing it then of course I want to compare this back to the original input so the thing that I have to do then is put the things together again because I just took every letter apart to be able to use the reverse function but now I have to kind of glue them all together again so gluing them all together is you can do using the paste function so I'm going to say paste the inverted string or the reversed string that I was the input and then collapse them with nothing right so and what this will do it will just glue the whole vector together into a single string and then I call this ref my string so this is the reverse of my string and then of course when something is a pollen drum then of course the reverse string should be equal to the input clear because that that I that I take it apart into individual letters I flip all of the letters around and then I paste them back together having a single string and then compare this new string the reverse to the original and if the reverse matches the original then something is a pollen drum and again I test it right because I then call the function using something that I know is a pollen drum and something which I know is not a pollen drum alright so let's go to our that's show you guys that this really works and then of course we can call it with a couple of other pollen drums unfortunately no one has put their favorite pollen drum in chat so I will just use my favorite pollen drum so this is of course pollen drum so and there's many many different pollen drums but pollen drums come back everywhere in biology and in genetics so it's important to be able to have like a little function like this I know that that's your favorite it's also my favorite my moderator wasn't paying attention to what I said in chat but doesn't matter but my favorite pollen drum also her favorite pollen drum so it works so you can see that indeed it's a pollen drum and we could test a couple more to make sure that it had functions for all kinds of different input of course if you input a vector right which is of length to it will only use the first one so if I would do something like this then it should stills it it should say true and false but of course the second one is not to be trusted because of my function I just take the first one so that's don't be sorry that you're late we're happy that you're here so okay so now we have a function that checks pollen drums and now we can start with the real assignment right because the real assignment was something that that is that is overly complex so but a pollen drumic number reads the same both ways the largest pollen drum made from the product so the multiplication of two did two two digit numbers is nine hundred nine thousand and nine and because if I multiply 91 with 999 then I get nine thousand and nine as the as the result and now the question here is find the largest pollen drum made from the product of two three digit numbers so do the same thing but now for two digit numbers do it now for three digit numbers so now again I have to start remembering a whole bunch of things so I'm going to have again three variables which will just help me to kind of or help the the R function or the R thing to remember some of the things that I have so I have X I which will be the first three digit number right then I have Y I which is the second three digit number and then I have something called big so far and this is the biggest pollen drum yet right because I'm just going to start at the bottom and I'm just going to brute force my way through so I'm going to first say a hundred times a hundred is the output of that a pollen drum then I'm going to do a hundred times a hundred and one is that a pollen drum then I'm going to do a hundred times a hundred and two is that a pollen drum right so I'm just going to test everything so I need to remember the biggest pollen drum that I have seen so far so the big so far is the biggest pollen drum that I've seen so far and then we're just going to start looping so I'm going to say four X in 100 to 999 right because those are all three digit numbers and then I'm going to say four Y so that's the second number in X to 999 and why am I using X here and not just a hundred because I could have just done a hundred because of course a hundred times a hundred and one is the same as a hundred and one times a hundred right so when I have calculated a hundred times a hundred and one I don't have to do the opposite right so and here I'm doing so I'm using the first number so I'm doing a hundred times a hundred and one a hundred times a hundred and two all the way up to 999 but then when I do a hundred and one then I'm not doing a hundred and one times a hundred I'm doing a hundred and one times a hundred and one because that's where I start then so that allows me to to not duplicate something that I already did but both both approaches are perfectly valid so we can just say four Y in 100 to 999 but this will of course calculate duplicates right so a hundred times a hundred and one but also a hundred one times a hundred so to prevent that we can say that X or that Y starts only at X so just to kind of half the work that we are going to do and then of course the thing that I'm going to do is just say well I'm going to multiply X and Y and then make a character out of it and then use my function so if something is a palindrome and the multiplication is larger than the largest number that I have seen so far then this is my new top candidate right and then I just remember it so I'm saying big so far is X times Y I'm remembering X in XY and I'm remembering Y in YI and then I'm just going to cut it because I just want to keep track of the algorithm so I'm just wanting to see kind of how it runs through all of the numbers and comes up with the biggest palindrome so far and of course then I'm just going to close all of the brackets because that's the only thing that I have to do and then in the end I can print big so far which is of course the largest palindrome made out of three digit numbers I'm going to print XY and YY to know which two numbers I have to multiply together to get the biggest palindrome so far all right so let's run this because this will this will run a little bit because it's a brute force and we're going to do a couple of thousand combinations and perhaps even a million combinations so it will take a little bit of time but the nice thing is is that because of the cut statement we will have continuous feedback of the algorithm what it is doing and how far it is which is nice right all right so let's just run it so we see that when we are at 101 we are actually calculating a lot of palindromes and we can see that the number is actually growing because I'm only printing a new combination when the combination is bigger so after running the algorithm we know that the biggest palindrome that you can make of two three digit numbers is 906609 which is indeed a palindrome and we can make this number by saying I'm taking 913 and I'm multiplying that by 993 it's not magic it's just mathematics with a little bit of like logical sense in there but this does this make sense to you guys or do you say like I don't understand anything of this like what he's talking now is complete Chinese but the thing is is that in in programming generally you use variables right to remember things for you so I up front I'm just going to say okay so if I would do this by hand I would do a hundred times a hundred and then I would check then the output to see if it's a palindrome then I do a hundred times 101 see if it's palindrome if yes then I would write the number down and I would just do all of the multiplications until I reach like the largest one and so defining some variables up front to kind of keep track of the things that you want to keep track of then you just brute force through the entire problem space and here I'm having our problem space but I could have said just go from a hundred as well and then check if it is a palindrome if it is bigger than the biggest thing that I've seen so far then I'm just going to remember it and I'm just going to give myself some feedback so when I'm running the algorithm that I'm not and that I'm not stuck that it's not an infinite loop and that's one of these important things if you write like while loops especially make sure that you always put in a cut statement because if it starts hitting an infinite loop then of course you can see that because you get the same output over and over again in your screen all right so these were relatively hard but these are really good things to kind of practice on for like half a like half a weekend right if you are not doing anything on on Saturday and it's like sweltering hot outside and hey you close all of the curtains and you just sit inside and have your Dyson air blade or some other like fancy machine that cools you down and just sit behind the computer and do something that kind of relaxes your mind and think about and try to make try to come up with an algorithm like this Miko yeah you want to have your Miko fan so I'm just going to promote your Miko fan as well by a Miko fan they actually are the number one in the German what's it called the German thing that the guys that test all of the different projects and they're not that expensive either and with like having 36 degrees outside it's nice to have a fan I wish I would have a fan in my office but unfortunately I do not okay so if this is clear right so make a little function and then use the function to kind of calculate what you want and then hit answer your question ah the Stichtung waren test or a constant ice cream supply I hope that you have a constant ice cream supply general good luck I really really hope that you do that if I would be you guys I would be sitting in a park underneath a tree with like a fan in front of me and like a laptop on my lap and just watching it like that unfortunately again alright so the next question was a relatively simple question because this one was already given or half of it was lemonade lemonade that's a good thing I just have water but at least I have enough water to get me through this three hour of lecture so but then the next question question number four was to make the countdown function also work for counting up so and in this case hip it I think it's relatively simple if you look at the assigned or at the lecture because in the lecture we we started from a hundred and then counted down to two one but in this case we also want to count up so someone fills in a negative number had them based on the number we want to then go up to zero and then once once we're at zero then everything is fine and we just finish so again hip because it's a recursive function we want to call the function itself but first we define our function as a function called countdown which takes a number and X is our number that we are going to start off with so if X is zero then we are finished so this is our base case of the recursion right and X will be our recursion invariant so the base case of our recursion is when we are at zero then we are finished if X is larger than zero then we are going to say cut the count with the current value and then we're going to call the same function again but now we make X one smaller right because the number was higher than zero so to go to zero we have to subtract one of course if X the second case is that X is smaller than zero so if X is smaller than zero what are we going to do well we're going to cut the number just like we did before and then we're going to say well call the countdown function now with X plus one and of course we always wanted to do the count right so we could have here have a slightly different structure but had to kind of because this is a duplicate line so duplicate lines of code are always highly suspicious because generally you can work them out of the if statement but we're not going to do that we're just gonna be like lazy and just gonna do forward programming so we're gonna say no if X is larger than zero I'm going to print the number and I'm calling going to call the same function again but now the number is with the number one smaller and if X is lower than zero I'm going to do the same thing so print the number but now I'm going to call the countdown function with a number which is one higher because we want to count up to zero or we want to go down to zero all right and that's it so let's count down from a hundred and then count down or kind of count up from a hundred or minus a hundred which is a little bit confusing so of course the first one will just count down and the other one will just count up and every time that we hit zero it will say countdown finished so those that was kind of the easiest way of doing this and very similar to what we saw in the in the lecture right because in the lecture we already had the countdown function counting down from a hundred to zero but it would error out on negative numbers and in this case we don't want an error on negative numbers but on negative numbers we want to just go the other way all right so question number five so question number five is interesting because there are very very different ways of answering it and in the last couple of years I have had like a couple of different versions that people have submitted for answer to number five but the way that I first did it is just the lazy function right so the question was let me read the question for you guys so number five is write your own version of L apply make sure to check that the input is a list and throw an error using the stop function if it is not a list the function signature should look like and then this is the function signature which we see here so that was a given and then you should implement the function so the easiest way to implement the L apply function is just to call the L apply function right so I'm going to say create a function called my L apply which is a function that takes x as input then it takes the function that I want to apply to each element of x and then I just want to do dot dot dot right because people might give more parameters of course there was this one sentence which says that if the input is not a list throw an error so that's what I'm going to do so if not is list x stop x must be a list right so this is my my own kind of addition and then because I'm going to be very lazy I'm just going to return the L apply of x of the function using the dots so I'm just going to call the L apply function right which is kind of cheaty but it's it's a very lazy way of doing it and it actually works so that's what I kind of thought would be okay and I would be okay with that right like like being smart and just using something which is already in the standard library is not bad right you don't have to program everything yourself then there was actually someone called Marlon in one of the earlier lectures who came up with this version so I'm just going to show you I'm not going to explain to you how it works but it's the same thing right because he just says if is list x is false right which is also a valid way you don't have to use the the not command you can also say if is list x is false stop this is not a list and then what he did was kind of sneaky because he kind of used the function so his version only works for functions which which do not which where the function actually supports a list as input so he just says return call the function with acts and then do the dot dot dot parameters inside of the function so this works for things like some and for mean and these kinds of things so pretty smart function as well because just calling the function and assuming that the function supports a list is also a valid method of course this doesn't work for functions that don't support lists as input of course there's also the version that I wanted you guys to write more or less because this is the non-chidi version right because you're not using the L apply function so I'm just going to use the signature as given so I'm going to say my L apply is a function which takes a list called acts it then takes a function that needs to be applied to each element of the list and then we have dot dot dot and dot dot dot here is the parameters to the function so what do we do well if not is a list acts stop right so that doesn't change we have to first test if acts is a list and then I'm going to do say well I'm now going to make an empty list right so I'm going to make something called res which is the result so result will will will store for each element the function apply to the element of acts right so and the thing is is that in our weirdness making a list requires you to use the vector function which I never understood but it's just the way that it is so if I want to make an empty list which I'm going to do I'm going to have to say well give me a vector which is a list type and then make space for this number of elements so this number of elements is the length of acts right because if the input of acts is a list length 20 then I need to have the result also a list length 20 if the input is 1500 long then the result also should be 1500 long and then I'm just going to go through acts right so I'm going to say for I which is the index in one to the length of acts what am I going to do well I'm going to store in the I result right so in the end result I'm going to store what am I going to store while I'm going to store the function called on the I element of acts and then I'm going to use the dot dot dot so the periodic parameters are going to be given to this function and because I'm using a for loop this will just go through each of the elements of the input apply the function to it and then store the result in in rest and then of course since I'm inside of a function I need to return something so of course I'm going to return the result so that's also a way to do it so hey you can just be lazy and call the built-in L apply function you can be like Marlin and just say well no I assume that my function that I'm calling as supports list or I'm going to use a for loop and be very very explicit about it and just do one by one so that's the way that I did it okay so let's let's run this version in R so to just show you guys that it does the exact same thing as the built-in L apply function of course the built-in L apply function has some additional advantages of being like memory efficient and not using a for loop but yes so this is what is going to happen so when I use the real L apply function which is built into R so I take a list of 1 2 3 4 5 6 and I'm going to apply the sum function to it which going not going to do anything right because the sum of 1 is 1 the sum of 2 is 2 the sum of 3 is 3 right so in the end I'm just going to get back the numbers that I input it and the same thing for my L apply function I am going to give it the list of 1 2 3 4 5 6 and I'm going to apply the sum function to each of these elements and of course I'm just summing up a single number so the number will be the same as the input but at least it works and you can see that the output of the my implemented version is the same as the L apply function I did like the cheaty version very much when someone came up with it just using like I when I made the question I wasn't thinking about someone just using the L apply function but sometimes it's fun to see people kind of looking at your questions and then doing their best to kind of exploit it in a way so that they don't have to do too much thinking but it's very creative and I like creative solutions to two answers alright question number six so this is just to show you how this very attic argument thing works right so very attic arguments are very very powerful because it allows you to have as many input parameters into your function as you want and you can actually do things with it so how does this function work well I'm calling I'm creating a new function called my dots which is a function which takes a variable number of arguments as input so what I mean what is it going to do well it's going to take these variable arguments and put them in a list and I'm going to call this params so these are the input parameters to the function and then I'm going to go through all of the input parameters and I'm going to write them out to the screen so I'm just going to say for I in one to the length of the parameters what do I'm going to do what I'm going to cut I'm going to take the names of the parameters I Mali Lenka thank you for following thank you thank you thank you the sound not too loud though for following I hope not but for me it's not but I can't hear what you guys are hearing so it might blow up your speaker but I think it's okay I didn't change it too much from last time but I had an update so or the OBS so the capturing software that I'm using had updates so it might be that it just blew up the sound again but for I in one to the length of the parameters I'm just going to say cut take the names of the parameters so the I th one and then say is parameters I right and here the thing is is that the names of the parameters are a vector so I can use the single square bracket while the parameters themselves are a list right so I have to use the double square bracket here to get the number one number two number three from the list and that is just because listen are you have to use this double double square bracket thing so of course this then is a function so I'm just going to not return anything from this function but when I call this function using a equals 10 and X actually equals 4 then of course it should print this to the screen so let's show you guys how this works in our so if we go to our and we just do our function then have when I do my dots a equals 10 xx equals 14 then it will say a is 10 xx is 14 and it will also work when parameters are unnamed so I can do something like my dots 10 comma x and then it will just say well there's a variable which has been given to my function which does not have a name but has the value of 10 and of course had that this could be multiple so hey you can also have like 15 so now it will say there are two parameters which have been passed to the function which do not have a name one with the value 10 one with the value 15 and then we have a variable which is named xx which is the value of 14 so using variadic arguments we can accept any number of input parameters to the function even zero so hey if I wouldn't do anything then of course it would give me an error because I didn't check that there are that there is at least one right the function just assumes that there is one because it goes from one to the length but so there have to if I want to kind of fix this error then I just have to check that there is at least one element in params but so in theory I could make it work but since I always assume that there is one parameter it gives me an error and subscript out of bounds so because I'm trying to access params I which does not exist there is no parameter alright so was this everything yes this was everything because the second two questions are additional questions so the answers you can find on Moodle I just want to show you question number eight because I spend a lot of time making this thing look good and it's drawing a tree using a recursion and this is very close to things like fractals so I found this a really really fun thing and it's already three but I just want to show you guys quickly what you can do with it and that you can make stuff look really really pretty in our if you spend some time on it so let me show you how I did it and this is so well that one good so I prettified it with color so this was my first most basic try so I just have a draw tree function right and the draw tree function takes a whole bunch of parameters so it takes an X position and a Y position so this is the starting point of my tree then it has an angle which is the the angle of the line that I'm going to draw it has a depth which keeps track of the recursion because I don't want to end up in infinite recursion so the depth is the current depth that I'm at then the max depth is how many times I want to iterate and then n branch is the number of branches that a tree can have at the end of each of the of the of the of the lines right because if you would draw a tree or at least if I draw a tree and then the thing that I do is I just do something like this right so I make a single line and then I do like this this this and then every time the branches go and they they kind of branch out and then sometimes I have a little bit more branches sometimes a little bit less right and this doesn't really start to look like a tree but if you do this a hundred thousand times then it starts becoming a really really nice nifty tree so that's what the thing does so the thing is that if if I'm not at the maximum depth right so if I'm not not if I haven't recursed enough I have to do something so I take my start position and then I do some sine cosine calculations and calculate the end position I'm going to make a line coloring because I want my tree to look pretty right so I want to the top branches should be green and the trunk itself should be brown so if the depth is larger than I set the color to brown and otherwise I take like a greenish color but the greenish color again is kind of a random number that I take so that it doesn't look too green and it has a little bit of yellow in there as well and then I just say make a line so draw a line from the start position to the end position that I calculated using the line color using the line width that I selected and then I'm going to do for each of the branches just call my draw three function again but now the end position right so the end position is now the start position and that is the way that this works and this is a really fun function that you can make at home and that you can spend a lot of time on but I'm just going to show you guys how it looks because I think it looks really really nifty so let's just throw in the function using the default parameters so it will open up a window and then it will start drawing a nice little tree and of course every time that I call it it will start drawing a slightly different tree right and not all of them will be very pretty trees but most of them will be really pretty trees and I could actually make the plot window a little bit smaller because like I don't think that they will be much bigger than like 600 right so it just uses recursion and randomness to draw trees and you can see that it draws a couple of heads so here you see how it works so it puts a single line from start to end position and then it calls the function three times so one time it goes here one time it goes there and one time it goes there and then it recurses and I use the sign and the cosine to make it loop around so that it doesn't only grow up but that it can also kind of grow down right and then you get these really really nice looking trees and you can just continuously like draw different trees and sometimes they don't really work right this is not really how a tree works but then this one goes all the way down but it still looks like a nice tree and this is something that is used in video games a lot because if in video game programming of course you don't want every tree in the forest to be exactly the same but you can't have like a graphic designer make like 500 different tree models for you so what you're going to do is you're going to use kind of a standard model that you have and then you're going to randomly change like little things so that every tree has a little bit of a uniqueness in there and these are these are all fractal trees so they're all fractals and they all look like really really nice and some of them actually fail like hard and some of them will actually grow down because like I haven't put any restrictions on that the tree cannot go into the ground but this is kind of how I do it and every time you get a really really nice looking tree based on the colors and sometimes it's kind of close to a real tree but sometimes it's it's it's not even a tree to speak of I think this is more of a bush not really a tree because you can see that here the the thing actually hits lower than the branch nice say so you can do really pretty stuff with R right and it doesn't have to be green tree right like we could be on a different planet and we could say well I want to have a blue tree right and then we just say well we changed the random color selection so instead of drawing a G a green color we're going to draw a blue color and now all of a sudden we have like a kind of alien looking tree or it could be a tree that's blooming and has different kinds of colors right so and the code is relatively flexible because I could make the tree bigger I could make it smaller I could recurse more to make it more dense I could recurse less to make it less dense but in the end it always kind of looks like a tree and it's kind of a fractal tree structure and it looks pretty okay good so the code for that you can see it here all right so you can see the code here the code is also going to be here in notepad there's some sign and cosine things but this is just mathematics right I think that everyone in high school learned that if you do like trigonometry and that if you have the sign that that's like if you have a circle then the sign is the thing like that and but it's just something that you have to experiment with right and I also have the basic code for the basic tree which doesn't look as pretty as the as the other tree but it it has kind of all of the elements right and this of course is the very basic tree because you start and it only recurses once right and you could recurse more because we can say well do a depth of three and then it will make the same tree but now add a little bit to it so I have to reset the plot window and it could just say well recurse three times right but now you can see what's starting to happen so we draw a single line and then from the from the endpoint of the line we draw three new lines and then we start drawing three or four more lines and of course this is just because we are using run F to do random functions and of course we can recurse even more so we could recurse not three times but it could recurse like ten times and then the tree starts to become relatively complex and then you see that it also starts growing up and like things start overlapping a lot but you get kind of a tree-ish structure oh damn you got into looking at the wrong window tell me that you're not able to see the window when I'm doing like really nice trees right and this of course starts becoming really heavy because every time we hit the recursion it will hit the recursion a couple of times so just to let you show how this works so if we draw the tree using two depth and we do one line and then we call the same function three times every time calculating a random new endpoint from where we started we could do the same thing and then recurse three times if we recurse three times then it starts more or less looking like this so we have the first line then we call the function three times and then for each of the endpoints we call the function again three times so recursion is a very good way of creating like random objects for games or making fractals which look really nifty so recursion is something that is really really useful and it generates it generates something in very small amount of code all right so that's that's it for for now so we will have a short break and then we will start the lecture the lecture today let me show you the PowerPoint so the lecture is going today is going to be me teaching you guys how to make an R package so hey imagine that you have a really nice way of generating all kinds of random trees and you want to give that to other people right so our packages are packages that we've been using like some of the libraries like the library for normalization and so imagine that in the future you write some code to analyze your own data for example during your PhD then of course you are going to publish the results of your research but you might also want to publish your code so that other people can use your code and you are not just getting cited for the results that you obtained but that you're also getting cited for the software that you created so that is why designing our packages is a really nice thing to be able to do I actually got a lot of not so much citations out of it but a lot of collaboration because a lot of people don't know how to make our packages so they can program in our they can write really really complex algorithms but then they don't know how to create an R package out of it so then they ask me and I say sure I can make an R package out of it and then I work for like a day to take their code and adjust it so that it can be in an R package and then I'm co-author on their publication so I'm somewhere in the middle of the author list so it's a really nice way to get co-authorships to be able to make an R package because a lot of people they are very good in mathematics or they're very good in statistics but they're not that good in creating our packages but for now I'm going to say we're going to take a break I'm going to get some more water and cool off a little bit so we'll stop