Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,850 --> 00:00:08,170
So what is going on, guys, and I've just looked at the previous exercise and and the previous solution
2
00:00:08,170 --> 00:00:09,190
that was provided.
3
00:00:09,610 --> 00:00:14,260
And I have been thinking to myself as to can we improve this solution?
4
00:00:14,270 --> 00:00:15,310
Yes or no?
5
00:00:15,880 --> 00:00:21,580
I mean, like it seems to be working just fine by splitting up every time for a smaller sleep problems
6
00:00:21,880 --> 00:00:29,590
and on every recursive call, taking some rightmost digit and comparing it with the results so far.
7
00:00:30,620 --> 00:00:37,460
And although this work just works just fine, I have been thinking if we can optimize it a little bit
8
00:00:37,460 --> 00:00:44,840
and reduce the number of lines of code that we use here in order to make these recursive call recursive
9
00:00:44,840 --> 00:00:47,330
function much smaller in code.
10
00:00:48,140 --> 00:00:51,590
So what I want you to think about is, first of all, these works just fine.
11
00:00:51,590 --> 00:00:55,460
Now we can try to think about how we can optimize it and minimize it.
12
00:00:56,450 --> 00:01:02,150
So a result so far, if the result so far is one right?
13
00:01:02,780 --> 00:01:10,010
So it will stay one if basically if we take a look at the right, most digit.
14
00:01:10,130 --> 00:01:17,480
So if the right, most digit will be even right, this indicates that the right most digit still remains
15
00:01:17,480 --> 00:01:22,400
even right, then the results so far will remain the same.
16
00:01:22,820 --> 00:01:28,820
And also, the results so far will have remained the same in this case, whenever the results so far
17
00:01:28,820 --> 00:01:29,840
is zero.
18
00:01:30,410 --> 00:01:35,960
So if the result so far was even or odd, OK doesn't really matter.
19
00:01:36,740 --> 00:01:46,220
And of the right most, the digit in this recursive call was what was even then the results so far,
20
00:01:46,220 --> 00:01:50,600
and this function will return the same value that it had previously.
21
00:01:50,600 --> 00:01:53,660
So if it was one, it still will will remain one.
22
00:01:53,960 --> 00:01:57,230
And if it was zero, it still will remain zero.
23
00:01:57,650 --> 00:02:02,600
OK, so what I'm trying to say is that the results so far, maybe even.
24
00:02:03,960 --> 00:02:10,620
Plus, the even right most digit will result in an even, and if it was odd plus in even which is the
25
00:02:10,620 --> 00:02:13,230
right most digit, it will still remain odd.
26
00:02:13,510 --> 00:02:15,480
OK, so it will not change the status.
27
00:02:16,230 --> 00:02:21,780
On the other hand, if we know that these else are relates to the fact that they're right, most digit
28
00:02:21,780 --> 00:02:22,500
is odd.
29
00:02:22,980 --> 00:02:27,340
Then if the result so far was one, it will change its status, OK?
30
00:02:27,360 --> 00:02:33,450
It will become zero, because if we know that the result so far is odd, and if we know that the result
31
00:02:33,450 --> 00:02:40,470
so far is even and we know that the right most digit is odd, then odd plus even will result in an odd
32
00:02:40,530 --> 00:02:41,190
value.
33
00:02:41,730 --> 00:02:45,990
And that's the same also for here, if it was an order value, right?
34
00:02:46,200 --> 00:02:49,110
The result so far was a sum that is odd.
35
00:02:49,380 --> 00:02:52,200
Plus, this else relates to the right most digit.
36
00:02:52,200 --> 00:02:58,730
When it's odd, then odd plus order will equal to even meaning we will need to change the status.
37
00:02:58,740 --> 00:03:01,980
The results so far, the return value from this function again.
38
00:03:02,670 --> 00:03:10,050
So what we'll try to do is basically to minimize this task, OK, and make it a little smaller with
39
00:03:10,050 --> 00:03:11,730
much less lines of code.
40
00:03:11,910 --> 00:03:14,010
OK, that's what I'm going to show you right now.
41
00:03:14,850 --> 00:03:21,690
So instead of using it like this, what we will ask is the following question we will ask if the right
42
00:03:21,690 --> 00:03:28,720
most digit like this, if it's the case right, then what we will need to do is simply too.
43
00:03:28,740 --> 00:03:31,600
Okay, I'm trying to build this thing together with you.
44
00:03:31,980 --> 00:03:33,840
If that's the case, right?
45
00:03:34,230 --> 00:03:40,770
If that's the case, then what we need to return is what do you think is just the results so far?
46
00:03:41,340 --> 00:03:43,100
Return result?
47
00:03:43,260 --> 00:03:44,520
So far, that's it.
48
00:03:45,060 --> 00:03:49,080
Because since the right, most digit is in even one.
49
00:03:49,350 --> 00:03:49,690
OK.
50
00:03:49,710 --> 00:03:52,020
There is no reason for us to change it.
51
00:03:53,120 --> 00:03:58,310
OK, but if it's not, then we will need simply to return what?
52
00:03:59,270 --> 00:04:01,330
We will simply need to return.
53
00:04:01,430 --> 00:04:02,990
Let me get it right here.
54
00:04:03,290 --> 00:04:06,800
We will simply need to return, not results so far.
55
00:04:07,790 --> 00:04:10,730
So what I've written here and I'm not finished yet.
56
00:04:11,510 --> 00:04:17,840
I said that if the rightmost digit will be even then returned the results so far because this does not
57
00:04:17,840 --> 00:04:20,450
affect the overall sum.
58
00:04:20,660 --> 00:04:25,370
If it was, if it was an even some, it will still remain even.
59
00:04:25,730 --> 00:04:28,310
And if it was odd, it will still remain odd.
60
00:04:29,120 --> 00:04:35,360
On the other hand, if the right was the it was odd, then switch from the results.
61
00:04:35,360 --> 00:04:38,210
So far, OK, not means here.
62
00:04:38,240 --> 00:04:40,130
Basically, if we use just ones and zeros.
63
00:04:40,430 --> 00:04:44,900
So if the result so far was zero, this not will turn it into one.
64
00:04:45,200 --> 00:04:50,360
And if the results so far was one, the Senate will change it to zero.
65
00:04:50,810 --> 00:04:52,630
OK, awesome.
66
00:04:52,640 --> 00:04:59,450
So now what I'm also going to do is to say that let's not using these results so far are variable.
67
00:04:59,750 --> 00:05:03,870
And instead of these results so far right, we know that this is the value.
68
00:05:04,310 --> 00:05:10,310
So instead of using it, we will simply change instead of results so far will specify the recursive
69
00:05:10,310 --> 00:05:10,610
call.
70
00:05:11,210 --> 00:05:11,580
OK.
71
00:05:11,600 --> 00:05:13,100
It's just the same.
72
00:05:13,910 --> 00:05:20,870
OK, so you see how the recursive call function, the recursive function has got much smaller, although
73
00:05:20,870 --> 00:05:24,740
I do not recommend to make this version right from the beginning.
74
00:05:25,010 --> 00:05:31,220
But after you practice the little bit of your exercises, I suggest creating the first initial version
75
00:05:31,220 --> 00:05:38,030
that we had previously OK with all these lines of code and then ask yourself a simple question Can you
76
00:05:38,030 --> 00:05:40,070
optimize this solution?
77
00:05:40,070 --> 00:05:46,880
Can you minimize the number of lines of code and the number of variables created in the process of the
78
00:05:46,880 --> 00:05:48,090
recursive function?
79
00:05:49,360 --> 00:05:50,530
And the answer is yes.
80
00:05:50,770 --> 00:05:51,090
OK.
81
00:05:51,910 --> 00:05:55,360
And now I want to also continue optimizing it.
82
00:05:55,390 --> 00:05:55,780
OK.
83
00:05:56,020 --> 00:05:58,930
I want to say that instead of using these four lines of code.
84
00:06:00,340 --> 00:06:02,710
What I will use is the following line of code.
85
00:06:03,130 --> 00:06:08,710
I will say return return in modular to.
86
00:06:10,060 --> 00:06:19,270
Equal to zero right, or B or C, let's use it like this, OK, if an equal to zero, then we will return
87
00:06:19,270 --> 00:06:19,810
one.
88
00:06:20,020 --> 00:06:20,410
Right?
89
00:06:20,500 --> 00:06:21,670
That's what we will return.
90
00:06:21,940 --> 00:06:23,860
Otherwise, we will return zero.
91
00:06:24,580 --> 00:06:32,020
So this line of code I simply am showing, I'm simply showing you a little another option to do that.
92
00:06:32,050 --> 00:06:35,430
OK, so return and modular two equals to zero.
93
00:06:35,440 --> 00:06:41,950
So basically, both of these two versions, these four lines and this one one on one line, they represent
94
00:06:41,950 --> 00:06:42,760
the same thing.
95
00:06:43,180 --> 00:06:44,980
So we ask if that's the case.
96
00:06:45,280 --> 00:06:47,800
Question mark, then return one.
97
00:06:48,040 --> 00:06:48,380
OK.
98
00:06:48,490 --> 00:06:50,590
The result for this condition is true.
99
00:06:50,590 --> 00:06:53,860
Then return one, otherwise return zero.
100
00:06:54,340 --> 00:06:56,530
OK, so that's basically what we have here.
101
00:06:57,580 --> 00:07:00,580
OK, and we can also further optimize it.
102
00:07:00,910 --> 00:07:05,140
Instead of saying it like this, we can say if and modular two.
103
00:07:06,040 --> 00:07:10,420
Does not equal to zero, then returns zero in one, I'm simply switching places.
104
00:07:10,690 --> 00:07:13,450
OK, follow up this like line by line, OK?
105
00:07:14,350 --> 00:07:15,780
I didn't do anything wrong.
106
00:07:15,790 --> 00:07:22,990
I just switched the places between the true result for this condition and the false result for this
107
00:07:22,990 --> 00:07:23,480
condition.
108
00:07:24,280 --> 00:07:26,560
And what I can ask is this simple question.
109
00:07:26,740 --> 00:07:33,320
I can also remove this not equal to zero, because that's how this condition is interpreted.
110
00:07:33,320 --> 00:07:35,200
It return in modular two.
111
00:07:35,830 --> 00:07:40,930
If that's the case, and if it doesn't equal to zero, then return zero, otherwise return one.
112
00:07:41,500 --> 00:07:45,190
Okay, so that's just short version of writing the code.
113
00:07:46,300 --> 00:07:48,950
And finally, I will do also the same here.
114
00:07:48,970 --> 00:07:56,710
I will say this line of code, I will say return, and I will say this part OK, if this part does not
115
00:07:56,710 --> 00:07:57,760
equal to zero.
116
00:07:57,970 --> 00:08:03,760
OK, so if that does not equal to zero, then return this part.
117
00:08:04,060 --> 00:08:09,550
OK, if there's an integral to zero on this one and otherwise return this option.
118
00:08:10,590 --> 00:08:11,190
That's it.
119
00:08:11,460 --> 00:08:15,750
So these four lines of code are equivalent to this line of code.
120
00:08:16,590 --> 00:08:18,240
So I will remove it in.
121
00:08:18,240 --> 00:08:22,410
Finally, the final version for these function looks like this.
122
00:08:23,250 --> 00:08:27,750
OK, so that's very nothing to do to read it first.
123
00:08:28,080 --> 00:08:30,340
But actually, that's the answer.
124
00:08:30,360 --> 00:08:34,830
That's the recursive function call optimized, minimized in lines of code.
125
00:08:35,250 --> 00:08:37,080
And yeah, OK.
126
00:08:37,080 --> 00:08:43,410
So you can also make sure that I've done everything correctly here by simply testing out with some examples
127
00:08:43,740 --> 00:08:50,700
and hopefully because the hour is late and make sure that it works exactly as you expect.
128
00:08:51,660 --> 00:08:58,080
And I really do recommend to start with the basic option that we've solved in previous videos.
129
00:08:58,320 --> 00:09:04,380
And only when you feel more confident to start trying to take a look at how you can reduce the number
130
00:09:04,380 --> 00:09:07,440
of lines of code and make it look like this, OK?
131
00:09:07,470 --> 00:09:10,940
So yeah, this is it for these video guys.
132
00:09:10,950 --> 00:09:12,270
Thank you so much for watching.
133
00:09:12,300 --> 00:09:13,410
Keep on practicing.
134
00:09:13,410 --> 00:09:14,940
Let me know what you think of it.
135
00:09:15,270 --> 00:09:19,950
And until the next time, I will see you in the next videos by.
12684
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.