All language subtitles for 2. Recursive Function - Total Numbers Less Than Num - Solution

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
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 Download
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,680 --> 00:00:07,700 All right, so now that you've tried to solve it on your own, let's do this together. 2 00:00:08,660 --> 00:00:16,290 And the first step that we need to to take is to understand the signature of the function. 3 00:00:16,940 --> 00:00:26,060 So let's say that we will decide on a name, let's say total smaller, let's call this function and 4 00:00:26,240 --> 00:00:28,040 what this function should return. 5 00:00:28,190 --> 00:00:33,280 Basically, we know that these function returns some count value, right? 6 00:00:33,290 --> 00:00:35,790 Either it's zero one, two or three and so on. 7 00:00:36,140 --> 00:00:41,570 So the type of the function is going to be int and we will say total smaller. 8 00:00:41,720 --> 00:00:49,310 OK, and the function, of course, is going to receive just in this case, one integer value and it 9 00:00:49,310 --> 00:00:53,750 is going to be not so in total smaller int num. 10 00:00:55,190 --> 00:00:55,740 Awesome. 11 00:00:56,390 --> 00:01:02,770 And now what we are going to do is to read OK, and basically to understand what we want to do. 12 00:01:03,380 --> 00:01:11,950 We want to read from the user some values right up until the user inserts minus one. 13 00:01:12,410 --> 00:01:19,340 But we cannot do it in some approach like this do while or while we cannot use the interactive approach, 14 00:01:19,340 --> 00:01:24,350 we have to use some recursive concept. 15 00:01:25,260 --> 00:01:35,820 And that's basically guys, where a lot of students have a hard time and also I as I remember myself, 16 00:01:35,820 --> 00:01:40,590 I really had it like like it was not it was not so trivial. 17 00:01:40,590 --> 00:01:44,430 It was not so easy to to grasp this concept at first. 18 00:01:44,430 --> 00:01:47,670 But still, there is nothing we can do. 19 00:01:48,900 --> 00:01:53,270 So we need to somehow to solve it together. 20 00:01:54,540 --> 00:01:56,520 So let's try to think about it. 21 00:01:56,520 --> 00:02:05,580 And let's start with the fact that we will try to basically read from the user, OK, on every recursive 22 00:02:05,580 --> 00:02:08,680 call, we will try to read some value. 23 00:02:08,700 --> 00:02:10,020 OK, we'll start. 24 00:02:10,020 --> 00:02:11,140 Let's start ups. 25 00:02:11,220 --> 00:02:12,790 Let's start with this one. 26 00:02:13,140 --> 00:02:14,910 OK, so that's the recursive call. 27 00:02:14,910 --> 00:02:24,090 And we will see or here an example like I don't know, let's call it first first call and we will read 28 00:02:24,210 --> 00:02:27,310 on this call some input from the user. 29 00:02:27,390 --> 00:02:29,700 OK, so we will read it. 30 00:02:30,030 --> 00:02:31,530 Look what happened. 31 00:02:32,070 --> 00:02:32,470 OK. 32 00:02:32,520 --> 00:02:33,090 Oh OK. 33 00:02:33,360 --> 00:02:40,740 So we will read it so into the input user and we will specify some descriptive message. 34 00:02:41,010 --> 00:02:52,680 Please enter a number, OK, and then we will read this information into this input user variable. 35 00:02:52,690 --> 00:02:54,420 So that user awesome. 36 00:02:55,530 --> 00:02:59,400 And now what we have to ask is basically he's basically. 37 00:03:01,490 --> 00:03:09,440 Let's let's consider the the terminating condition, which is minus one, so if the input user input 38 00:03:09,440 --> 00:03:15,430 user equals two minus one, then what can we conclude out of it? 39 00:03:15,890 --> 00:03:26,120 We can say that if the user has inserted minus one, then the contribution of this input of these given 40 00:03:26,120 --> 00:03:34,850 input to the total number of values smaller than this number, what is the contribution? 41 00:03:35,240 --> 00:03:36,110 It's zero. 42 00:03:36,140 --> 00:03:36,470 Right. 43 00:03:36,470 --> 00:03:38,810 We don't take it into consideration. 44 00:03:38,820 --> 00:03:42,320 So that's why we will return zero. 45 00:03:42,560 --> 00:03:47,480 OK, if the input user equals to minus one, then we don't care about it. 46 00:03:48,380 --> 00:04:00,560 But otherwise, if the input of the user, if the input of the user, if input user is less than num, 47 00:04:01,790 --> 00:04:12,440 then in this case this recursive call to this function should add one one, a value of one to the total 48 00:04:12,440 --> 00:04:15,140 numbers smaller than the non value. 49 00:04:15,770 --> 00:04:23,660 And it should look like this to return one plus the next recursive call. 50 00:04:23,660 --> 00:04:28,280 So total smaller in using this num. 51 00:04:29,750 --> 00:04:38,030 So once again, if the input user in this specific function goal in this specific recursive call is 52 00:04:38,030 --> 00:04:43,010 less than NUM, then we know that to the final result. 53 00:04:43,190 --> 00:04:49,790 This value should also these total numbers should take one plus whatever comes next. 54 00:04:50,190 --> 00:05:00,560 OK, but if nothing was on the input user was not less than num, it was num and above. 55 00:05:01,010 --> 00:05:08,600 Then in this case we don't want to take it into consideration because we don't want to calculate the 56 00:05:08,600 --> 00:05:19,070 total numbers, which are not smaller than that in this case, to somehow to to relate and to call for 57 00:05:19,070 --> 00:05:20,510 the next recursive call. 58 00:05:20,690 --> 00:05:30,560 We should do like something like this return total, total smaller for NUM without adding this one to 59 00:05:30,710 --> 00:05:32,420 the final result. 60 00:05:32,630 --> 00:05:41,120 OK, so that's basically the whole solution that you have for this exercise. 61 00:05:42,050 --> 00:05:50,060 And I know that's not the trivial way, and that's not so easy to grasp at first, but let's try to 62 00:05:50,060 --> 00:05:51,050 make some example. 63 00:05:51,050 --> 00:06:00,860 Let's say now equals to four and let's say we have like these numbers two, three and no, let's go 64 00:06:00,860 --> 00:06:04,220 like two, five and three. 65 00:06:04,520 --> 00:06:05,760 OK, and then minus one. 66 00:06:06,440 --> 00:06:13,610 And what I want us to do now is to simply to run all of these function again and again. 67 00:06:13,760 --> 00:06:20,690 OK, up until we see these full visualization of what is going on behind the scenes so that this way 68 00:06:20,720 --> 00:06:27,920 I hope and I think it will be much easier for you to grasp is what is going on here in this function. 69 00:06:27,950 --> 00:06:29,090 So let us start. 70 00:06:30,150 --> 00:06:34,730 And the first thing that we have to do is like to use our drawing thing. 71 00:06:34,770 --> 00:06:35,310 OK, so. 72 00:06:37,360 --> 00:06:46,390 Basically, basically, what will happen in the main function is very simple, we will simply like create 73 00:06:46,390 --> 00:06:56,470 a number and no equals to four and then we will create also some I don't know, total smaller numbers 74 00:06:56,860 --> 00:07:02,510 will say it will be equal to the total smaller function for this given number four. 75 00:07:02,800 --> 00:07:08,080 OK, so that's will that will be our first call to this function. 76 00:07:08,350 --> 00:07:15,490 And what we will do is that we will write down that this is the first call in this case num equals to 77 00:07:15,490 --> 00:07:16,450 four, right. 78 00:07:16,540 --> 00:07:18,480 NUM equals to four. 79 00:07:19,270 --> 00:07:26,230 And we come here and we ask the user, please enter a number, OK, please enter a number of ganef input 80 00:07:26,230 --> 00:07:26,630 user. 81 00:07:26,650 --> 00:07:29,080 So in this case, input user. 82 00:07:30,480 --> 00:07:37,770 So we have these Nomikos to for the input user, in this case, the input user, let's call it like 83 00:07:37,770 --> 00:07:41,940 input will be equal like we said here, will be equal to two. 84 00:07:42,060 --> 00:07:43,670 So input equals to two. 85 00:07:44,220 --> 00:07:46,500 And then we go through this next line. 86 00:07:46,510 --> 00:07:49,860 So if input user equals to minus one, no, that's not the case. 87 00:07:50,080 --> 00:07:58,770 If it's less than NUM, then this function, these function calls should return one plus the result 88 00:07:58,770 --> 00:08:01,830 that should be received from the call to the total. 89 00:08:01,830 --> 00:08:04,540 Smaller for this given num. 90 00:08:04,600 --> 00:08:06,990 So then we will have another call. 91 00:08:07,140 --> 00:08:07,530 Right. 92 00:08:07,530 --> 00:08:16,920 To calculate this value that will be like with num equal to for and input equal to five in this case. 93 00:08:16,920 --> 00:08:17,230 Right. 94 00:08:17,430 --> 00:08:18,420 That's the next call. 95 00:08:18,420 --> 00:08:21,210 So we called another call for, for this function. 96 00:08:21,210 --> 00:08:23,220 Total smaller number equal to four. 97 00:08:23,430 --> 00:08:26,490 We ask the user once again he answers the five. 98 00:08:26,760 --> 00:08:34,800 OK, so this function, these function call also checks, this condition false checks, this condition 99 00:08:34,800 --> 00:08:38,360 also false and it returns total smaller for NUM. 100 00:08:38,760 --> 00:08:42,360 So it should return this result should come to here. 101 00:08:42,630 --> 00:08:44,240 But what is this result? 102 00:08:44,490 --> 00:08:45,830 We still don't know it. 103 00:08:45,840 --> 00:08:49,200 We call another call for a function total smaller. 104 00:08:49,410 --> 00:08:52,470 Right return total smaller for num. 105 00:08:53,040 --> 00:09:00,270 So we know num equals two for and input here on this call will be get R. 106 00:09:00,390 --> 00:09:04,530 S three so input equals to three. 107 00:09:05,460 --> 00:09:09,660 And now what we will do is simply go again. 108 00:09:09,660 --> 00:09:12,830 If input is equals to minus one, return zero. 109 00:09:12,840 --> 00:09:13,740 That's not the case. 110 00:09:13,980 --> 00:09:15,970 If input user is less than num. 111 00:09:15,990 --> 00:09:17,980 OK, is this less than num. 112 00:09:18,000 --> 00:09:18,610 Yes. 113 00:09:18,900 --> 00:09:27,690 So this function should return to here one plus the result of the next call to total smaller and then 114 00:09:27,690 --> 00:09:37,320 we call this function again total smaller from the start with once again num equals two num equals num 115 00:09:37,320 --> 00:09:42,300 equals to four and we get an input again and the final input is minus one. 116 00:09:42,300 --> 00:09:46,800 So input equals two in this case two minus one. 117 00:09:47,190 --> 00:09:53,070 And if that's minus one, we know that if input user equals to minus one, then we return zero. 118 00:09:53,070 --> 00:09:56,700 Then from this function call we return zero. 119 00:09:57,120 --> 00:10:00,300 OK, that's zero and one plus zero. 120 00:10:00,300 --> 00:10:01,230 That's the result. 121 00:10:01,230 --> 00:10:04,440 We return also from here we return one. 122 00:10:04,450 --> 00:10:06,540 So one return to here is one. 123 00:10:06,960 --> 00:10:10,830 And from here we know that the results should be also one. 124 00:10:11,250 --> 00:10:12,660 So that's one plus one. 125 00:10:12,840 --> 00:10:19,890 And the final result that will be returned is two, which is exactly what we have expected, because 126 00:10:20,040 --> 00:10:23,130 that's the total numbers smaller than four. 127 00:10:23,160 --> 00:10:25,320 So which these two and three in this case. 128 00:10:25,710 --> 00:10:27,780 And that's what happening behind the scene. 129 00:10:29,460 --> 00:10:37,170 These are basically the recursion calls, the recursion function are recursive, for instance, that 130 00:10:37,290 --> 00:10:39,080 are called one after another. 131 00:10:39,090 --> 00:10:46,140 So you start with one function call, OK, from here, that's the first one you get into here and that's 132 00:10:46,140 --> 00:10:47,370 the unit you specify. 133 00:10:47,880 --> 00:10:55,700 You run all the commands and you want to already return one plus something, but that something is unknown. 134 00:10:56,070 --> 00:10:59,580 So that's why you call this function once again. 135 00:10:59,580 --> 00:11:01,710 And then that's the second instance. 136 00:11:01,950 --> 00:11:07,740 And you run it once again from the start and you find the result and you know, OK, I can return it. 137 00:11:08,010 --> 00:11:12,720 I can't return the total smaller that will be received from the next call. 138 00:11:12,990 --> 00:11:19,320 And then you go again, again, again, until you reach some stopping condition when you don't have 139 00:11:19,320 --> 00:11:22,690 to make any additional recursive calls. 140 00:11:23,370 --> 00:11:30,860 In this case, you simply return zero and you build your way, let's say, down or up, however you 141 00:11:30,960 --> 00:11:34,860 look at it to return your final result. 142 00:11:34,890 --> 00:11:46,800 OK, so take your time, compare your result with my result and make some conclusions for that and see 143 00:11:46,800 --> 00:11:50,370 if everything was working correctly in your case. 144 00:11:50,370 --> 00:11:53,700 If not, also try to fix that. 145 00:11:53,700 --> 00:12:01,170 Save the stopping condition is OK if the logic behind the recursive call is all right, if also there 146 00:12:01,170 --> 00:12:06,270 was no endless calls and. 147 00:12:06,600 --> 00:12:06,980 Yeah. 148 00:12:07,320 --> 00:12:08,910 So this is for this video. 149 00:12:08,910 --> 00:12:16,080 Guys, I hope you got as much information as you as you want it. 150 00:12:16,530 --> 00:12:18,900 And until next time I'll see you then. 151 00:12:19,050 --> 00:12:23,850 Please also leave some radio, some feedback to let me know if you like this video. 152 00:12:24,400 --> 00:12:26,210 And until then, my name is Lord. 153 00:12:26,250 --> 00:12:27,180 This is Alphatech. 154 00:12:27,180 --> 00:12:27,990 I'll see you then. 14574

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