WEBVTT
Kind: captions
Language: en

1
00:00:00.140 --> 00:00:02.080
Seven-segment displays are great.

2
00:00:02.080 --> 00:00:03.800
We don't see them as much
in electronics these days

3
00:00:03.800 --> 00:00:07.859
because screens are a lot cheaper than they
used to be, but these used to be everywhere

4
00:00:07.859 --> 00:00:11.580
from clocks to supermarket checkouts
to calculators to slot machines.

5
00:00:11.580 --> 00:00:17.009
Here, they're being used as part of the Megaprocessor
at the Centre for Computing History in Cambridge.

6
00:00:17.009 --> 00:00:21.199
This is the clock speed that the whole system
is running at.

7
00:00:21.199 --> 00:00:24.619
Seven-segment displays are a really clever
bit of design.

8
00:00:24.619 --> 00:00:29.689
Seven is the minimum number of segments required
to show every number using straight lines.

9
00:00:29.689 --> 00:00:34.140
It doesn't matter that the 4 isn't actually
how most people write a number 4,

10
00:00:34.140 --> 00:00:36.910
it's close enough that we've all just got
used to it.

11
00:00:36.910 --> 00:00:39.280
And seven segments plus a decimal point

12
00:00:39.280 --> 00:00:43.789
means that there are eight lights here to
turn on or off, which is convenient:

13
00:00:43.789 --> 00:00:45.370
computers like working with eights,

14
00:00:45.370 --> 00:00:46.820
there are eight bits in a byte,

15
00:00:46.820 --> 00:00:51.310
so you can store the state of those lights
in just one byte of memory.

16
00:00:51.310 --> 00:00:54.050
And you can use these for some letters, too.

17
00:00:54.050 --> 00:00:57.040
You could show the word "Error",
or even base-16 numbers.

18
00:00:57.040 --> 00:01:00.180
But there's no way you can write an
M or a W on this display.

19
00:01:00.180 --> 00:01:02.920
So here is a code question for you:

20
00:01:02.920 --> 00:01:07.380
what is the longest English word that you
can write on a seven-segment display?

21
00:01:10.020 --> 00:01:13.600
I love questions like this because there are
so many ways to approach them.

22
00:01:13.600 --> 00:01:17.619
I'm going to give one solution here because
that's the way I want to tell the story,

23
00:01:17.619 --> 00:01:20.090
but I can think of a couple of others just
off the top of my head,

24
00:01:20.090 --> 00:01:23.680
and there will be a dozen more that I haven't
thought of,

25
00:01:23.680 --> 00:01:25.490
or maybe couldn't even think of.

26
00:01:25.490 --> 00:01:27.420
To start off, we need a dictionary,

27
00:01:27.420 --> 00:01:29.480
and fortunately here the work
has been done for us.

28
00:01:29.480 --> 00:01:31.969
There is a public-domain list of English words
available,

29
00:01:31.969 --> 00:01:33.590
I've put the link in the description.

30
00:01:33.590 --> 00:01:35.610
Now, this is not a perfect list:

31
00:01:35.610 --> 00:01:38.380
right at the top there are
two different spellings of 'aarrgh'

32
00:01:38.380 --> 00:01:41.719
and I'm really not sure either
of those should count,

33
00:01:41.719 --> 00:01:44.570
but the list is good enough for our purposes.

34
00:01:44.570 --> 00:01:47.399
We can manually filter it later if there are
any strange results.

35
00:01:47.399 --> 00:01:50.440
Now, I'm going to code this in JavaScript
using Node.

36
00:01:50.440 --> 00:01:53.259
Not the best language, but it's
not bad for beginners,

37
00:01:53.259 --> 00:01:55.670
and importantly it's easy for me to explain.

38
00:01:55.670 --> 00:01:58.789
First, we're going to add
a bit of boilerplate code,

39
00:01:58.789 --> 00:02:00.670
just stock stuff to get it working.

40
00:02:00.670 --> 00:02:02.619
Did I know all that code off by heart?

41
00:02:02.619 --> 00:02:03.920
No, of course not,

42
00:02:03.920 --> 00:02:08.009
I Googled 'load array from file in node' and
I adapted some of the results.

43
00:02:08.009 --> 00:02:10.729
This is how basically how everyone
codes stuff like that.

