1 00:00:00,000 --> 00:00:00,500 2 00:00:00,500 --> 00:00:02,640 ZAMYLA CHAN: It's a-me, Zamyla. 3 00:00:02,640 --> 00:00:06,950 Today, we're going to implement Mario, where we draw Mario 4 00:00:06,950 --> 00:00:10,730 a full pyramid for him to jump over. 5 00:00:10,730 --> 00:00:14,430 Let's break down the sub-tasks for this problem. 6 00:00:14,430 --> 00:00:17,930 >> First, we want to prompt the user for the height of the pyramid. 7 00:00:17,930 --> 00:00:20,510 And then we want to make sure that that input is valid. 8 00:00:20,510 --> 00:00:23,280 And then we're going to want to draw the pyramid. 9 00:00:23,280 --> 00:00:26,850 >> So let's talk about prompting and validating user input. 10 00:00:26,850 --> 00:00:29,920 It might look something like this, a do-while loop that 11 00:00:29,920 --> 00:00:33,250 prompts the user for an integer and then only repeats 12 00:00:33,250 --> 00:00:35,700 if that integer is invalid. 13 00:00:35,700 --> 00:00:38,220 So what should this condition be? 14 00:00:38,220 --> 00:00:40,630 >> For this, let's go back to the specification. 15 00:00:40,630 --> 00:00:46,240 Well, the spec tells us that any valid height is going to be between 0 and 23, 16 00:00:46,240 --> 00:00:47,350 inclusive. 17 00:00:47,350 --> 00:00:51,400 So then that means that any invalid height is going to be less than 0 18 00:00:51,400 --> 00:00:53,840 or more than 23. 19 00:00:53,840 --> 00:00:57,220 >> So now that we have this information, let's design our condition. 20 00:00:57,220 --> 00:00:59,230 But we need to be careful, because now we have 21 00:00:59,230 --> 00:01:02,130 two Booleans that we want to evaluate. 22 00:01:02,130 --> 00:01:04,150 >> Here I've provided you with a truth table. 23 00:01:04,150 --> 00:01:07,520 This allows us to take two Booleans, Boolean one and two. 24 00:01:07,520 --> 00:01:13,600 And we can evaluate either bool1 and bool2 or bool1 or bool2. 25 00:01:13,600 --> 00:01:17,390 >> So what's the difference between and and or? 26 00:01:17,390 --> 00:01:21,810 Well, bool1 and bool2 will evaluate to true if and only 27 00:01:21,810 --> 00:01:25,680 if both Booleans are true, whereas the or operation 28 00:01:25,680 --> 00:01:30,190 will be true if either one of the Booleans or both are true. 29 00:01:30,190 --> 00:01:33,110 So with this in mind, see if you can figure out 30 00:01:33,110 --> 00:01:37,460 what the appropriate condition for your do-while loop for an invalid n 31 00:01:37,460 --> 00:01:38,850 is going to be. 32 00:01:38,850 --> 00:01:43,540 >> With that, we've prompted and validated the user for the height of the pyramid 33 00:01:43,540 --> 00:01:44,550 that they want. 34 00:01:44,550 --> 00:01:47,740 So now, it's up to us to draw the pyramid. 35 00:01:47,740 --> 00:01:50,520 The full pyramid for this problem will look a little bit 36 00:01:50,520 --> 00:01:56,340 like this, where we have a left pyramid, some gap, and then a right pyramid. 37 00:01:56,340 --> 00:01:58,630 So let's break this down a little bit. 38 00:01:58,630 --> 00:02:03,510 >> If I go to my text editor, here I've drawn us a left-aligned pyramid. 39 00:02:03,510 --> 00:02:05,160 But that won't do. 40 00:02:05,160 --> 00:02:10,139 What we want to do is we want to create a right-aligned pyramid first. 41 00:02:10,139 --> 00:02:13,580 So to do this, to just push my hashes along to the side, 42 00:02:13,580 --> 00:02:18,240 I'm just going to put some characters in between, just these dots. 43 00:02:18,240 --> 00:02:22,560 Then I'm going to put two on the next line, and one on that line. 44 00:02:22,560 --> 00:02:26,070 And so here I have a right-aligned pyramid. 45 00:02:26,070 --> 00:02:28,540 >> After that, I'm going to go back to the top row 46 00:02:28,540 --> 00:02:33,930 and put in the gap, which is, per the spec, two spaces. 47 00:02:33,930 --> 00:02:37,680 Then I'm going to fill in the other side of the pyramid. 48 00:02:37,680 --> 00:02:42,030 I'm going to go to the second row, write two spaces for the gap and then two 49 00:02:42,030 --> 00:02:42,920 hashes. 50 00:02:42,920 --> 00:02:47,020 Back to the third row, two spaces for the gap and three hashes. 51 00:02:47,020 --> 00:02:51,480 And at the end, two spaces for the gap and four hashes. 52 00:02:51,480 --> 00:02:54,400 So that's what a full pyramid will look like. 53 00:02:54,400 --> 00:02:57,260 >> Of course, we don't want these dots in the way. 54 00:02:57,260 --> 00:03:01,220 So we're going to replace these dots by putting some spaces in. 55 00:03:01,220 --> 00:03:03,720 One, two, three on the first line. 56 00:03:03,720 --> 00:03:05,650 One, two on the second. 57 00:03:05,650 --> 00:03:08,200 And one on the third line. 58 00:03:08,200 --> 00:03:12,060 So this is what we would do if we wanted to just make a full pyramid, 59 00:03:12,060 --> 00:03:13,700 say, in our text editor. 60 00:03:13,700 --> 00:03:15,860 >> So let's take this, understanding the patterns, 61 00:03:15,860 --> 00:03:19,870 and convert it over to some pseudocode. 62 00:03:19,870 --> 00:03:24,500 For each row of the pyramid, we want to print the left pyramid and then 63 00:03:24,500 --> 00:03:26,580 the gap and then the right pyramid. 64 00:03:26,580 --> 00:03:30,260 For the left pyramid, we print the requisite amount of spaces, 65 00:03:30,260 --> 00:03:31,740 followed by the hashes. 66 00:03:31,740 --> 00:03:35,030 Then we print the gap, which is just two spaces every time. 67 00:03:35,030 --> 00:03:38,080 And in the right pyramid, we print the requisite number of hashes. 68 00:03:38,080 --> 00:03:41,410 >> For the second row, we conducted the same exact process. 69 00:03:41,410 --> 00:03:44,510 We print the spaces for the left pyramid, the requisite amount 70 00:03:44,510 --> 00:03:49,930 of hashes, the gap, two spaces, and then the hashes for the right pyramid. 71 00:03:49,930 --> 00:03:52,490 >> Let's tackle the pattern for the left pyramid. 72 00:03:52,490 --> 00:03:55,660 If I had, for purposes of this example, a height 73 00:03:55,660 --> 00:03:58,790 of eight requested by the user, then my first row 74 00:03:58,790 --> 00:04:01,020 would have one hash and seven spaces. 75 00:04:01,020 --> 00:04:03,860 My second row would have two hashes, six spaces. 76 00:04:03,860 --> 00:04:06,749 Third row, three hashes, five spaces. 77 00:04:06,749 --> 00:04:09,040 You can probably do this yourself for a height of eight 78 00:04:09,040 --> 00:04:13,470 and determine for each row how many hashes and how many spaces you need. 79 00:04:13,470 --> 00:04:16,209 But what we want to do is we want to abstract it. 80 00:04:16,209 --> 00:04:22,660 So I ask you for any nth row, how many hashes and how many spaces do we need? 81 00:04:22,660 --> 00:04:25,410 >> Now, as you determine the pattern for how many hashes 82 00:04:25,410 --> 00:04:29,920 and how many spaces you need for any nth row for a given height, 83 00:04:29,920 --> 00:04:32,910 remember to be careful of how you're indexing. 84 00:04:32,910 --> 00:04:37,160 What I mean by this is that in everyday life most of us start counting by one. 85 00:04:37,160 --> 00:04:39,680 So the first row would be number one. 86 00:04:39,680 --> 00:04:43,620 And the second row would be row number two, so on and so forth. 87 00:04:43,620 --> 00:04:47,620 >> But in computer science and CS50, we are zero-indexed. 88 00:04:47,620 --> 00:04:50,750 So we actually start counting at zero. 89 00:04:50,750 --> 00:04:54,020 So the first row would be row number zero. 90 00:04:54,020 --> 00:04:56,640 And the second row would be row number one. 91 00:04:56,640 --> 00:05:02,510 So if I had a height of eight for my pyramid, then the last value of n 92 00:05:02,510 --> 00:05:05,850 would actually be seven and not eight. 93 00:05:05,850 --> 00:05:07,010 >> So be careful about this. 94 00:05:07,010 --> 00:05:10,750 And be aware when you're determining your pattern whether your zero-indexed 95 00:05:10,750 --> 00:05:12,360 or one-indexed 96 00:05:12,360 --> 00:05:13,070 >> OK. 97 00:05:13,070 --> 00:05:15,380 So now that we have the pattern for the left pyramid, 98 00:05:15,380 --> 00:05:18,650 we need to determine the pattern for the gap. 99 00:05:18,650 --> 00:05:20,480 Luckily, this is really easy. 100 00:05:20,480 --> 00:05:22,690 It's just always two spaces. 101 00:05:22,690 --> 00:05:25,240 >> So now we proceed to the right pattern. 102 00:05:25,240 --> 00:05:27,220 The first row will have one hash. 103 00:05:27,220 --> 00:05:28,640 The second row, two. 104 00:05:28,640 --> 00:05:29,530 Third row, three. 105 00:05:29,530 --> 00:05:30,790 So on and so forth. 106 00:05:30,790 --> 00:05:35,540 So again, determine for any abstract n and any height how many hashes 107 00:05:35,540 --> 00:05:38,810 and how many spaces each row should have. 108 00:05:38,810 --> 00:05:39,700 >> OK. 109 00:05:39,700 --> 00:05:45,050 So we know that for each row we need to conduct some process. 110 00:05:45,050 --> 00:05:46,600 How do we do that? 111 00:05:46,600 --> 00:05:50,440 Well, we use the for loop construct, comprised of an initialization, 112 00:05:50,440 --> 00:05:52,310 a condition, and an update. 113 00:05:52,310 --> 00:05:54,830 For loops can be used to repeat processes. 114 00:05:54,830 --> 00:05:59,610 >> So say I want it to say hello, world 50 times, then my for loop 115 00:05:59,610 --> 00:06:03,510 would look something like this, where I initialize my variable to zero. 116 00:06:03,510 --> 00:06:06,200 The condition is that i is less than 50. 117 00:06:06,200 --> 00:06:09,670 And then the update is that it increments by one every time. 118 00:06:09,670 --> 00:06:14,700 So what this would do would be to print hello, world 50 times in a row. 119 00:06:14,700 --> 00:06:18,080 >> Now, say I wanted to iterate over the height of the pyramid. 120 00:06:18,080 --> 00:06:21,560 Then instead of hard coding some value in the condition, 121 00:06:21,560 --> 00:06:23,280 I just use the variable height. 122 00:06:23,280 --> 00:06:27,180 So what this will do is iterate over the height each row. 123 00:06:27,180 --> 00:06:30,280 And I can do something inside of the body of that loop. 124 00:06:30,280 --> 00:06:32,710 >> What do we want to do inside the body of the loop? 125 00:06:32,710 --> 00:06:36,550 Well, as we indicated before, we want to print the spaces and the hashes 126 00:06:36,550 --> 00:06:40,730 for the left pyramid and then print two spaces and then print the hashes. 127 00:06:40,730 --> 00:06:42,500 So we've already figured that out. 128 00:06:42,500 --> 00:06:45,670 So we can start to fill in our program a lot more. 129 00:06:45,670 --> 00:06:49,850 >> Here I have an outer for loop that iterates over every row in the pyramid. 130 00:06:49,850 --> 00:06:53,340 And inside that body I'm going to print spaces repeatedly, 131 00:06:53,340 --> 00:06:56,860 hashes repeatedly, and then the gap, and then hashes for the right pyramid, 132 00:06:56,860 --> 00:07:00,440 and then, finally, a new line to start the next row. 133 00:07:00,440 --> 00:07:02,860 >> With that, we've prompted the user for the input. 134 00:07:02,860 --> 00:07:04,510 We've made sure that it's valid. 135 00:07:04,510 --> 00:07:06,280 And then we've drawn the pyramid. 136 00:07:06,280 --> 00:07:09,540 So Mario can successfully climb the pyramid. 137 00:07:09,540 --> 00:07:10,970 >> My name is Zamyla. 138 00:07:10,970 --> 00:07:13,840 And this is CS50. 139 00:07:13,840 --> 00:07:15,419