 And welcome everybody to FizzBuzzSolution. And this is a common interview question that you're beginning programmers or beginning job seeking for programmers. Get this one a lot for some reason. It seems kind of simple on the surface, but has a few gotchas you have to be careful of. So before I get started, I want to give a quick shout out to my channel members. Kevin and Paul are still leading the pack. And in this video, we welcome super solvers. Thanks so much to them and everybody here for supporting the channel. Much appreciated. So let's get it started with our challenge here today. FizzBuzz. FizzBuzz is basically a child's game. I've never really heard of it, but apparently I guess people play it. And what the concept is, is that you are counting from one to 100. And if the number is divisible or evenly divisible by three, instead of saying three, you say Fizz. Okay, so that'd be three, six, nine, 12, and let's see what we have 15 out of there. If it's divisible by five, you say Buzz. But if it's divisible by both, like 15, you say FizzBuzz. You can do the same thing all the way up to 100 and we'll include 100 in this particular example. So it'd be one, two, Fizz, four, Buzz, Fizz, seven, eight, Fizz, Buzz, 11, Fizz, 13, 14, Fizz, Buzz, et cetera, et cetera up to 100. So let's go ahead and think about that. So what I'm gonna do is I'm gonna do four I in range. Again, if you're at the point where you're looking to do a job interview, this should be pretty straightforward. We're starting at one. We wanna end at 100, but we want it to be inclusive. So we need to do 101 here. And I could test it. I could say print I just to make sure it does start at zero and end at 100 or does start at one and end at 100. But again, if you're at the point where you're interviewing, hopefully you got this part down. So we gotta think about this. We've got basically four conditions. We've got FizzBuzz. We've got Fizz and we've got Buzz. And then of course we have none of the above. Okay. So FizzBuzz requires divisibility by three and divisibility by five. So I'm gonna just go ahead and code that. So if I percent three equals zero and I percent five equals zero print Fizz, oops, need some quotation marks there, FizzBuzz. That's one condition. L if, and we'll do Fizz, which is divisible by three. And I'm doing this one first just because it's the most common. Actually, I should probably reverse these because the five is less common than the three. So I percent three equals zero because of short circuit evaluation. And if that is, look it up, I guess. So I percent three, let's see, equals. Again, equals zero. We're going to print Fizz. And then we're gonna do our buzz. So L if, I percent five equals zero, print Buzz. Now if it's none of these, we would do else, print Fizz, print the number. So in this case. And let's go ahead and test it and see if it's working. Okay, so now again, I haven't memorized all the numbers, but let's say one, two, Fizz, because that's divisible by three. Buzz is divisible by five. Six is divisible by three. Nine's divisible by three. Buzz is divisible by five, 11. And then we got Fizz, and then we do have a FizzBuzz. So we should see FizzBuzz like 15, 30, 45, etc, etc. Okay, and if I go all the way to the end, just to kind of test the last few here, I got a buzz for 100, which is good. I got a Fizz for 99. And you can see how basically it is working as expected. Now again, I did a little bit of an optimization here right away is I checked for divisibility by five first, just because it's rare. Because what happens, and this is what I said earlier about short circuit evaluation, because I have an and here, if this is false, it doesn't matter if this is true or false. There's no point in checking it. And the computer will not check that statement. It'll just check this if it's false. It knows better than to go ahead and check this because it'd be a waste of time. So then we go and we do check for three. And again, I chose three because actually three is more common. And since three is more common, I basically wanted to, then it doesn't check for five because five is rare. So hopefully that kind of makes a little bit of sense and why I chose to do it that way. Now just out of curiosity, if we counted how many comparisons we had to do for each of these. So in this case, we have to do two comparisons. Actually, I guess you could argue it's 1.5 because part of the time it's a five, it's this is done and this isn't. Let's just go ahead and count this as one if statement. And how many if comparisons we'll do? We'll just ignore the fact that's an and. Now if we do fizz, count is gonna be plus equal to because we had to do this and this. And if we get the buzz, we've had two failed accounts, two failed, and we end up with three altogether. And then else is just what's left over. So that's not really a comparison. So let's print the count of this particular approach. Now this isn't maybe the best way of calculating it, but it'll give us a rough idea of how many comparisons it took us to get this. You can see here the answer is 102. So we have a certain number of these, we have a certain number of these and we have a certain number of these. And then of course this is the default, we're not counting the else because the comparison has already been done. So just out of curiosity, it's 102 comparisons to get to this. And again, I'm ignoring the fact that this is really two, but sometimes it's only one, sometimes it's two. So we could put it 1.5, but you get the idea. But I don't think it's gonna be a huge change there. So just kind of remember that number. Now there's another, I mean, there's plenty of different ways to do this. So I'm gonna go ahead and get rid of this and try to come up with a different way of doing it. So what I'm gonna do is I'm gonna use an auxiliary kind of variable here to help. So I'm gonna call it found and that's gonna be false. So what I do here is I'm gonna say if I percent three equals zero, then I'm gonna go ahead and print fizz, no space, end equals single quotes. I think I can use double quotes there too. And this is Python three and what this does, this is like print in Java versus print line. So what this does is it prints across and it doesn't go to the next line. Because what I wanna do here is if I percent three equals, or I percent five equals zero, I'm going to print buzz and end equals quote, quote. So now notice I still have to do this. So for every three, I'm still doing this one. Now if it's not a fizz and not a buzz, I wanna do if not found, I know I didn't add that yet, print i and then print, I'm gonna say print i, print i, else print, just put a space. And what I gotta do here is I also have to put, I have to put found equals true. So it's a little bit more complicated, but I think it's gonna be fewer comparisons. I'm not sure, let's find out. So found equals true. Now I could also at this point just do, actually, I don't need to do end here, I could do print. Well, I'll leave it like this. I'll say found equals true and then we'll kind of go ahead and kind of play with the numbers a little bit and see what happens. Again, it's a seemingly simple problem, but yeah, it's kind of interesting. So let's go ahead and run this and see if we get the correct results. And I think we did, yeah. So look, it looks very similar. So we got one, yeah, so fizz, we got buzz, fizz, fizz, buzz, fizz and fizz, buzz, okay. So let's go ahead and do a quick count here. So count plus equals one. And if I got to this one, I've done two. So count plus equals two. And then here I got count plus equals three. So that's the only way it would get there. I think this is gonna be a lot more in the counts. Yeah, so that's 232 different comparisons. So that's kind of interesting. Now I can cut this one down a little bit by doing the following. I can do, let's see. Actually, I should have added count plus equals three here because I didn't count this. Because even if it's not found, I'm still doing a count. So that's kind of interesting. Actually, that should be plus equals one. I guess they should all be plus equals one because I'm still counting these. The other example, I wasn't. So let's try that again. That's 120. So that's just a little bit more than the other one. Oh, we got a little bit of an error here. Okay, because I forgot to put found equals true. So it's kind of an interesting problem. Oh, that was 106. So it's only four more comparisons than the original method that I did. But again, I didn't really account for the fact that in that first comparison, there were actually two comparisons. So I think this is probably a slightly more efficient method. Now the other thing I can do here, I realized is I could get rid of this and I could just print buzz and then continue. Because if I find a buzz, I've maybe found a fizz, maybe haven't, but I don't need to do not found. And then because I don't need to worry about this because I'm using print here. So let's see what result is we get here. Still 106, okay. So that didn't really help as much. I really thought it would help a bit but I guess I was wrong on that one. Probably because I wasn't counting in here. So count plus equals one. I still have to, I guess I still have to count this one. So maybe I did that wrong. So this is 133 and let's do found equals true. Let's go ahead 153. So actually, okay. So this is 153. If I change this to continue, it avoids a few down here, a few comparisons. Because it was 133. But the original method that I did actually gave us fewer comparisons because of the use of if, L, if, et cetera. But again, I'm not sure if I correctly kind of accounted for the fact that there was an and here. So yeah, it's kind of interesting. So those are a couple different ways to, to solve this particular problem. This one looks a bit more complex. But, and it also relies on the fact that you can print without doing a line return. And so yeah, so that's kind of interesting. It's really a fascinating problem. I should probably try this again and do it like with a proper timer and maybe just do it up to a million and see which is fastest, which would be kind of an interesting exercise. But maybe I'll save that for you, the viewer. So that is fizzbuzz. That's two ways of solving it. Again, this is a common interview question or, so I hear, because I haven't actually done this type of interview myself, but it's pretty straightforward. So this is something, if you're going to be going for a programming job, you probably want to know how to do this in whatever language it is that you are focusing on. All right, good luck. Thanks to everyone for watching and, you know, keep on coding. Take care.