All language subtitles for 012 Exercise Three (Solution)_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

All right, let's debug exercise number three.

So in this exercise, it's supposed to output a random number between one and 15 printed and then print

whether that number is prime or composites.

Now, prime means that the number is only divisible by itself and one.

So numbers like 1117 are 43.

But if we run the code.

Clearly it doesn't work.

And we're going to use breakpoints to visualize the runtime and debug the issue.

And there's your first mistake.

It returns 16 when it should return.

Year enemy number between one and 15.

But why is the code crashing?

Let's not worry about that right now.

There's clearly a bug right here, so let's step into the function.

And now it's perfectly clear what the error was.

The range that comes in is 15 plus one, plus that random decimal, which is going to be 16 point something.

And then casting that to an integer is always going to return 16.

So clearly this is all wrong.

Instead, we should multiply the range by the random value that gets returned and then add one.

All right, let's try this again.

Replay the debugger.

Much better.

We can keep testing this out and it keeps returning a random number each time.

Okay.

That number gets stored in a variable named random and gets printed.

Okay.

Now the crash happens in this function, so we'll step into it.

First, it's going to go inside the print line function.

We don't care about that.

So step out of it.

And this is the last line of code.

So I'm assuming here is where it crashes.

Okay.

What's going on?

We have a random number coming in.

And that number modulus.

Some value is causing the crash.

Ah, that makes sense.

As you know by now.

Modulus means dividing two numbers and returning the remainder, and you can never divide a number by

zero.

That's impossible, which causes an arithmetic exception.

And so what we want to do is start the loop counter at one, none at zero.

All right, let's try this out.

Wait.

This number is not a prime number.

There's something wrong.

Let's restart the debugger and go through the loop.

Okay.

So this number should be a composite.

And the remainder from dividing this number by one and zero, which would make it composite.

But wait a second.

Every number is divisible by one, so it doesn't really make sense to start the loop at one.

Anyways, let's keep going.

It is divisible by two into the fact that it's divisible by two means.

We know this number is composite, so we don't need to keep going with the loop.

But the loop keeps going and because it's not divisible by three, it marks it back as prime, which

doesn't make any sense.

The first thing we need to do is break the loop as soon as we establish that the random number is composites.

All right, relaunch the debugger.

Okay.

This time you get a number that's clearly prime.

And remember the rule that you can't divide a prime number by anything except itself in one.

But wait, our code isn't going to work because the loop starts at one and it's going to think, okay,

this number divided by one returns a remainder of zero.

So clearly this number is composite and it's going to break the loop.

And here you can see that happen in real time.

This number is not a composite number, and dividing it by one should not be a criteria to determine

if a number is prime or composites.

So we need to start the loop off at two.

All right, let's try this again.

Now we get a composite number.

That number is indeed divisible by two.

So it's going to return a remainder of zero, which means it's composite.

Break the loop and we're good.

All right, let's keep running tests.

Ken.

Now we've got a prime number.

That number is not divisible by anything.

So it's going to keep going through the entire loop and none of the numbers are going to return a non-zero

modulus.

And since the number is not divisible by anything, it's always going to be prime.

And that's all.

Now I would like to rewrite this code.

A more efficient way to do this is to start the variable office prime.

And if the numbers happen to be divisible by one of the numbers inside the range.

Then we're going to label it as composite and break the loop.

To me, that makes a lot more sense.

So let's rerun and visualize the runtime.

Okay.

Now we're going to composite number.

First we assume that it's prime.

That number is indeed divisible by two.

So this condition is going to be true, making it composite.

Breaking the loop and returning the results.

All right.

This time you get a prime number.

First we assume it's prime.

It's not going to divide by anything.

So it will stay prime.

Eventually the loop is going to break and return a prime result.

And the one last thing is that the number one is not a prime number.

So in the event that the random number returned is one, this function is actually going to return prime,

which is incorrect.

So here what I can do is use the ternary operator to say if random value equals one.

Otherwise we'll pass that number into the is prime function and then it returned the correct results.

All right.

One more time.

Number is one.

What a coincidence.

This evaluates the true.

This time.

This evaluates the files and it's going to run the function which determines whether the value is prime

or composite.

In this case, it ends up being composites.

By the way, you don't have to use the ternary operator if you're not comfortable with it.

You can use standard default if you want.

It's the same thing.

All right.

That's all.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.