44
00:02:10.729 --> 00:02:14.690
Don't ever be afraid that you're not a real
programmer because you still look stuff up.

45
00:02:14.690 --> 00:02:18.350
The important part about programming is not
remembering exact words or syntax:

46
00:02:18.350 --> 00:02:22.200
it is breaking down a problem,
working out how to solve it,

47
00:02:22.200 --> 00:02:25.010
and then fixing all the inevitable bugs in
your solution.

48
00:02:25.010 --> 00:02:28.180
It's about holding lots of
complicated connections in your head,

49
00:02:28.180 --> 00:02:30.980
not the exact magic words
that you need this one time.

50
00:02:30.980 --> 00:02:34.140
I still forget which order to put
basic stuff in sometimes.

51
00:02:34.140 --> 00:02:38.340
Anyway, the first line loads in the bits of
Node that deal with reading and writing files,

52
00:02:38.340 --> 00:02:44.060
and the next line loads the entire dictionary
into a single long string called "words".

53
00:02:44.060 --> 00:02:48.700
The next line converts that single string
into an array, a long list of smaller strings,

54
00:02:48.700 --> 00:02:51.090
based on where the new-line characters are.

55
00:02:51.090 --> 00:02:55.090
And now we have an array of all the words
in the English language:

56
00:02:55.090 --> 00:02:57.349
basically just a long list of strings.

57
00:02:57.349 --> 00:03:01.620
Let's see how long that array is by telling
the console to output the array's length.

58
00:03:01.620 --> 00:03:08.769
Okay, more than 370,000 words, each one in
a separate item in that array called “words”.

59
00:03:08.769 --> 00:03:10.519
Next problem: we need to filter that list

60
00:03:10.519 --> 00:03:14.360
and remove any words that use letters that
we can't display with seven segments.

61
00:03:14.360 --> 00:03:17.430
Which gives us an interesting design problem:

62
00:03:17.430 --> 00:03:19.030
which letters can't we display?

63
00:03:19.030 --> 00:03:23.290
Now, I'm going to use fancy graphics here
rather than actual seven-segment displays,

64
00:03:23.290 --> 00:03:24.590
but for extra credit,

65
00:03:24.590 --> 00:03:28.920
you can try and figure out the After Effects
expressions that I used to make these.

66
00:03:28.920 --> 00:03:31.810
Letters A through F are easy, there's almost
a standard for those.

67
00:03:31.810 --> 00:03:33.870
But G is difficult.

68
00:03:33.870 --> 00:03:37.480
We can't use the obvious pattern,
because that's a 6, or a 9 if it’s lowercase.

69
00:03:37.480 --> 00:03:40.939
And if we use an alternate pattern for it,
it's... not really a G?

70
00:03:40.939 --> 00:03:42.020
It's a C with aspirations.

71
00:03:42.020 --> 00:03:46.450
But to be fair, if any of those patterns appear
at the start of a word like 'GOAL',

72
00:03:46.450 --> 00:03:48.900
no-one's going to look at it and say
'oh, six-OAL'.

73
00:03:48.900 --> 00:03:51.840
But I'm going to make the call that
we don't allow it.

74
00:03:51.840 --> 00:03:53.769
I think I is all right, though.

75
00:03:53.769 --> 00:03:55.230
Like, that's clearly an I,

76
00:03:55.230 --> 00:03:58.150
it's not like the half-assed G which
sorta looked kinda like a C?

77
00:03:58.150 --> 00:04:01.750
It's clear. I mean, I don't care that I'm
not applying strict rules here,

78
00:04:01.750 --> 00:04:04.409
I'm just going on what feels right,
and if you disagree,

79
00:04:04.409 --> 00:04:06.180
you can fix it in your version.

80
00:04:06.180 --> 00:04:09.160
Other letters that I'm ruling out: K.
Just can't be done, requires a diagonal.

81
00:04:09.160 --> 00:04:12.319
M: I've seen it displayed like this before,
but: no. Not having it.

82
00:04:12.319 --> 00:04:16.079
N is borderline, but I'm going to allow it
because there's nothing else it could be.

83
00:04:16.079 --> 00:04:18.140
Q is out: that is just a 9.

84
00:04:18.140 --> 00:04:19.950
R, I'll allow if it's lowercase.

