Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,960 --> 00:00:06,920
All right so hopefully you gave that challenge a good go and you managed to get the correct solution.
2
00:00:07,050 --> 00:00:13,590
Abiding by all of the rules now the rules are there because we want to test your understanding of variables
3
00:00:14,130 --> 00:00:20,370
and this particular exercise is something that you often see encoding interviews where they're trying
4
00:00:20,370 --> 00:00:25,930
to test a candidate's thinking process how they go about reasoning using code.
5
00:00:26,370 --> 00:00:32,010
So let's go through the solution together and see if you got it right now as I mentioned previously
6
00:00:32,100 --> 00:00:34,560
were not allowed to change any of the existing code.
7
00:00:34,620 --> 00:00:41,130
So the solution can't just be us changing this from three to eight and that from eight to three.
8
00:00:41,130 --> 00:00:42,690
This is wrong.
9
00:00:42,690 --> 00:00:48,390
The second rule is we're not allowed to type any numbers so we can't just simply say well net a now
10
00:00:48,390 --> 00:00:51,660
equals eight and b now equals three.
11
00:00:51,660 --> 00:00:55,650
This is also wrong because it breaks the second rule.
12
00:00:55,650 --> 00:00:59,660
Now the last rule is we're not allowed to re declare the variables A and B.
13
00:00:59,670 --> 00:01:08,030
So finally we can't say a equals eight and b variable B equals three.
14
00:01:08,280 --> 00:01:11,270
In this case we've actually just created two new variables.
15
00:01:11,310 --> 00:01:17,180
And you can see we're getting a little warning on the side and it says that a is ready to find b is
16
00:01:17,180 --> 00:01:18,640
ready defined up here.
17
00:01:18,660 --> 00:01:20,870
So we can't re declare them.
18
00:01:20,880 --> 00:01:25,920
So given all of those constraints how do we arrive at the answer.
19
00:01:26,340 --> 00:01:33,330
Well as I showed you earlier on it's really useful to think about it in terms of real objects to understand
20
00:01:33,330 --> 00:01:34,570
the logic.
21
00:01:34,590 --> 00:01:37,680
Now we said that we've got two variables right.
22
00:01:37,680 --> 00:01:42,300
A and B and we can imagine them as two buckets bucket and Bucket B.
23
00:01:42,300 --> 00:01:45,340
Now a contained son and B contains water.
24
00:01:45,390 --> 00:01:51,890
How would we switch the contents of the buckets around so that a hold water and b hold sand.
25
00:01:51,910 --> 00:01:58,230
Well if this was a real life situation then the simplest way is probably just getting hold of another
26
00:01:58,230 --> 00:01:59,410
bucket right.
27
00:01:59,580 --> 00:02:04,650
And this is exactly the same solution that we're going to implement using code as well.
28
00:02:04,650 --> 00:02:07,470
So always remember that coding is not the hard part.
29
00:02:07,620 --> 00:02:10,190
Remembering what the semicolons are equal sign.
30
00:02:10,200 --> 00:02:11,910
This is all very easy.
31
00:02:11,940 --> 00:02:15,180
The difficult part is getting the logic in the first place.
32
00:02:15,720 --> 00:02:21,990
So let's create this variable C and let's get it to hold some of the content that's currently inside
33
00:02:22,430 --> 00:02:23,790
a.
34
00:02:23,820 --> 00:02:28,850
And now that a is freed up we can use a to hold what's inside B.
35
00:02:28,920 --> 00:02:36,550
And finally we can get B to hold what is inside C which is what was previously inside a.
36
00:02:36,630 --> 00:02:39,750
So now let's go ahead and test our code.
37
00:02:39,750 --> 00:02:45,810
So I'm just going to copy these three lines and I'm going to paste it inside here and when I hit run
38
00:02:46,110 --> 00:02:52,680
you can see that now a is a and b is three and just these three lines of code allowed us to do the bucket
39
00:02:52,680 --> 00:02:59,690
switch and if we click check solution then everything should pass and we should be allowed to continue.
40
00:02:59,700 --> 00:03:05,580
So how did you get on with this challenge if you got really stuck and you don't understand why the solution
41
00:03:05,670 --> 00:03:09,780
is the way it is then I urge you to just think about the buckets.
42
00:03:09,780 --> 00:03:11,940
Think about how you would do it in real life.
43
00:03:11,940 --> 00:03:18,450
What is the logic of switching the contents between A and B around when you have a Bucket C and then
44
00:03:18,450 --> 00:03:22,360
the code is going to become a lot more easily understood.
45
00:03:22,370 --> 00:03:26,840
So if you need a moment pause a video and have a think about it and then once you're ready head over
46
00:03:26,850 --> 00:03:27,690
to the next lesson.
4694
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.