1
00:00:05,380 --> 00:00:07,150
Welcome back everyone in this lecture.

2
00:00:07,180 --> 00:00:09,750
We're going to go through the solutions for the function tasks.

3
00:00:09,760 --> 00:00:10,700
Let's get started.

4
00:00:10,990 --> 00:00:11,230
OK.

5
00:00:11,230 --> 00:00:14,090
Let's begin by taking a look at task one.

6
00:00:14,110 --> 00:00:18,300
So the first one I wanted you to return a boolean to check if there's some of these two numbers was

7
00:00:18,310 --> 00:00:19,630
equal to 10.

8
00:00:19,720 --> 00:00:21,370
Often beginners will do something like this.

9
00:00:21,370 --> 00:00:32,930
They'll say if and one plus and two is equal to 10 return TRUE else return false.

10
00:00:32,930 --> 00:00:37,760
Now while this is a perfectly valid solution it's actually not the most efficient solution because remember

11
00:00:37,760 --> 00:00:43,700
just like we showed in some previous lectures this itself is already the boolean true or false.

12
00:00:43,700 --> 00:00:51,470
So instead of doing something like this you could just return there some if it's equal to 10 or not.

13
00:00:51,500 --> 00:00:53,050
So you could take the sum first.

14
00:00:53,090 --> 00:00:59,750
So we'll say OK and one plus and two and check if this sum is equal to the number 10 and I'll return

15
00:00:59,750 --> 00:01:00,190
a boolean.

16
00:01:00,200 --> 00:01:01,220
True or false.

17
00:01:03,180 --> 00:01:06,390
Moving on to task 2 wanted to check 10 some.

18
00:01:06,420 --> 00:01:08,280
So this one was a little different.

19
00:01:08,300 --> 00:01:12,840
Now you are going to need some sort of if statement because I want you to first check if the sum is

20
00:01:12,840 --> 00:01:23,560
equal to 10 return TRUE else return there are some so say if and one plus two is equal to 10 return

21
00:01:24,040 --> 00:01:32,930
TRUE else will return their actual sum or turn and one plus and two OK.

22
00:01:33,180 --> 00:01:36,730
So again now we actually do need that if statement.

23
00:01:36,740 --> 00:01:42,320
Now let's return to task 3 and this one wanted you to create a function that takes in a string and returns

24
00:01:42,320 --> 00:01:45,040
back the first character of that string in uppercase.

25
00:01:45,050 --> 00:01:46,300
This is pretty straightforward.

26
00:01:46,310 --> 00:01:53,250
All we need to do is say my string grab the first character which is going to be 0 and then perform

27
00:01:53,250 --> 00:01:55,610
the upper on it and just return that result.

28
00:01:56,880 --> 00:01:58,280
OK that's all you have to do for that one.

29
00:01:58,280 --> 00:02:01,170
Just again grabbing the first character and then calling the upper.

30
00:02:01,170 --> 00:02:06,560
You could technically separate this out into two steps but you can also do it all in one step OK.

31
00:02:06,560 --> 00:02:11,360
Moving along to task for this one wanted you to create a function that takes in a string and returns

32
00:02:11,360 --> 00:02:12,860
the last two characters.

33
00:02:12,860 --> 00:02:17,420
Now there was a stack overflow alink that give you a pretty good answer on how to actually grab characters

34
00:02:17,420 --> 00:02:19,960
from the end more than just one character from the end.

35
00:02:20,210 --> 00:02:22,100
So let's walk through how we could do this.

36
00:02:22,100 --> 00:02:26,640
First we should be checking at the length the string is less than two.

37
00:02:26,780 --> 00:02:41,330
So I'll say if length of my string is less than 2 return error else and this is where you could use

38
00:02:41,330 --> 00:02:49,220
that stack overflow alink we're going to return my string starting index position negative to essentially

39
00:02:49,220 --> 00:02:53,140
two steps back from the end colon all the way to the end.

