2025斯坦福大学 Code in Place Week1 Practice【个人代码+参考答案】

 题目

用python语言,以及吉祥物karel建造医院,分为small和hospital两个world,如下图所示:

我的思路

(1)一开始要让karel处于直行状态,因此定义straight_line;

(2)抵达有beeper的地点后,开始建造医院,因此定义build_small_hospital;

(3)建造完医院后重新直行,重复之前的动作。

埋坑点

(1)small world和hospital world的终点有差异,small world建造完医院后要向前走一步,遇到墙,然后停下,而hospital world在建造医院之后,前面有墙,要立刻停下。我们的代码要两个世界观都适用,因此考虑增加条件,if front is clear,才直行

【对应下方代码:if front_is_clear:

                                straight_line()

(2)karel在脚下没有beeper,前面有路的情况下要往前走,建造完医院的那个节点,前面有路,karel也要恢复前进的方向,往前走,因此定义end_move;

【先把方向扭转过来,再判断前路是否通畅:

在hospital world里,如果前路通畅,就会直行,然后重复while循环,在最后遇到墙体了,把方向变为east,就停了;

在small world里,建造完医院后,前路是通畅的,就move了一次,后续继续遇到if front_is_clear语句,条件判断为false,就停下来了

请结合下图main()和def end_move()一起理解噢~

(3)build_small_hospital最后不要写move(),因为它在Hospital世界观下最后一次不能move;

 我的代码示例

from karel.stanfordkarel import *

def main():
    straight_line()
    while beepers_present() and front_is_clear():
        build_small_hospital()
        end_move()
        if front_is_clear():
            straight_line()


def build_small_hospital():
    turn_left()
    move()
    put_beeper()
    move()
    put_beeper()
    turn_right()
    move()
    put_beeper()
    turn_right()
    move()
    put_beeper()
    move()
    put_beeper()

def straight_line():
    while facing_east() and no_beepers_present():
        if front_is_clear():
            move()

def end_move():
    while facing_south() and beepers_present():
        turn_left()
    if front_is_clear():
        move()


# Note: turn_right() is not called above but added for reference.
def turn_right():
    for i in range(3):
        turn_left()

if __name__ == '__main__':
    main()

代码运行成功!

官方参考答案

"""
Program: Hospital Karel
Karel traverses 1st Street from west to east, building hospitals
wherever it encounters a beeper.
"""
from karel.stanfordkarel import *

def main():
    while front_is_clear():
        if beepers_present():
            build_hospital()
        safe_move()


def build_hospital():
    """
    Karel picks up supplies and builds a hospital.
    Pre-condition: Karel is on a beeper, representing a
        pile of supplies. Karel is facing east.
    Post-condition: Karel is standing at the bottom
        of the last column of the hospital, facing east.
    """
    # pick up supplies
    pick_beeper()
    do_one_column()
    move()
    do_one_column()


def do_one_column():
    """
    Karel builds a single column of a hospital.
    Pre-condition: Karel is facing east at the bottom
        of where we want to build a column.
    Post-condition: Karel is facing east at the bottom
        of the column it just built.
    """
    turn_left()
    put_three_beepers()
    return_to_base()
    turn_left()


def put_three_beepers():
    """
    Karel places three beepers in a row.
    Pre-condition: Karel is on the corner where we want
        to place the first beeper.
    Post-condition: Karel is on the corner where it
        placed the third beeper in a row.
    """
    put_beeper()
    move()
    put_beeper()
    move()
    put_beeper()


def return_to_base():
    """
    Karel turns around and goes to the wall.
    Pre-condition: Karel is at the end of the column
        it just built, facing north.
    Post-condition: Karel has returned to 1st Street,
        below the column is just built, facing south.
    """
    turn_around()
    move_to_wall()


def move_to_wall():
    while front_is_clear():
        move()


def safe_move():
    if front_is_clear():
        move()


def turn_around():
    turn_left()
    turn_left()


# Note: turn_right() is not called above but added for reference.
def turn_right():
    for i in range(3):
        turn_left()

    

if __name__ == '__main__':
    main()

官方代码还是比我的要简洁许多,且使用了拆分的思维,建造医院的过程拆分成:

(1)捡起第一个beeper

(2)往上建一列,回来

(3)往右走一步

(4)重复(2)

My coding style feedback

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值