Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,426 --> 00:00:02,370
In this lecture,
2
00:00:02,370 --> 00:00:05,500
we're gonna talk about solving problems.
3
00:00:05,500 --> 00:00:08,630
Solving problems is one of the most important things
4
00:00:08,630 --> 00:00:10,320
in programming.
5
00:00:10,320 --> 00:00:13,760
So if your goal is to become a great programmer,
6
00:00:13,760 --> 00:00:17,363
you need to learn how to deal with problems effectively.
7
00:00:18,260 --> 00:00:19,410
So in this video,
8
00:00:19,410 --> 00:00:22,270
I will show you how to think as a developer,
9
00:00:22,270 --> 00:00:23,690
by solving problems,
10
00:00:23,690 --> 00:00:26,583
using a simple four step framework.
11
00:00:27,900 --> 00:00:31,430
And let's use our friend John again.
12
00:00:31,430 --> 00:00:34,970
So after some time John can code now,
13
00:00:34,970 --> 00:00:37,700
he is almost job ready at this point,
14
00:00:37,700 --> 00:00:41,940
but he still needs to work on his problem solving skills.
15
00:00:41,940 --> 00:00:43,170
And in programming,
16
00:00:43,170 --> 00:00:45,310
when we say solving problems,
17
00:00:45,310 --> 00:00:47,150
we mean a real problem
18
00:00:47,150 --> 00:00:49,240
that needs a real solution.
19
00:00:49,240 --> 00:00:50,460
For example,
20
00:00:50,460 --> 00:00:53,070
in an array of GPS coordinates
21
00:00:53,070 --> 00:00:55,670
find the two closest points,
22
00:00:55,670 --> 00:00:59,180
and this problem is not straight forward, right?
23
00:00:59,180 --> 00:01:02,760
There are a lot of steps involved in solving this.
24
00:01:02,760 --> 00:01:04,230
And so we need a way
25
00:01:04,230 --> 00:01:07,600
to effectively solve problems like this one,
26
00:01:07,600 --> 00:01:10,440
or even way more complex ones.
27
00:01:10,440 --> 00:01:12,960
So problem solving does not mean
28
00:01:12,960 --> 00:01:16,660
to fix coding mistakes or bu, all right?
29
00:01:16,660 --> 00:01:19,720
Anyway, whenever John encounters a problem
30
00:01:19,720 --> 00:01:21,320
that he needs to solve,
31
00:01:21,320 --> 00:01:23,630
he usually just jumps at the problem
32
00:01:23,630 --> 00:01:25,720
without much thinking.
33
00:01:25,720 --> 00:01:27,980
He also implements his solutions
34
00:01:27,980 --> 00:01:29,860
in an unstructured way,
35
00:01:29,860 --> 00:01:32,460
without much of a logical approach.
36
00:01:32,460 --> 00:01:34,360
This makes him stressed out
37
00:01:34,360 --> 00:01:37,660
when things don't work right away.
38
00:01:37,660 --> 00:01:39,510
Another problem that John has
39
00:01:39,510 --> 00:01:42,180
is that he is too proud to research
40
00:01:42,180 --> 00:01:44,280
when he doesn't know how to come up
41
00:01:44,280 --> 00:01:46,390
with his own solution.
42
00:01:46,390 --> 00:01:49,830
So all of this is not effective at all
43
00:01:49,830 --> 00:01:52,900
and makes John waste a lot of time.
44
00:01:52,900 --> 00:01:54,660
Now, the first fix to this
45
00:01:54,660 --> 00:01:57,740
is to always stay calm and slow down
46
00:01:57,740 --> 00:02:00,130
and not just jump at a solution
47
00:02:00,130 --> 00:02:01,723
without having a plan.
48
00:02:02,600 --> 00:02:04,830
Also, when solving problems,
49
00:02:04,830 --> 00:02:06,500
you need to be in a mindset
50
00:02:06,500 --> 00:02:09,950
of taking a logical and rational approach,
51
00:02:09,950 --> 00:02:14,500
after all programming is just logic, all right?
52
00:02:14,500 --> 00:02:17,130
Then when you are in the right mindset,
53
00:02:17,130 --> 00:02:19,430
just use my four step framework
54
00:02:19,430 --> 00:02:21,163
to solve the problem at hand.
55
00:02:22,140 --> 00:02:24,180
And the first step of the framework
56
00:02:24,180 --> 00:02:25,013
is to make sure
57
00:02:25,013 --> 00:02:28,240
that you 100% understand the problem.
58
00:02:28,240 --> 00:02:31,000
Step back and take a high-level look
59
00:02:31,000 --> 00:02:32,500
at the big picture.
60
00:02:32,500 --> 00:02:34,850
And the most important part of the step
61
00:02:34,850 --> 00:02:37,080
is to ask the right questions
62
00:02:37,080 --> 00:02:39,020
in order to get a clear picture
63
00:02:39,020 --> 00:02:40,870
of the whole problem.
64
00:02:40,870 --> 00:02:43,140
And the best way of understanding this
65
00:02:43,140 --> 00:02:46,193
is probably to look at an example together here.
66
00:02:47,200 --> 00:02:49,940
So let's say you are working at some company
67
00:02:49,940 --> 00:02:51,330
on some project,
68
00:02:51,330 --> 00:02:54,250
and then your project manager comes and tells you,
69
00:02:54,250 --> 00:02:56,690
we need a function that reverses
70
00:02:56,690 --> 00:02:59,510
whatever we pass into it.
71
00:02:59,510 --> 00:03:01,990
And that's all you know about the problem,
72
00:03:01,990 --> 00:03:04,530
but do you understand it 100%?
73
00:03:04,530 --> 00:03:07,260
Well, probably not, right?
74
00:03:07,260 --> 00:03:09,240
I know that I don't.
75
00:03:09,240 --> 00:03:13,470
So let's ask the right questions to get there.
76
00:03:13,470 --> 00:03:15,010
First we can ask,
77
00:03:15,010 --> 00:03:18,540
what does whatever actually mean in this context?
78
00:03:18,540 --> 00:03:21,810
So what should actually be reversed here?
79
00:03:21,810 --> 00:03:24,220
Well, and if we think about it,
80
00:03:24,220 --> 00:03:25,360
it only makes sense
81
00:03:25,360 --> 00:03:29,130
to reverse strings, numbers and arrays.
82
00:03:29,130 --> 00:03:31,930
Objects don't have a well-defined order.
83
00:03:31,930 --> 00:03:33,960
So we can't reverse them.
84
00:03:33,960 --> 00:03:35,790
We're also not gonna reverse
85
00:03:35,790 --> 00:03:40,159
like undefined or no, or a Boolean, right?
86
00:03:40,159 --> 00:03:43,200
Then after we have that figured out,
87
00:03:43,200 --> 00:03:44,520
we can ask,
88
00:03:44,520 --> 00:03:46,740
what should we do if something else
89
00:03:46,740 --> 00:03:48,060
is passed in
90
00:03:48,060 --> 00:03:51,160
that is not a string, number or array?
91
00:03:51,160 --> 00:03:53,270
How are we gonna handle that?
92
00:03:53,270 --> 00:03:55,120
It's also relevant to ask,
93
00:03:55,120 --> 00:03:58,360
what exactly should be returned from the function?
94
00:03:58,360 --> 00:03:59,390
For example,
95
00:03:59,390 --> 00:04:01,260
should it always be a string
96
00:04:01,260 --> 00:04:03,010
or should the type be the same
97
00:04:03,010 --> 00:04:05,160
as what's passed in?
98
00:04:05,160 --> 00:04:06,240
Well, in this case,
99
00:04:06,240 --> 00:04:07,900
probably we should always
100
00:04:07,900 --> 00:04:10,220
just returned the exact same type
101
00:04:10,220 --> 00:04:11,820
that was passed in.
102
00:04:11,820 --> 00:04:15,940
Also we can start to ask more solution oriented questions,
103
00:04:15,940 --> 00:04:17,820
like how to recognize
104
00:04:17,820 --> 00:04:18,820
whether the argument
105
00:04:18,820 --> 00:04:19,880
is a number,
106
00:04:19,880 --> 00:04:22,280
a string or an array,
107
00:04:22,280 --> 00:04:24,750
or how to actually reverse a number
108
00:04:24,750 --> 00:04:27,230
a string and an array.
109
00:04:27,230 --> 00:04:29,510
So these are just some of the questions
110
00:04:29,510 --> 00:04:31,320
that we can ask here.
111
00:04:31,320 --> 00:04:32,710
I'm sure there are more,
112
00:04:32,710 --> 00:04:34,460
but this should already give us
113
00:04:34,460 --> 00:04:37,993
a way more clear picture of this problem.
114
00:04:38,920 --> 00:04:39,930
Now, right now,
115
00:04:39,930 --> 00:04:41,600
at the beginning of your journey,
116
00:04:41,600 --> 00:04:43,610
you will probably not yet know
117
00:04:43,610 --> 00:04:46,510
what the right questions actually are,
118
00:04:46,510 --> 00:04:47,520
but don't worry.
119
00:04:47,520 --> 00:04:50,643
This will come with time and practice, I promise.
120
00:04:52,270 --> 00:04:55,360
The next step is probably the most important one,
121
00:04:55,360 --> 00:04:58,820
which is the divide and conquer strategy.
122
00:04:58,820 --> 00:05:02,130
Divide and conquer means to break up the big problem
123
00:05:02,130 --> 00:05:05,300
into as many small problems as possible
124
00:05:05,300 --> 00:05:07,170
because these small problems
125
00:05:07,170 --> 00:05:09,940
are then a lot easier to solve.
126
00:05:09,940 --> 00:05:13,180
And this also ties in with the previous step
127
00:05:13,180 --> 00:05:16,470
because by dividing the problem into sub-problems,
128
00:05:16,470 --> 00:05:18,880
you also take the big problem apart
129
00:05:18,880 --> 00:05:21,690
until you understand it exactly.
130
00:05:21,690 --> 00:05:23,730
So based on the previous step,
131
00:05:23,730 --> 00:05:26,130
if we wanted to break down this problem,
132
00:05:26,130 --> 00:05:29,500
we could define a couple of sub-problems.
133
00:05:29,500 --> 00:05:32,460
First, we need to check if the argument is a number,
134
00:05:32,460 --> 00:05:34,840
a string or an array,
135
00:05:34,840 --> 00:05:36,360
and this is in fact,
136
00:05:36,360 --> 00:05:38,930
a small sub-problem, right?
137
00:05:38,930 --> 00:05:42,000
So now, we can just solve this one in isolation
138
00:05:42,000 --> 00:05:43,650
and then move on.
139
00:05:43,650 --> 00:05:46,190
The next sub-problem is to implement,
140
00:05:46,190 --> 00:05:47,760
reversing a number,
141
00:05:47,760 --> 00:05:50,360
then implement reversing a string,
142
00:05:50,360 --> 00:05:53,060
and then implement reversing an array
143
00:05:53,060 --> 00:05:54,610
so that we are ready to deal
144
00:05:54,610 --> 00:05:57,270
whatever is passed into the function.
145
00:05:57,270 --> 00:06:00,120
Finally, we then also need of course,
146
00:06:00,120 --> 00:06:03,210
to return the reversed value.
147
00:06:03,210 --> 00:06:05,203
So these are our sub-problems
148
00:06:05,203 --> 00:06:08,770
and they kind of look like a task list
149
00:06:08,770 --> 00:06:11,860
that we now need to go ahead and implement.
150
00:06:11,860 --> 00:06:13,100
And this is great
151
00:06:13,100 --> 00:06:16,627
because it makes our work so much easier now.
152
00:06:16,627 --> 00:06:18,450
Now sure.
153
00:06:18,450 --> 00:06:21,220
This example is a pretty simple problem,
154
00:06:21,220 --> 00:06:23,720
but this strategy works just as well
155
00:06:23,720 --> 00:06:27,340
for bigger and way more complex problems.
156
00:06:27,340 --> 00:06:29,260
In fact, divide and conquer
157
00:06:29,260 --> 00:06:32,290
is an essential method of problem solving
158
00:06:32,290 --> 00:06:35,223
that is also used a lot outside of programming.
159
00:06:36,120 --> 00:06:37,640
Anyway, with this,
160
00:06:37,640 --> 00:06:39,410
we have actually already covered
161
00:06:39,410 --> 00:06:42,810
the two most important steps of the framework,
162
00:06:42,810 --> 00:06:44,010
but now let's say
163
00:06:44,010 --> 00:06:46,060
that we actually do not know
164
00:06:46,060 --> 00:06:48,720
how to implement one of these sub-problems.
165
00:06:48,720 --> 00:06:50,790
Well, in that case,
166
00:06:50,790 --> 00:06:52,490
we should not be afraid
167
00:06:52,490 --> 00:06:55,730
of doing as much research as we have to.
168
00:06:55,730 --> 00:06:56,800
Now, of course,
169
00:06:56,800 --> 00:06:59,060
we should always first try to implement
170
00:06:59,060 --> 00:07:01,140
a sub-problem on our own
171
00:07:01,140 --> 00:07:04,140
using our own coding abilities.
172
00:07:04,140 --> 00:07:06,140
But if we're constantly hitting a wall
173
00:07:06,140 --> 00:07:07,880
and cannot move on,
174
00:07:07,880 --> 00:07:09,700
then we should waste no more time
175
00:07:09,700 --> 00:07:13,590
and just find out how it works using Google
176
00:07:13,590 --> 00:07:16,240
or a questions and answers websites
177
00:07:16,240 --> 00:07:18,130
like Stack Overflow
178
00:07:18,130 --> 00:07:21,430
or the MDN JavaScript documentation.
179
00:07:21,430 --> 00:07:24,520
You will see how to use these in the next lecture
180
00:07:24,520 --> 00:07:25,880
when we're gonna work together
181
00:07:25,880 --> 00:07:27,800
on a real problem.
182
00:07:27,800 --> 00:07:29,600
And actually researching
183
00:07:29,600 --> 00:07:32,630
is a huge part of a programmer's job,
184
00:07:32,630 --> 00:07:35,240
as you will find out very soon.
185
00:07:35,240 --> 00:07:37,630
So you really should not be ashamed
186
00:07:37,630 --> 00:07:39,120
when you don't know enough
187
00:07:39,120 --> 00:07:40,730
to solve a certain problem.
188
00:07:40,730 --> 00:07:43,090
That's just completely normal.
189
00:07:43,090 --> 00:07:43,960
Now, in this case,
190
00:07:43,960 --> 00:07:46,710
some questions that we could Google are,
191
00:07:46,710 --> 00:07:49,150
how to check if a value is a number
192
00:07:49,150 --> 00:07:50,230
or an array
193
00:07:50,230 --> 00:07:52,430
or a string in JavaScript,
194
00:07:52,430 --> 00:07:54,950
or we could Google how to reverse a number
195
00:07:54,950 --> 00:07:56,970
or a string or an array
196
00:07:56,970 --> 00:07:58,920
in JavaScript, okay?
197
00:07:58,920 --> 00:08:01,650
And again, we will do this in the next lecture
198
00:08:01,650 --> 00:08:03,363
on a real problem together.
199
00:08:04,380 --> 00:08:08,210
Finally, in case we are trying to solve bigger problems,
200
00:08:08,210 --> 00:08:10,850
we should put all the previous steps together
201
00:08:10,850 --> 00:08:13,860
and write some so-called pseudo-code
202
00:08:13,860 --> 00:08:16,410
before writing the actual code.
203
00:08:16,410 --> 00:08:17,450
And pseudo-code
204
00:08:17,450 --> 00:08:20,050
is simply an informal description
205
00:08:20,050 --> 00:08:23,020
of the actual code that we're gonna write.
206
00:08:23,020 --> 00:08:24,020
So it's like code
207
00:08:24,020 --> 00:08:27,290
for humans to understand not computers.
208
00:08:27,290 --> 00:08:28,530
For our example,
209
00:08:28,530 --> 00:08:32,140
the pseudo-code might look something like this.
210
00:08:32,140 --> 00:08:33,030
So you see,
211
00:08:33,030 --> 00:08:36,310
we use some structures of a programming language,
212
00:08:36,310 --> 00:08:39,940
but this is clearly not JavaScript, right?
213
00:08:39,940 --> 00:08:42,550
And there are no real rules
214
00:08:42,550 --> 00:08:44,780
on how to write pseudo-code either.
215
00:08:44,780 --> 00:08:47,960
You just write so that you understand it yourself
216
00:08:47,960 --> 00:08:50,260
or other people on your team.
217
00:08:50,260 --> 00:08:51,400
So in this example,
218
00:08:51,400 --> 00:08:55,180
we just start by naming the function as a reverse,
219
00:08:55,180 --> 00:08:58,270
and then we basically pass in a value.
220
00:08:58,270 --> 00:08:59,220
Then we say,
221
00:08:59,220 --> 00:09:00,730
if the type of the value
222
00:09:00,730 --> 00:09:01,860
is not a string
223
00:09:01,860 --> 00:09:04,930
or not a number or not an array,
224
00:09:04,930 --> 00:09:07,160
then simply return the value back.
225
00:09:07,160 --> 00:09:09,930
And that's because as we defined previously,
226
00:09:09,930 --> 00:09:11,510
we only want this function to work
227
00:09:11,510 --> 00:09:13,690
for strings or for numbers
228
00:09:13,690 --> 00:09:15,720
or for arrays.
229
00:09:15,720 --> 00:09:17,910
And again, you can clearly see that
230
00:09:17,910 --> 00:09:21,255
this is not any valid JavaScript at all,
231
00:09:21,255 --> 00:09:22,750
but anyway,
232
00:09:22,750 --> 00:09:26,560
if the type of the value passed this initial test,
233
00:09:26,560 --> 00:09:27,393
then we say,
234
00:09:27,393 --> 00:09:29,490
if the value is a string,
235
00:09:29,490 --> 00:09:30,950
then reverse the string.
236
00:09:30,950 --> 00:09:32,480
If the value is a number,
237
00:09:32,480 --> 00:09:34,090
then reverse the number.
238
00:09:34,090 --> 00:09:37,230
And if the value is an array,
239
00:09:37,230 --> 00:09:39,350
then simply reverse your array.
240
00:09:39,350 --> 00:09:40,183
And then at the end,
241
00:09:40,183 --> 00:09:43,610
we want to return the reversed value, okay?
242
00:09:43,610 --> 00:09:46,720
So this is really more like a list of instructions
243
00:09:46,720 --> 00:09:48,270
that we now could go ahead
244
00:09:48,270 --> 00:09:50,120
and actually implement
245
00:09:50,120 --> 00:09:52,371
using the JavaScript language.
246
00:09:52,371 --> 00:09:54,500
Okay, then that's it.
247
00:09:54,500 --> 00:09:57,050
That's the four steps to solve any problem,
248
00:09:57,050 --> 00:09:59,290
no matter how big.
249
00:09:59,290 --> 00:10:01,040
And now before I leave you,
250
00:10:01,040 --> 00:10:03,520
a final tip I can give you
251
00:10:03,520 --> 00:10:07,270
is to develop a genuine curiosity and passion
252
00:10:07,270 --> 00:10:10,420
for understanding how things actually work,
253
00:10:10,420 --> 00:10:12,180
not only in programming,
254
00:10:12,180 --> 00:10:15,240
but really in the whole world around you.
255
00:10:15,240 --> 00:10:16,073
For example,
256
00:10:16,073 --> 00:10:18,400
the famous scientist Richard Feynman
257
00:10:18,400 --> 00:10:19,790
was repairing radios
258
00:10:19,790 --> 00:10:21,770
when he was just a teenager
259
00:10:21,770 --> 00:10:24,760
and Elon Musk made his first computer game
260
00:10:24,760 --> 00:10:26,450
when he was 12.
261
00:10:26,450 --> 00:10:28,990
And there are many more examples like this,
262
00:10:28,990 --> 00:10:32,200
and all of them became geniuses in their fields
263
00:10:32,200 --> 00:10:35,690
in part because of their genuine curiosity
264
00:10:35,690 --> 00:10:37,953
on how the world around them works.
265
00:10:38,870 --> 00:10:41,190
All right, and with that being said,
266
00:10:41,190 --> 00:10:43,530
let's now go ahead and solve a real problem
267
00:10:43,530 --> 00:10:44,810
in the next lecture,
268
00:10:44,810 --> 00:10:46,200
using this framework
269
00:10:46,200 --> 00:10:47,850
that we just learned about.
270
00:10:47,850 --> 00:10:49,033
So see you there.
18126
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.