40
00:02:53,180 --> 00:02:56,930
So if you take a look at the stack overfilling it would have given you this sort of syntax.

41
00:02:56,930 --> 00:03:01,760
So this isn't something we exactly covered in our string lecture but it is something you would be able

42
00:03:01,760 --> 00:03:04,120
to figure out if you used this link.

43
00:03:04,470 --> 00:03:04,740
OK.

44
00:03:04,760 --> 00:03:09,800
Again just going back from the very end to spaces and then colon go all the way to the end.

45
00:03:09,800 --> 00:03:18,220
So our starting position here is necklet to task number five was given a list of integers return true

46
00:03:18,340 --> 00:03:21,700
is the sequence 1 2 3 is somewhere in the list.

47
00:03:21,730 --> 00:03:25,290
So this one probably has the most variety of ways you can tackle this problem.

48
00:03:25,300 --> 00:03:26,700
Lots of different ways to do it.

49
00:03:26,710 --> 00:03:28,940
I'll show you one simple way.

50
00:03:29,050 --> 00:03:34,560
So one way to do this is we can iterate through the list all the way up to the last two.

51
00:03:34,570 --> 00:03:41,660
So we're going to say for I in range length of Nom's minus two

52
00:03:45,140 --> 00:04:01,330
numbs of I is equal to 1 and number of I plus 1 is equal to 2 and Nom's of E-plus to is equal to three

53
00:04:02,880 --> 00:04:10,380
then return true otherwise otherwise we'll just return false.

54
00:04:10,560 --> 00:04:15,330
So let me zoom out here just we can kind of see what's going on.

55
00:04:15,340 --> 00:04:20,990
So again what I'm doing here is going to say for every i in range length of Nom's minus two.

56
00:04:21,280 --> 00:04:26,140
So what this is doing and I'll probably make more sense if we look at this line first let's imagine

57
00:04:26,140 --> 00:04:30,250
what it is just going to be the index positions themselves.

58
00:04:30,580 --> 00:04:32,600
So this for loop right here.

59
00:04:32,770 --> 00:04:40,730
That's going to start at zero than 1 One two three and it's go all the way until the length of the list.

60
00:04:40,960 --> 00:04:42,120
Minus two.

61
00:04:42,280 --> 00:04:44,410
And often beginners get confused Well why not.

62
00:04:44,410 --> 00:04:46,910
All the way to the length list why minus two.

63
00:04:47,020 --> 00:04:52,340
And that's because those what we're doing in this line we're saying if none of AI is equal to 1.

64
00:04:52,390 --> 00:04:57,880
So if in that list of numbers in addition is equal to 1 and the very next index to an AI plus one is

65
00:04:57,880 --> 00:05:01,630
equal to two and then next to this and after that I plus two is equal to three.

66
00:05:01,630 --> 00:05:02,600
Well then you have your match.

67
00:05:02,620 --> 00:05:04,540
You're going to return true.

68
00:05:04,540 --> 00:05:08,280
Now the trick is we're saying I plus 1 and I plus 2.

69
00:05:08,320 --> 00:05:12,960
That means we only need to go in this range up to and minus 2.

70
00:05:12,970 --> 00:05:15,120
We don't need to go all the way to the end of that list.

71
00:05:15,190 --> 00:05:20,460
Otherwise I plus one and I plus two would be reaching out beyond the length of the list.

72
00:05:20,470 --> 00:05:26,580
So that's why we say length of numbers minus two because we have I plus two and I plus 1.

73
00:05:26,650 --> 00:05:31,600
So we don't need to go all the way to the end of the list and said we need to go to up to the analyst

74
00:05:31,630 --> 00:05:37,180
because I plus 1 I plus two themselves will reach the end of the list and then if this happens to be

75
00:05:37,180 --> 00:05:39,290
the case 1 2 and 3.