85
00:04:19.950 --> 00:04:21.550
S is OK, same reasons as I.

86
00:04:21.550 --> 00:04:23.820
But there's no way to do V, or W, or X.

87
00:04:23.820 --> 00:04:27.630
And as for Z... no.
It needs the diagonals. Not having it.

88
00:04:27.630 --> 00:04:31.350
Here's our alphabet, then.
Eighteen letters left, eight disallowed.

89
00:04:31.350 --> 00:04:33.860
That's actually more than I'd expected
left in there.

90
00:04:33.860 --> 00:04:36.200
And I'm going to cheat.
I know this is called the Basics,

91
00:04:36.200 --> 00:04:38.160
but doing this the long way
would be really dull,

92
00:04:38.160 --> 00:04:42.440
so I'm going to put those disallowed letters
into something called a regular expression,

93
00:04:42.440 --> 00:04:45.210
or a regex. Or "reg-ex", whichever.

94
00:04:45.210 --> 00:04:47.590
Those slashes indicate that it’s a regex,

95
00:04:47.590 --> 00:04:52.420
and whatever's inside those slashes is like
a test that a string can match against.

96
00:04:52.420 --> 00:04:56.130
So this regex would match any word
with an X in it,

97
00:04:56.130 --> 00:04:58.300
whether that X is at the
start or middle or end.

98
00:04:58.300 --> 00:05:03.100
As long as there's an X somewhere in the string,
it passes that test.

99
00:05:03.100 --> 00:05:05.090
If we put all our disallowed letters in,

100
00:05:05.090 --> 00:05:08.120
then surround them with square brackets so
they're treated as a class,

101
00:05:08.120 --> 00:05:13.130
this regex will match any string, any word,
with any of those letters anywhere in it.

102
00:05:13.130 --> 00:05:16.780
If a string matches this, we cannot use it.

103
00:05:16.780 --> 00:05:19.560
Regular expressions are a heck of a lot
more complicated than this,

104
00:05:19.570 --> 00:05:22.210
and they can boggle even experienced
programmers' minds,

105
00:05:22.210 --> 00:05:28.320
but using one here will save me about
five minutes of really dull script later on.

106
00:05:28.320 --> 00:05:30.770
The good news is: we can now just use the
function 'match'

107
00:05:30.770 --> 00:05:32.920
to test a string against this regular expression,

108
00:05:32.920 --> 00:05:35.070
which I'm calling badLetters,

109
00:05:35.070 --> 00:05:37.950
and it'll tell us whether there are
any bad letters in there.

110
00:05:37.950 --> 00:05:40.721
So how do we filter the array?

111
00:05:40.721 --> 00:05:44.990
One of the important trade-offs here is between
code that is fast

112
00:05:44.990 --> 00:05:47.820
and code that everyone can understand.

113
00:05:47.820 --> 00:05:50.240
This is not going to be an efficient and fast
approach.

114
00:05:50.240 --> 00:05:53.250
But because we're running it on a modern PC,
at the command line,

115
00:05:53.250 --> 00:05:58.930
and we don't mind waiting a fraction of a
second after telling it to go, that's no big deal.

116
00:05:58.930 --> 00:06:00.770
It's more important that I can show the code
and explain it,

117
00:06:00.770 --> 00:06:02.930
and look at it again later
and understand it.

118
00:06:02.930 --> 00:06:06.380
But imagine if something like this had to
be run on the scale of YouTube or Google,

119
00:06:06.380 --> 00:06:09.010
running millions or billions of times a day.

120
00:06:09.010 --> 00:06:12.640
Every minor improvement you could make
would be worth it.

121
00:06:12.640 --> 00:06:17.380
At some point, I should do a video about
Big O notation, but now is not the time.

122
00:06:17.380 --> 00:06:21.650
Here, where we're just writing code to find
the answer to one simple question,

123
00:06:21.650 --> 00:06:22.650
don't worry about it.

124
00:06:22.650 --> 00:06:26.680
Sure, there are more elegant solutions.
But this is easy to explain.

125
00:06:26.680 --> 00:06:30.520
I'm going to declare an empty string.
longestAcceptableWord.

126
00:06:30.520 --> 00:06:34.810
Then I'm going to tell the code to start testing
every word in the array.

127
00:06:34.810 --> 00:06:37.800
This line will run the code in between
those brackets once for each word,

