 So welcome everyone to lecture number three We had a couple of people in the zoom meeting at the beginning of this week Strangely enough, I did not get any email about the assignments Which either means that you guys are all very very good and don't need any help Or it means that some of you didn't try and do the assignments and I'm hoping it's the first From experience, I know it might be the second. All right, but were you able to do the assignments? General Gulag, I don't mind people not joining the Tuesday thing. That's not the big issue The the issues is that I 90 percent. All right Next slide in German. Oh my god, Tester Sauer, if you saved up way way way way too much channel points already All right It's good to do the next slide in German. That's actually very easy because the next slide is just The intro slide, which doesn't have anything on there Guilty guilty of not doing the assignments It's so funny Anyway, let's just start right so I Will have to move my windows around a little bit. I was Picking up packages like five minutes before we had to start. I did all the assignments very good very good very good So let's see if the answers match my answers. I think I already put the answers on Moodle I also put the assignments on Moodle But I will first do a test of Sauer's his next slide in German. So let's see what the next slide is so Hello and welcome to the data analysis with the statistic packet R I'm Danny Arendt and That's all that's your whole slide in German I work on the field of research biology and molecular Tiersuchtung so yeah, well like there's not much more on the slide. You could have known this like the slides follow a very standard layout The Assignments were tough Yeah, they were they were they were Okay, see you on Tuesday. My work is done here. Yes. See you Tuesday. Yeah, the assignments were tough And that is because they are a little bit different from normal normal normal assignments And that is because I want you guys to kind of get familiar with if statements and for loops and The thing is most our courses or most programming books about are don't explain for loops and and these kinds of things I Don't know why they don't because like they generally only teach you to use the apply or the L apply function, but It's something that if you learn it and you do it properly. It's it's a really big help Because it allows you much much more flexibility So I hope that everyone Tried to do the assignments at least and got stuck at a certain point. I agree. Yeah. Yeah. Yeah No, I know that they are difficult today. They will be a little bit easier although There's one which is really really hard. So I'm already going to prepare you guys that let me look at the assignments for today because You will be in for a a very very difficult assignment. So assignment number Four and number five today will be really hard again But like you have to remember I don't want you guys to know exactly like there's there's part of the lecture Just shows you what's possible, right? Had like today we will talking about reading in binary data and I only learned that like Four years after I started programming are I need to practice more. I'm sure walking was once tough. Yeah Yeah, yeah, yeah, yeah, and the thing is is that I also want to show you guys what's possible, right? So like today's assignment There's assignment number four and assignment number five and you will see like one of them is about continuing An analysis that you already started and the other one is about reading in binary data And these are things that I only learned when I was using our for like three to four years So they are relatively advanced concepts, but I want to kind of show you how I do it And if in the future you have to read in binary data, then you can just take the slide And just copy the code and adjust it to how you need it, right? It's also giving you guys some tools but Let's just start with the lecture. Otherwise, we're sitting here like chatting for 10 15 minutes by the way We have 26 viewers at the moment so if everyone can say hi in chat then I have 20 people talking at once which is one of the twitch achievements that I still need to get and If I get that achievement I get I think like 15 more Things so you see that some people have like a diamond in front of their name and Anna has like a little sword but So if everyone just says hi, then we have 20 people chatting at once And that should be enough to get me a couple more diamonds that I can give away so that people can be like VIP to the channel Yellow yeah, you can say anything that you want. All right. Thank you guys Welcome everyone good so for today first things first, let's look at my answers for lecture two we did a couple of them already during the Tuesday seminar and I can do them live if you want, but I think I will just show you guys my answers All right, so here we see my answers of course Every file that I make starts with a header just to let people know that it's my code. I did it I changed this one in 2017 So if I change the assignments somewhere after 2017 they might not directly match, but at least I know right So it's good to kind of write down what this file does Who wrote it? When was it first written and when was it last modified? So just try and keep that in mind that every time that you start an R file you just type that in All right, so the first question was to draw a random number So question one a just use the uniform function. Of course, you can use something else as well You could draw a normal normal from a normal distribution as well But the question here was to draw from a uniform distribution. I think and store it in unknown So one of the questions that we had on the Tuesday seminars this this arrow thingy here You don't have to use the arrow. You can also use the is There is no difference between the arrow and the is the only advantage is is that the arrow can point Backwards but the arrow can also point forward. So a statement like this is the same thing and of course that you cannot do with the is because This works But this doesn't work because now I'm trying to assign the unknown variable into the function So that's not allowed. So with the arrow. I can just very basically say well I had a sign from the left side to the right side or a sign from the right side to the left side All right question one be test if this number that you just drew was higher or lower than 0.5 So I'm just writing a basic if statement. So if unknown is smaller than 0.5 I use the cut function here. I like the cut function much more Yeah, you can use the print function as well Yeah, the print function does kind of is very similar to the cut function and the cut function is a little bit more flexible Because you can say file equals out dot txt and then it will write to a file And that is more useful because generally you don't want to have stuff printed on the R window But you want to print stuff to a file, right? Normally if you would have like an analysis Then this analysis would write a log file. So after each step in the analysis, you just write to the log file And that makes the cut function a little bit more flexible If you use the print like risk fund it, then you don't have to specify the enter key So the enter if you have to use the cut or if you use the cut function, then you have to specify the slash n For the enter key. All right, so just let's run this a couple of times, right? And see if we can draw a number which is higher than 0.5 So let's show you guys the R window and there we go So first number we draw was higher and of course we want to see what we drew So we drew 0.64 and we can do again another higher Another higher. Well, that's that's quite uncommon Right, there's three times in a row like rolling a dice or rolling like a coin and getting heads So let's see if you can be lucky and get no Lower. All right. So that was the first question So relatively easy question. I hope everyone was able to do it All right, so next one here is Here I wanted you guys to use the other parameters for the function So the other parameters are drawing one random number Then you give the lower bound and then you give the upper bound Of course, there's also another way to do this because you can also just draw a random number between Zero and one then multiply this number by 40 because that's the range right from minus 10 to 30 and then just subtract 10 Many different programming languages. So R allows you to do this But a lot of programming languages only allow you to do this. So all random numbers is Yeah, yeah, no no using print is perfectly fine as well I don't think that I specified what you should use if I want you to use a very specific function or something like that Then I will write it in the in the in the question So and in this sense if you want to use print or cut or something like that, that's perfectly fine All right, so drawing an unknown number from Minus 10 to 30 and then we want to check if this number is with within or between 0 and 10 inclusively so I'm just saying if unknown is larger than or equal to 0 and Unknown is smaller than or equal to 10. So this is just to have you guys practice with the Kind of logical Boolean operators, right? So the logical and or the logical or And then I cut again So I say the unknown value is unknown and then put a new line and otherwise I do a stop error Money yeah At least people still remember that there is the mood box. So let me change mine as well interesting All right, so unknown number Check if it's between 0 and 10 if not throw a stop error and of course like for stop errors I use the paste function because I want people to know what is the what is the error, right? So to know what is the error? We have to give the user the the number back, right? Because otherwise, how would he know what goes wrong? So let's quickly do this in R and So I got a number which was 15.08 something and that's not in range And of course we can run it a couple of times and of course we can see that when the number is between 0 and 10 It is in the right order. All right. So The next question is to use the for loop to sum up the numbers one to a thousand so We had a very similar assignment during or we had a very similar example in the lecture But just to be sure that like people choose names for their variables kind of in the logical way Because the question to a and question to be looks very much similar I decided to store the first sum into for some and This for question to be I actually chose the answer to be while some of course You can choose any variable name what you want But what we do is is we first assign zero to the thing which will hold our answer And then we go and have the for loop going through X so a new variable called X being one to a thousand and then of course we do for some is for some plus X And so we just add up X to what we already had and of course We have to remember that we store it here and here you can see where I kind of do my difference between the arrow and the Because I generally tend to use the arrow when I'm defining a new variable And I tend to use the is when I'm assigning to a variable that is already there And that is not something that is that is written or that people do but for me I think it's more clear so when I'm defining a new variable which hasn't existed before I Assign or I kind of created using the arrow key And when I assign to a variable that I kind of expect there to be already loaded in our somewhere I use the is so for me That's just a physical representation saying that oh I'm defining a new variable and I don't always do it right here I unknown have because unknown already exists But here I'm kind of overriding the value in a way that I'm destroying the initial value So but generally I use the arrows and the is is for this All right, so that's the four sum to a we can run it and the answer should be 500 500 So let's just go to r and show you guys that that is really the answer So yeah, so it will just add up and of course if we wanted to know how this number grows We could do something like okay, so we want to check it in the middle then we could say like cut and we could say so far and then we could say comma four sum comma Slash new line right and now this will print to the screen a thousand times But then you can see how the number grows and that might be interesting And so you see that the number kind of just adds up. Yes, so every time It it will just update the number All right, and of course we're not done, but that's just because r takes a little bit of time All right, then the while some and I think that the while some is One of the harder ones So if at the end of the whole lecture series you're able to write something like this Then I would be very very happy because if you're able to use a while loop and Implement a while loop like like this Then that's pretty advanced already like that's kind of the goal that I have for the for for the lecture that people can do a while some But like I give it as an example or as an assignment in lecture number two But for me if like I know that people won't directly understand for loops and while loop so it it takes a while to kind of Get familiar with these concepts So since the while doesn't allow us to define a variable like the for does like if you say for x in something Then this defines a new variable x, but here I have to define my own x So that's the first thing that I'm going to do So I'm going to define my own x and I'm going to put the number one in there Then I'm going to define the variable which holds the total calling it while some and initially I put a zero in there And then I say while the x is smaller than or equal to a thousand What do I have to do so open up the curly brackets to kind of Create a block and then the first thing that I want to do is add x to the while some storing it back in the while some So this is what we exactly the same as what we did with the for some But now we have to remember that we have to add or increase x by one if we don't do this It will just continuously loop and loop and loop because x will never be larger than a thousand But of course when we increase x by one then at a certain point it will be larger than a thousand So let's just run this and then we can see that it gives the same answer. So it also should go 500 500 Well, that's that's the way that we do it if anyone has a question. Just stop me and ask that's That's what we're here for All right so the next one question number three was a little bit more difficult and That is because it is an if statement inside of a for loop So the way that I would write something like this and let me pull up the assignments Just so that I can read the assignment and then do it for you guys live So it's our course assignments to All right. So number three says create a for loop that does the following a hundred times So when I read something like this Then the first thing that I would do is say, okay So I need something that does something a hundred times So I'm going to just define for x in one to a hundred and then I'm gonna just put it like this, right? And that's the first step. Okay, so then I've I've done the for loop that does something a hundred times Okay, so the first step is generate a now a random number between zero and a hundred and store it in a variable. All right, so I Know how to do this. So it's run if one zero 100 right and the first thing that I'm going to do is check if this does what I want because hey, of course The next part of the question is use an if statement to check if the variable is lower higher or equal to 42 So I'm just gonna just run this in our right so that you can see that it draws a single random number between zero and a hundred And the thing is is that when I do this I see that it always draws something behind the comma So of course, I can never compare this to be exactly 42. So at this point, I have to kind of realize that This will never be 42 Hey, I can just do this millions and millions and millions of times, but it will never be exactly 42 Right here. We get close to 42, but still you have the stuff behind the comma So at this point you should realize that oh no if I want to have a number which is exactly identical to 42 Then I have to round down the number right because I need to compare the number to 42 So that is what I do and then I say, okay, so I know that this will That this will generate a random number, but it will have stuff behind the comma So I'm going to use the round function and I'm going to round it down No digits behind the comma and I think you can also write digits is Zero just to make it a little bit clear so that we get rid of the stuff Which is behind the whole number because we do want to compare the number and we want to get 42 Because 42 is the answer to life the universe and everything, right? So when we do this, of course, I want to store it in something. Let's call this my bar So now I know that when I draw a number this number will be a whole number and I can compare this All right, so the next is using an if statement check if the variable is lower higher or equal to 42 And then use the cut function to print one of these three statements replace X by the random number generated and make sure you add a new line To the statement, right? So that that's how I answered it here So the first thing that I'm going to do is ask if the number that I drew is lower than 42 If it is I'm going to print the numbers lower than 42 Else if The number is exactly 42 then I'm going to print 42 is the answer to life the universe and everything and Else and because I don't need to do an else if statement here because I know that when the number is not smaller And it is not equal to 42 then it has to be larger than 42 So there's no reason here to have an additional statement saying my bar Larger than 42 because we already know this right if if if it's not smaller and if it's not Equal then it has to be larger that there's no other possibility for numbers. I Use sample instead of run if and it's a round number. Yes. Yes. Yes. Yes. Yeah That's that's a very good very good way of doing it as well. So sample is indeed sample is generally used if you want to Mix up a matrix, right? So if you have a matrix and you want to shuffle the rows or the columns Then you use the sample function saying from one to the number of columns Draw the number of columns and then you can use the replaces true or the replace is false to not allow for drawing duplicate numbers But yeah, you can you can use the sample function as well I Thought the answer was 420. No, the answer is not for the answer is never 420 even though 420 was last week It's not the answer All right, let's let's just run it in our right and and see if we get a get a nice If we get 42 because that's the that's the goal. So All right, so let's run it and And we were pretty unlucky so in all the draws that we did we didn't draw 42 So let's just try it again and here we have it. So here. It says 42 the answer to life and everything all right, so I Hope everyone was able to do this one because this one is kind of And it combines these two concepts it combines the concept of looping So doing something an x number of times combined with the concept of checking if a number is something or something else, right? So and There's really nothing more in data analysis, right? It normally you would go through your matrix through each row of the matrix and then you would say well if the Weight of the animal is larger than a certain number then included in the experiment Otherwise excluded. I took some time for exercise three, but it was good. Yeah Yeah, that's one of the things that I love about programming the a fork saleiveness, right? The the the feeling that you get when you when you work on it for like 45 minutes And you're like why doesn't it work and at a certain point you're thinking like Yeah, that and that's the thing that that like that's the thing that I love about programming is is Coding and then in the end getting it to work And it can be the best feeling in the world and the thing is is that you generally can't really share it with anyone I Recently spent almost like two days to just make a Triangle on screen and then I got it working. I was so happy because it was like almost a thousand lines of code and then like If I would Run to my dad and say dad I made a triangle on screen He's like okay and nice, but that the your folks are late. This is it's something that you can enjoy And that that's one of the nice things about being a programmer All right, so question number four use a while or a for loop and the cut function to print out a triangle of hashtags having 12 lines Yeah, you cheered for my trying I'm not saying that no one will understand you It's just that you have to select your partner in such a way that they also get excited about programming And you cheered but if I would have called my dad He would be like meh like he would probably be happy that I'm happy, but he wouldn't really understand the investment All right, so question number four use a for loop or a while loop and the cut function to print out a Triangle of hashtags having 12 lines each line should have one more hashtag than the previous line And then there's a spelling error and then I show you one of these are a part of the triangle So let's I can show you my answer and there have been a lot of interesting answers During the past five years that I've been giving the course. I've seen like 20 different answers I think and all of them are fine right in the end if you I totally overthought a thought exercise for yeah That's what most people do So this is kind of the first answer that people come up with so or no Generally, this would be the first one All right, so general a gulag. Let me just get your answer out of twitch I think I can just copy paste from here and just put it in here All right, so let's see All right So the first thing that I don't like about the code here, right is that if you write a for statement You should use the brackets right the curly brackets are there for a reason They are there for a reason to make clear to people That you're starting a something which is inside of the for statement, right? So if anything even if you want to keep it on one line I would always put the curly brackets there because the curly brackets just say it goes from here to there So it's not just something that the computer understand. It also makes it easier for anyone reading your code All right, so what happens here, so you are doing a cut which is okay So let's remove the cut because that's what we will all do. So just make sure that we don't do that All right, so you paste string replace this thing 12 minus I String I I'm gonna throw it in R and see what it does. I'm very curious I this is this is a new answer. So that's always good. All right. Let's go to art throw it in see what it does Wow now, that's a good triangle. That's a much better triangle than mine There are spaces between the hashtags though I I think I made an explicit remark about it that there shouldn't be any spaces between the hashtag But this looks really really fun like this is a good triangle I have a similar answer for this. So why did everyone look up the string rep function? That's interesting that's interesting like this is there like an answer like this somewhere on stack overflow Where they say you should use string rep. That's an interesting one. All right, let's put this one in as well So yeah, that's a good one. That's a good one and this is very similar to mine, right? So my my original answer Which not this one. Let me show you notepad plus plus So my original answer that I came up with was this one This is just because I wanted people to understand that you can use a double for loop and that a for loop can actually be Inbounds that you want so the initial answer that I came up which was saying well I want to do something 12 times. So I'm just going to type 4x in 1 to 12. So what am I going to do? well, I I'm going to draw hashtags, right and the hashtags I need to have as many hashtags as the line that I'm on so I'm saying 4 y in 1 to x And so the first time that it goes here x is 1 so it cuts a single hashtag The second time that it comes in here Then it draws to hashtags and then of course the new line, right should be outside of the of the second for loop right so you have a for loop which does some printing and then prints and enter and It just cuts these things. Of course, you can do it a lot Lot smaller and this is very similar to the string wrap function, of course is that 4x in 1 to 12 cut and then repeat the hashtag x number of times Based them together. I don't know why there's a slash B in here Why the hell did I put a slash B in here? Let me see what that does the slash B Hmm interesting, so it doesn't even remove the All right, so the thing would be something like this might be that the answers were a little bit older All right, so there was a similar feed in Stack Overflow about a similar question, but they try to build a router Okay, so a diamond shape All right for number in 1 to 12 hashtag is Hashtag hashtag is replicate n is number hashtag All right, let me let me see if I can get that one for you, Jan Haga So this one, okay So I think the thing is that which throws it all on one line, right? So you don't really have any code layout So I'm just gonna lay out it as I want and then you're going to print it collapse is nothing That's a good one. All right, so now everyone's sharing their answers I like this one as well and this one I like a lot because it's very explicit, right? So you define something called hashtag and then you have hashtags, which is the repeat or the replicate and I Would just say in this case you probably just want to use the rep function with n hashtag and then number I Like the rep function a lot repeat this thing this many times Of course the replicate function works as well and then you print pasting them together So this is perfectly fine. This will draw a nice nice triangle as well All right. So alexander came up with a different answer. So let's put that one in there as well All right. So why is empty? 4x in something Why paste why? Oh, that's a good one. That's a good one. I like this one alexander I like it a lot and you know why I like it a lot because it reuses One of the answers that we already had right? It looks very similar to the four some where you add stuff to the sum And it's it's very similar to that right? So I hope everyone can understand what happens here But he just builds up this this string called why and then every time he just adds a hashtag to it That's a good answer. I like it like this is the way that a mathematician would think right? There's this joke about a mathematician that that like hey You have a physicist and a mathematician in a room and they want to boil a pot with water And the first time that they get in the room. There's a pot with water and there's an open fire There's a there's an empty there's an empty Kettle and there's a water faucet and there's a fire And so the first time the mathematician and the physicist go in and well They take the water they fill the kettle and then they put it on on the on the on the fire And it borrow boils then this the second time that they come in they come into the room And the kettle is already full so the first thing that the mathematician does is just throw out all of the water from the Kettle and then refills it with water and then the physicist asked Why do you do that? Well because I want to go back to the original question because I already solved that one So why think again? Why not just reuse what I already had? First time reading through question for I thought it's a fun. Fraga because of the hashtag Because of it being comments. Yeah, that's no. It's not like I generally don't try to trick people with with like trick questions Interesting all right, but I hope that everyone sees that there's many many different ways of solving a question like this And of course, they're all fine in the end when you get a nice Router or you get a nice triangle head and that's the head But the idea is is that you just try and solve it for yourself But yeah, I like coloxon. There's I like yours a lot. I like yours a lot Like it's thinking like a mathematician They're going back to a question that you have already answered and then using kind of the structure of that answer to answer the next one interesting All right, so Question number five escaping so this is just to teach you guys that some things need to be escaped So of course, there was a sentence. I say escaping stuff is great But slash slash but backslash and forward slash might be a nuisance And then we want to have a new line And then you are correct But I think that the top and the the backspace character create more problems than the basic thing so the the the question was a little bit strange because There was a smart quote in there so smart code quotes are something that Microsoft Word does and Also PowerPoint and these kinds of things so the smart quotes are the quotes that you put around like a statement right but In programming you only have like these real double air quotes I couldn't zoom in a little bit to show you why the smart quote is different, right? So you can see that the smart quote here is slightly different than this one it's a little bit less and it and it and it slanted and that's just because in typing text you use smart quotes and Microsoft Word tries to be smart and Change quotes by smart quotes And every time that I make slides for programming in our it's it's really annoying because every time that I type like the double air quote PowerPoint thinks that I'm starting a real quote from some famous guy and this changes it by a smart quote And of course smart quotes are just characters, but they're not the real quote characters So they they are in the same character set, but they're slightly different So yeah, of course here Just escape the the characters that need to escape So the the real double quote needs to be escaped The tab character or the the slash character itself needs to be escaped And of course here you need to escape it as well. Otherwise, it would do a tab and a backslash But of course the smart quote in the end it doesn't need escaping because it's a different character than the other quote So had just that you're aware that that some letters and numbers although they look very similar They are different The double quote thing cost me at least 15 minutes today. Yeah. Yeah. Yeah, and it will Bite you in the ass because if people do stuff in Microsoft Excel Microsoft Excel also likes using smart quotes and then you're hitting yourself over the head because you you took your data from Excel and saved it to a Word document and then you're trying to load it in into our and for some reason are just completely messes up the smart quotes and in this case, it's not too bad 15 minutes wasted on a single character. That's the essence of programming. I agree. I agree. I agree That that's just the way it is like it's like working in a lab, right? You have to be very secure so if you want to see something funny, then there's an esoteric language. There's a programming language, which is called white space and The whole programming language consists of different white space characters So you have a space, of course, you have a tab character, but then you have like the space character in Farsi and then you have the space character in like Chinese and these are all different characters and the computer can read that. So just Google on or go to Google and Google white space programming language And then of course in the end, the whole script is or it looks empty But you can program more or less anything that you want in it. You just have to use the right white space characters Of course, this is a complete waste of time But someone figured out or that it was a fun thing to do and have a programming language which only centers around white spaces All right, then question number six, we spent some time on this during the lecture on Tuesday So if you want to know what you should see here then look at the Moodle and Check out the last part. I think it's the last part of the lecture. I'm just going to run it for you guys and then see what I observe So I think the question was relatively clear on what to do. All right, so we set our C to 1 We draw a random number between Between 0 and 10 so we draw 15 random numbers. We round them down and then we'd say random 1, right? So it draws the numbers 3 4 6 9 and so forth Then we set our C to 1 now we first draw a normally distributed number and then we do The same call again as that we did before What do we see now is that if you look at random 1 it starts with 3 4 6 9 If you look at random 2 it starts with 6 9 So you see that the whole this part here is the first part here So by drawing a single random number not from the nor from the uniform distribution But from the normal distribution before We lost two of our numbers in in the random one draw So the thing is is that in many programming languages? Every random number function has its own random number generator But in our this is not the case our has a single random number generator and a single random number generator will Will Will feed all of the random number functions, so if I do a uniform number random, so run if Then this will take away from the our uniform and The our uniform will take away from the run if function and the our Poisson does the same thing So they all draw from a single source of randomness and by drawing a single number here, right? Using the our norm function the our norm function takes away kind of two numbers from the run if function that we do after and Other programming languages don't do this So I put this in for people that come from like a Java background Because they are always stunned that when they reset their seed doing the same thing is Different from doing it the first time and that is just because they do another function call in the middle And that just confuses the hell out of them and Of course if you have no other language experience Then this is just the way that it is that there's a single source of randomness and drawing a random number using our norm Takes away some of the randomness then drawing a random number using the uniform distribution takes away some of it but other languages do not function that way, so if I think about The deep programming language or Java or C sharp They do it in a different way So there the our norm function has its own random number generator the R The run if function also has its own random number generator, so they don't influence each other But in R they don't and you have to be aware of that Especially when you start doing things like multi-core programming, but of course that's far far away It's only lecture three, but that's the thing that I wanted you to see that there's a shift And so the first two numbers are used to generate this number and then all of the other numbers shift And we talked about it also on the on the Tuesday seminar or the Tuesday thing. All right, and that was it Because oh no, we have function still. Oh, yeah, so question number seven create a function that returns the result of a coin flip all right, so first little function So I called it flip coin. It's a function. It does not take any parameters and Then I define a block So the first thing that I'm going to do is generate a random number a uniform random number because coin flips are generally Like equal right? There's an equal chance of putting heads putting tails And then I draw the random number and then I say when the random number is lower than 0.5 I say heads if it's larger than 0.5 I say tails and I return this number so the function ends here So as soon as you call return the function stops and it returns the thing which is inside to the user Of course the number we could be very very lucky and the number could be exactly 0.5 and Then I return it's on its side Because of course like I want to the coin to be equal to be fair, right? So I want to say heads half of the time tail more or less half of the time, but the problem is is that 0.5 is also could also be drawn the chances of it are very very small because it generally draws like five or six digits behind the Comma, but there is a chance that you will draw exactly 0.5. So in this case, I say that the coin is on its side So let's just run it and flip the coin a couple of times and see what it comes up with So let me switch you guys to the R window All right, so let's flip the coin and have first time I did tails then I did heads and then I did tails So I hope everyone was able to do the function and because functions You could also do something like this where you limit it to either zero or one Yeah, yeah, if you would round it down But also rounding down as an issue because rounding down is also not entirely fair because 0.5 Exactly gets rounded up so your function Selena has a slightly higher bias towards the Tail side All right, could someone block the ship Mustang guy I can actually just do that Where is my moderator tools? No, I want this guy to be reported and That's too much work Could my moderator just step in all right good. Thanks moderator Yeah, so but yeah, of course you have to remember that if you draw a random number between zero and one then there's no fair way to really Be equal so even if you would do something like this Let me show you guys the answer So just that it's not just in chat, but that it's also on the on the screen All right, so we do a function and we do if And then we do else so again like I like the curly brackets So if you use an if statement always put curly brackets around it That just makes it so much easier for the reader to read And then we do tails and we do like this Yeah, so here the the this works perfectly fine just as well as mine The only thing is is that here the round function will take zero point five exactly and round it up So it it has a slight bias to call Tails because we're comparing if it's zero. So had the zero will be slightly less Convene or slightly less occurring and of course we can only see this when we draw this Cointoss function like a couple of billion times So if we would run this and do it a couple of billion times and then look at the graph for heads and tails And then you will see that if you would do this a billion times Then tails would come up slightly slightly more often than the other one you can use sample Sample is probably the best way to sample between One and two and then if it's one you could also use sample and directly sample heads and tails Yeah, because you can do something like this like sample Where do I want to sample from well? I have something called heads. I have something called tails and then sample 10,000 times and then I say replace equals true because I want to do it and so something like this Should work as well, and it should sample and this is the most honest way because now that there's no chance of Having one of the two being being over represented So if we would store this or in something called R and we do a histogram of R then We can do histogram We can why can we do histogram? Let's just do an ass fucked or on it It should be no no, it's not a histogram what I want is a barplot. No, what I want is a How's a plot like that cool? I don't care. So I do a barplot and do it as numeric and Then just do a histogram of those Right so now we can see that that they are more or less exactly identical to each other But if we would use one of these functions with which rounds the number Then of course there would be a slight bias towards one of the two. All right All right, so next function. I think this one is really nice So the next function question six was just make the make your triangle thing into a triangle function With a parameter called size to make it as big as possible So I just took my rep answer which collapses the thing and then of course you can just use Make a triangle as big as you want Of course, it will start looping at a certain point in R But that's okay. So maybe we can do a triangle 25 and then it's a little bit bigger I'm interested in the router thing because that will look very very funny when you start off from the middle so All right questions seven a why is this numbered so weird? Okay, so then we have question nine, which is the last question so create a function that calculates the Factorial of a given number and the function signals your shoe looks like look like this here X is the function parameter that represents the hold or the input value from the user. All right, so The last one so question number nine actually It used to be seven a but now it's nine so then this is question number eight So let me update that for you guys. So this is my factorial It takes X and again, I do the same thing but now had the F here is my some kind of thing But since I'm starting to multiply stuff I cannot put F to zero because if I would put it to zero then of course I would multiply with zero and then it would Always be zero. So I have to put F to one because the fact that it's a multiplication and multiplication Hey, if you multiply a number with zero, it's always zero And this is different from summing a number to zero, right five plus zero is still five But five times zero is zero. So if we calculate the factorial Then we have to start with F being one And then we go just through the number. So we say for I so I is the the variable that is generated Internally in one to X. What do I want to do? Well, I want to take the number F and then multiply the I with the F that I already had and then in the end I want to return F So let's just run it and then one of the additional assignments Normally, I don't do them, but I think that this one is quite funny because The plus function we already saw right so we already saw that when we do the plus function Then if you want to apply the plus function, you have to do double quotes, but you can also define your own Operators so you can define your own kind of special function and give it a name like Exclamation mark and that's what we're doing here So we're just defining a new function with the name exclamation mark, but we we can't do it like this Because if we do it like this then now R will not recognize that I'm trying to assign a function It will now say well the exclamation mark is something that I already have defined But we can overwrite these things by doing something like this where we say well make a function call the function exclamation mark And then just call the my factorial function and then of course we can use factorial 5 or 5 factorial like this to kind of run that All right, so let's go to R and Let's run the factorial function as well. So and of course head. This will work perfectly fine as well So now we can define our own things and of course we can define a lot So you can also use like the skull and bones if you wanted to Because you can just use unicode characters as well. So you can make or you can make as funny as function names as you want Yes, so in theory you could use also the yes So everything that shows up in the mood box is also in R a valid Identifier so you could also take like the little money face And then copy paste it into R and then assign it as a function. So you you can do that. So that's allowed All right, so that's that's it for the assignments for today So let's go to the PowerPoint Alrighty then so Let's take a look at my answers from lecture two. So we did that. Are there any questions about the assignments? Were they hard were they too hard did you guys enjoy? Something that you had to kind of think about a little bit because that's always what what is difficult for me, right? Like I make the assignments, but I have like years and years of programming experience. So it might be that in my mind they are Really really easy and for you, it's like no, I spent like half of my weekend on it So if you if you think that they are too hard, then definitely let me know But be aware that some of the assignments are hard not to Kind of have you guys work on it for three hours, but to also have you guys realize that I'm working on this question now for three hours, and I can't figure it out perhaps I should ask someone to to help right because it's Programming is something that you only it took a lot of time, but I'm fine with it. Okay. It's good. He that just says crying face, which is But then in the end like programming something that you two hours and got to number five. Well, that's good That's good. So that's fine, right? I don't expect you to do all of them Hey, I expect you to kind of Try it Do it yourself and if you get stuck right on an assignment and you don't know what to do Ask for help because asking for help is nothing which is bad It takes me forever all the time so yeah No, but then like be be aware that there's nothing wrong with asking for help like there's there's stack overflow, right? Which is a whole platform which makes their money by helping or connecting people that have questions about programming with other people who ran into the same questions or had to to help each other and because programming is something that like it works to collaborate and Normally when we do do the assignments we would do the assignments directly after the lecture We would do it in person and then I I always say it's perfectly fine If you want to sit in groups of two or in groups of three I Also took some hours, but after it I understood the functions and loops and that's the thing because at a certain point I Think it was difficult. Maybe I it could help to get answers earlier No, I'm not gonna put the answers earlier. So in that sense if you get stuck just send me an email Ask for help Because that's the thing that you should learn as well. You should learn that at a certain point there are things that you don't know which other people might know and I Don't like people just looking at the answer and then thinking like yeah, oh no, so that's how it should be done I'd rather Have people mail me. I give you a hint if you still can't solve it then I can show you how it's done Hey, because in the end like only by getting stuck and getting frustrated Do you learn in a way? Because without like getting stuck or hitting a wall It's it's really hard to learn new things and to to kind of And it takes away from the a folks a lateness as well Right, if you just see the answer and think yeah, that's the way that it should have been done And then you don't have this like really nice feeling when you spend like two hours on something and finally you fix it And finally you get it to work and then you're like, yeah, see I can do it as well So that that's the thing that I That I that I I'm against putting out answers early And of course you're more than welcome to join the Tuesday and send me an email Because there's like there's a lot of help out there. I'd rather not people spam stack overflow with questions about the our course so just Yes, please same for the examination. Yeah, yeah, that will be fun, right? Yeah Then you have to just ask people who did the examination last year because they don't change that much between years So find someone who did the exam last year and just get a copy from them Although they're they're different every year So it's not that you can just remember the answers for the examination and then just go into the examination and pass it but Generally the examination doesn't have very complex questions on programming like you might have to do a little function like a function like the factorial function or a little for loop that draws a triangle But I don't expect you guys to be able to do something like Let me see. Are you recording? Yeah, I'm still recording. We should actually take a break soon Because I've already been recording for almost an hour. So All right, so but I'm not I Don't want you guys to be able to write like massive amounts of code I want you guys to understand what you can do with our and How you should do it, right? Like know which functions there are Have what kind of parameters these functions have and then that for me is good enough have because in the end you're only going to learn programming by practicing and In the end I can't really Kind of grade how much you practice because I don't know how much you practice. So that's the thing All right, so Let's take a little break and this is going to be a little bit annoying show us