76
00:05:39,650 --> 00:05:41,450
Well then you can just return true.

77
00:05:41,710 --> 00:05:45,640
Then if you've gone through this entire four loop and you never returned to true then I'm just going

78
00:05:45,640 --> 00:05:46,690
to return false.

79
00:05:46,690 --> 00:05:51,740
So notice how my for loop is now indented at the same as it's return false.

80
00:05:51,760 --> 00:05:52,330
OK.

81
00:05:52,390 --> 00:05:54,650
There's lots of other ways you get done this sequence check.

82
00:05:54,660 --> 00:05:56,790
Maybe you could have just done this as a single slice.

83
00:05:56,950 --> 00:06:00,400
So Gone slice by slice and then check it to be the list.

84
00:06:00,400 --> 00:06:05,010
One two three but I think this is the most kind of fundamental way of doing it.

85
00:06:05,010 --> 00:06:09,080
Yes go throughout the list and then check three at a time separately and return true.

86
00:06:09,100 --> 00:06:10,370
They happened to be the case.

87
00:06:10,390 --> 00:06:12,960
The trick here being you have to go all the way to the end of the list.

88
00:06:12,970 --> 00:06:20,200
You just have to go to the length of the list minus two because of this I plus one and I plus two OK.

89
00:06:20,210 --> 00:06:25,540
Let's move on to task number six which was given two strings create a function that returns a length.

90
00:06:25,640 --> 00:06:27,620
The difference in length between them.

91
00:06:27,640 --> 00:06:31,010
This it's actually pretty straightforward if were able to figure out how to do an absolute value in

92
00:06:31,010 --> 00:06:35,270
Python which in this case was just abs.

93
00:06:35,280 --> 00:06:41,700
So first we take the length of string 1 which here we called S1 and then we subtract the length of S

94
00:06:41,760 --> 00:06:42,580
2.

95
00:06:42,600 --> 00:06:46,760
So this right here this gives you the difference in length between S1 and S2.

96
00:06:46,950 --> 00:06:51,350
However if one is smaller than as two then you'll get a negative number.

97
00:06:51,370 --> 00:06:54,730
So you're going to want to return the absolute value of this difference.

98
00:06:54,750 --> 00:07:04,320
So then you just pass it into abs absolute value and then you just return that and that is your solution.

99
00:07:04,540 --> 00:07:12,030
So we turn the absolute value between the difference of length of these two strings finally task number

100
00:07:12,030 --> 00:07:15,800
seven given a list of integers is the length but the list is an even number.

101
00:07:15,900 --> 00:07:20,940
Return the sum of that list is the length of the list is odd return the max value in that list.

102
00:07:21,210 --> 00:07:22,320
So how can we do that.

103
00:07:23,640 --> 00:07:29,790
Well we can say length is equal to length of my list.

104
00:07:31,460 --> 00:07:32,460
And then we'll say the following.

105
00:07:32,510 --> 00:07:37,670
If the length mod to is equal to zero.

106
00:07:38,450 --> 00:07:48,520
Return the sum of the list else return the max of my list.

107
00:07:48,810 --> 00:07:54,450
So all we're doing here is we're checking the length of that list saying if the length Mattu is equal

108
00:07:54,450 --> 00:07:56,530
to zero then return the sum.

109
00:07:56,520 --> 00:08:01,390
Otherwise just return the max of the list and that's the sum or max function.

110
00:08:01,760 --> 00:08:03,650
Ok that's it for this lecture.

111
00:08:03,690 --> 00:08:06,690
I hope you found the section useful if you're a beginner to Python.

112
00:08:06,690 --> 00:08:11,920
Coming up next we're going to begin to discuss more advanced Python topics in Python level 2.

113
00:08:12,000 --> 00:08:15,060
If you have any questions feel free to post the Q&amp;A forums.

114
00:08:15,060 --> 00:08:16,020
We'll see you there.