128
00:06:37.800 --> 00:06:40.230
and on each run through, this variable,
testWord,

129
00:06:40.230 --> 00:06:42.580
will be the next word in the list.

130
00:06:42.580 --> 00:06:47.120
First question: is the word we're looking
at shorter, or the same length,

131
00:06:47.120 --> 00:06:49.310
as the current longest acceptable word?

132
00:06:49.310 --> 00:06:51.380
If it is, then we can just ignore it,

133
00:06:51.380 --> 00:06:53.300
we know it’s not longer,
it's of no use to us,

134
00:06:53.300 --> 00:06:55.280
and we can just say 'continue'.

135
00:06:55.280 --> 00:06:57.110
That 'continue' skips the rest of the loop,

136
00:06:57.110 --> 00:06:59.320
and kicks us back to the start
with the next word.

137
00:06:59.320 --> 00:07:01.880
Some people hate 'continue',
they think it shouldn't be used anywhere

138
00:07:01.880 --> 00:07:04.860
because it can cause confusion.
And, yeah, it can,

139
00:07:04.860 --> 00:07:07.980
but I reckon for things like this it's fine.

140
00:07:07.980 --> 00:07:11.270
Anyway, this'll save a lot of processing time,
because after the first few words,

141
00:07:11.270 --> 00:07:14.270
we're not even going to bother to analyse
the short ones,

142
00:07:14.270 --> 00:07:16.580
we'll just ignore them.

143
00:07:16.580 --> 00:07:19.140
So let's say we've got to this point
in the code,

144
00:07:19.140 --> 00:07:22.250
it's a new, longer possible word: is it acceptable?

145
00:07:22.250 --> 00:07:24.140
Here we can use our
regular expression from earlier.

146
00:07:24.140 --> 00:07:26.140
Does the word match our test for bad letters?

147
00:07:26.140 --> 00:07:31.070
If it does, it's not acceptable,
ignore it, ignore the rest of the loop,

148
00:07:31.070 --> 00:07:32.680
continue on to the next word in the list.

149
00:07:32.680 --> 00:07:35.750
But if it’s passed both these tests,
if we’ve got to this point,

150
00:07:35.750 --> 00:07:38.550
then we know that our word is longer than
anything that we've accepted before

151
00:07:38.550 --> 00:07:40.330
and it has no bad letters in it.

152
00:07:40.330 --> 00:07:43.570
So we change the longest acceptable word to
be our new word

153
00:07:43.570 --> 00:07:46.660
and we start over again with the next one
in the array.

154
00:07:46.660 --> 00:07:52.760
And when we're done, longestAcceptableWord
will be the longest acceptable word.

155
00:07:52.760 --> 00:07:54.650
We tell the code to write that out, and the
result is...

156
00:07:56.660 --> 00:07:59.970
Huh. That's actually the longest word
in the list anyway.

157
00:07:59.970 --> 00:08:02.220
You know what?
I'm going to rule out I and O.

158
00:08:02.220 --> 00:08:04.260
They are just numbers with aspirations.

159
00:08:04.260 --> 00:08:10.310
Fortunately, we've made the code easy to edit,
so we can just make that change, and...

160
00:08:12.600 --> 00:08:15.370
Sure, fine, that'll do.
The lesson here, I guess,

161
00:08:15.370 --> 00:08:17.610
is that sometimes you might write code to
find something out

162
00:08:17.610 --> 00:08:20.580
and the answer is really unsatisfying.

163
00:08:20.580 --> 00:08:22.960
Of course, there is one thing
we're missing here.

164
00:08:22.970 --> 00:08:26.800
Our test was only checking for words longer
than the current acceptable one.

165
00:08:26.800 --> 00:08:30.790
What happens if there was another
acceptable word of the same length?

166
00:08:30.790 --> 00:08:34.780
We'd have ignored it.
There could be multiple correct answers.

167
00:08:34.780 --> 00:08:37.750
But I'll leave checking that up to you.

168
00:08:38.420 --> 00:08:41.560
Thank you very much to the
Centre for Computing History in Cambridge,

169
00:08:41.560 --> 00:08:43.870
for lending me their space and their Megaprocessor.

170
00:08:43.870 --> 00:08:48.110
Thank you also to all my proofreading team
who made sure I got the script right.

