The Prison Break Think Like A Coder Ep 1

Upon emerging from stasis, Ethic is the
unfortunate recipient of three surprises.

The first: a prison cell.

The second: complete amnesia.

And the third: a mysterious stranger
has gotten stuck

squeezing through the bars on her window.

His name is Hedge, and he has come
to help Ethic save the world.

But first they have to break out of jail.

Hedge turns his hand into a lockpick and
outlines the challenge ahead.

Each lock in the prison works
in the same unusual way.

Inside the keyhole is a red dial that can
be rotated to one of 100 positions

numbered 1 through 100.

The key for a given cell spins the dial
to the right position,

which, when stopped there, makes it turn
green and unlocks the door.

It would be out of the question to steal
keys from a guard,

but Hedge has a better idea.

Hedge can carry out Ethic‘s commands.

If Ethic tells him to walk
5 steps forward,

turn right, then walk another 5 steps,

that’s exactly what he’ll do.

Hedge needs specific instructions though.

If Ethic says “pick the lock” or
“try every combination”

that would be too vague, but “spin the
dial 5 positions forward” would work.

Once out of the cell, they will only have
a few moments to crack the lock

for the outer prison door too before
the guards catch them.

So what instructions will allow Hedge
to efficiently open any door?

Pause now to figure it out for yourself.

Before we explain the solution,
here’s a hint.

A key programming concept that can help
unlock the door is called a loop.

This can be one or more instructions
that Hedge will iterate— or repeat—

a specified number of times,

like “jump up and down 100 times.”

Or an instruction that Hedge will
repeat until a condition is met,

such as “keep jumping up and
down until it’s 7 o’clock.”

Pause now to figure it out for yourself.

The first thing that’s clear is that you
need to find a way for Hedge

to try every combination until one works.

What takes a little more effort is
how exactly you do so.

One solution would be to instruct Hedge
to try every combination in succession.

Try 1 and check the light.

If it turns green, open the door,
and if not, try 2.

If that doesn’t work try 3.
All the way up to 100.

But it would be tedious to lay
that out in its entirety.

Why write more than 100 lines of code,

when you can do the same thing
with just 3?

This is where a loop comes in.

There are a few ways to go about this.

The lock has 100 positions,

so Ethic could say “Check the dial’s
color, then spin the dial forward once,

for 100 repetitions.

Remember where the dial turns green, then
have Hedge set it back to that number.”

A loop like this, where you specify
the number of times it repeats,

is called a “for" loop.

But an even more efficient loop

would have Hedge spin the dial one
position at a time until it turns green

and as soon as that happens, have him
stop and open the door.

That way if the door unlocks on 1,

he doesn’t need to cycle through
all the rest of the numbers.

This is an “until” loop,

because it involves doing an
action until a condition is met.

A similar, alternate approach would be
to turn the dial while it’s still red,

then stop.

That’s called a “while” loop.

Back to the adventure.

Hedge loops through the combinations,
and the cell opens at 41.

Ethic and Hedge wait until the
perfect moment in the guards’ rotation

and make a break for it.

Soon, Ethic faces a choice:
hide inside a mysterious crystal,

or try to crack the outer door
and make a run for it.

Ethic chooses to run.

The second door takes Hedge longer,
requiring him to spin all the way to 93.

But he gets it open

and takes the opportunity
to explain why he’s rescued Ethic.

The world is in turmoil:

robots have taken over,
and only Ethic can set things right.

In order to do so,

they’ll need to collect three
powerful artifacts

that are being used for nefarious
purposes across the land.

Only then can Ethic return to the
world machine— that giant crystal—

to set things right.

Ethic may have escaped the prison…

but what has she gotten herself into?

走出停滞后,
Ethic 不幸遭遇了三个意外。

第一个:牢房。

第二:完全失忆。

第三个:一个神秘的
陌生人被困

在她窗户上的栏杆上。

他的名字叫赫奇,他是
来帮助艾提克拯救世界的。

但首先他们必须越狱。

赫奇把手变成了一把开锁器,
勾勒出前方的挑战。

监狱里的每一把锁都以
同样不同寻常的方式工作。

钥匙孔内是一个红色表盘,
可以旋转到编号为 1 到 100 的 100 个位置之一

给定单元的钥匙将表盘旋
转到正确的位置

,当停在那里时,它会变成
绿色并解锁门。

从守卫那里偷钥匙是不可能的

但赫奇有一个更好的主意。

Hedge 可以执行 Ethic 的命令。

如果 Ethic 告诉他向前走
5 步,

右转,然后再走 5 步,

这正是他会做的。

不过,对冲需要具体说明。

如果 Ethic 说“撬锁”或
“尝试所有组合”

,那将过于含糊,但“将
表盘向前旋转 5 个位置”会起作用。

一旦出了牢房,他们也
只有片刻的时间来

打开外牢门的锁,
然后被守卫抓住。

那么什么指令可以让
Hedge 有效地打开任何一扇门呢?

现在停下来自己弄清楚。

在我们解释解决方案之前,
这里有一个提示。

可以帮助解锁门的关键编程概念
称为循环。

这可以是
Hedge 将迭代或

重复指定次数的一条或多条指令,

例如“上下跳跃 100 次”。

或者 Hedge 会
重复直到满足某个条件的指令,

例如“继续上下跳跃,
直到 7 点钟”。

现在停下来自己弄清楚。

首先要明确的是,您
需要找到一种方法让 Hedge

尝试每种组合,直到其中一个有效。

需要更多努力的是
你如何做到这一点。

一种解决方案是指示 Hedge
连续尝试每种组合。

尝试 1 并检查灯光。

如果它变成绿色,请打开门
,如果不是,请尝试 2。

如果这不起作用,请尝试 3。
一直

到 100。但是将其全部列出会很乏味

为什么要写超过 100 行代码,而只需 3 行代码

就可以完成同样的事情

这就是循环出现的地方。

有几种方法可以解决这个问题。

锁有 100 个位置,

因此 Ethic 可以说“检查表盘的
颜色,然后向前旋转表盘一次

,重复 100 次。

记住表盘变成绿色的位置,然后
让 Hedge 将其设置回那个数字。”

像这样的循环,您可以在其中指定
重复的次数

,称为“for”循环。

但更有效的循环

将让 Hedge 一次旋转一个
位置,直到它变成绿色

,并且一旦发生这种情况 ,让他
停下来开门。

这样如果门在 1 上解锁,

他就不需要循环遍历
所有其余的数字。

这是一个“直到”循环,

因为它涉及到执行一个
操作直到一个条件 遇到

。类似的替代方法是
在表盘仍为红色时转动表盘,

然后停止。

这称为“while”循环。

回到冒险

。Hedge 循环遍历组合,
并且单元格在 41 处打开。

道德和 Hedge 等到
守卫轮换的完美时刻,

然后休息一下。

很快,Ethic 面临一个选择:
躲在一个神秘的水晶里,

或者尝试打开外门逃跑。Ethic
选择逃跑。

第二扇门需要更长的时间,
需要他一直旋转到 93。

但他打开了门

并采取了 是
解释他为什么救出 Ethic 的机会。

世界处于动荡之中:

机器人已经接管
,只有 Ethic 才能解决问题。

为了做到这一点,

他们需要收集三件
强大的神器

,这些神器
在这片土地上被用于邪恶目的。

只有这样,Ethic 才能回到
世界机器——那颗巨大的水晶——

来解决问题。

Ethic 可能已经越狱了……

但她自己卷入了什么?