All language subtitles for 052 [Interactive Coding Exercise] Adding Even Numbers.en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,120 --> 00:00:03,540 Click on 5.3 adding evens. 2 00:00:05,190 --> 00:00:10,190 The exercise is kind of similar to what Gauss had to do that I showed you in the 3 00:00:11,610 --> 00:00:13,890 last lesson, but in your case, 4 00:00:14,070 --> 00:00:19,070 I want you to calculate the sum of all the even numbers from 1 to 100, 5 00:00:20,310 --> 00:00:24,150 including 1 and 100. So for example, 6 00:00:24,150 --> 00:00:26,730 it might be two plus four plus six plus eight, 7 00:00:26,760 --> 00:00:29,640 and so on and so forth until you get to a hundred. 8 00:00:30,210 --> 00:00:34,260 It's a little bit more complicated than what the ten-year-old had to do. 9 00:00:34,680 --> 00:00:38,280 And the important thing to remember is that there should only be one print 10 00:00:38,280 --> 00:00:40,170 statement in your output. 11 00:00:40,410 --> 00:00:44,940 It should just print the final total and not every step of the calculation. 12 00:00:45,720 --> 00:00:48,270 So have a think about how you might solve this challenge, 13 00:00:48,660 --> 00:00:50,610 pause the video and try to give this a go. 14 00:00:56,450 --> 00:00:56,750 All right. 15 00:00:56,750 --> 00:01:00,590 So there's actually quite a few ways that you could have solved this challenge. 16 00:01:01,160 --> 00:01:01,400 Now, 17 00:01:01,400 --> 00:01:05,480 one of the ways would have been using what we learned from the last lesson and the 18 00:01:05,480 --> 00:01:09,680 ability to create a range function using a different step size. 19 00:01:09,920 --> 00:01:10,753 I'll show you what I mean. 20 00:01:11,150 --> 00:01:16,150 So let's create a full loop where we say for every number in the range between 21 00:01:16,970 --> 00:01:20,030 1 to 100, so that's 101, 22 00:01:20,720 --> 00:01:24,110 then we're going to do something with each number, right? Namely, 23 00:01:24,110 --> 00:01:27,200 we're probably going to create a variable called total 24 00:01:27,590 --> 00:01:32,590 and then we're going to add each of the even numbers together and accumulate it 25 00:01:32,930 --> 00:01:34,190 inside this variable. 26 00:01:34,940 --> 00:01:39,590 Now remember that I mentioned that the range function actually has a third thing 27 00:01:39,590 --> 00:01:42,920 that you can add to it, not just the start and the end, 28 00:01:43,160 --> 00:01:47,210 but also the step size. so we can change the step size to two, 29 00:01:47,660 --> 00:01:52,660 and then now what it's going to do is it's going to go through 1 to 100 and 30 00:01:54,440 --> 00:01:57,230 it's going to increase the step size by two. 31 00:01:58,490 --> 00:02:01,820 So that means if we print this range as it is right now, 32 00:02:01,880 --> 00:02:06,560 it's going to give us one plus two which is three, plus two 33 00:02:06,560 --> 00:02:08,840 which is five and so on and so forth. 34 00:02:09,260 --> 00:02:12,560 But if we change the range to start off from two 35 00:02:12,710 --> 00:02:16,640 which is the first number that we have to start adding in our sequence, and then 36 00:02:16,640 --> 00:02:18,860 increase it by two every single time, 37 00:02:19,130 --> 00:02:23,570 then we end up with a range that includes all of the even numbers up to a 38 00:02:23,570 --> 00:02:26,510 hundred. And I'll show you if I run this code right now. 39 00:02:27,290 --> 00:02:32,000 So the next part is simple. All we have to do is to add the number to 40 00:02:32,000 --> 00:02:35,420 the current total. So total += number. 41 00:02:35,900 --> 00:02:38,570 And then finally outside the for loop, 42 00:02:38,720 --> 00:02:42,230 you can go ahead and print that total. Now, 43 00:02:42,380 --> 00:02:46,490 when I go ahead and run this code now, it's going to give me just the total. 44 00:02:46,850 --> 00:02:50,960 Now remember that the indentation really matters because if I have it like this, 45 00:02:51,260 --> 00:02:54,800 then it's going to give me the total for every step of the calculation, 46 00:02:55,100 --> 00:03:00,100 because it's now included in the for loop and will be executed for as many times 47 00:03:00,910 --> 00:03:05,350 as the for loop runs. But if I remove the indentation, then the 48 00:03:05,350 --> 00:03:08,680 for loop is just this and this is outside of it 49 00:03:08,920 --> 00:03:12,160 so it's only going to give it to me once everything has been done 50 00:03:12,280 --> 00:03:16,360 and I've got the final total. And it's the correct solution. 51 00:03:17,140 --> 00:03:19,870 But you might have also done it in a different way. 52 00:03:20,200 --> 00:03:23,370 You might have said for a number in range, 53 00:03:24,090 --> 00:03:25,860 1 to 101, 54 00:03:26,820 --> 00:03:31,820 and you had some sort of another total, let's call it total2 55 00:03:34,200 --> 00:03:35,460 and started at zero, 56 00:03:35,880 --> 00:03:40,880 and then we say that if the number % 2 is equal to 0, 57 00:03:41,970 --> 00:03:44,490 so if it's even essentially, 58 00:03:44,730 --> 00:03:49,350 then we're going to add this number to the total2. 59 00:03:51,060 --> 00:03:54,480 Finally, outside of the if and outside of the for, 60 00:03:54,750 --> 00:03:58,620 we're going to print the final total. Now, 61 00:03:58,650 --> 00:04:01,140 if I just run the code, then you can see 62 00:04:01,200 --> 00:04:04,350 I get exactly the same number as before. 63 00:04:05,310 --> 00:04:09,240 So if you chose to solve this challenge in either of these ways, 64 00:04:09,270 --> 00:04:11,910 then you've succeeded. They're both right. 65 00:04:12,390 --> 00:04:15,180 And there's probably even other ways that you could solve this as well. 66 00:04:15,480 --> 00:04:18,570 But these two, I suspect will be the most popular approaches. 67 00:04:19,770 --> 00:04:24,150 Now in the next lesson, I've got another coding exercise for you 68 00:04:24,330 --> 00:04:29,160 and it's probably one of the top interview questions that programmers get asked 69 00:04:29,220 --> 00:04:34,020 when they're being interviewed for a job. So head over there and give it a go. 6677

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.