יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (20 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.
שאלהQuestion 6
קפיצה עם כבידהJumping with gravity
כתבו simulate_jump(jump, gravity, frames) שמתחילה ב-y = 0, vy = jump (שלילי = למעלה), ובכל פריים: vy += gravity, y += vy, ואם y >= 0 נוחתים (y = 0, עוצרים). החזירו (max_height, landed_frame) כש-max_height חיובי (מעוגל לספרה אחת) ו-landed_frame הפריים שבו נחתו (מ-1), או None אם לא נחתו.
Write simulate_jump(jump, gravity, frames) starting at y = 0, vy = jump (negative = up), and each frame: vy += gravity, y += vy, and if y >= 0 land (y = 0, stop). Return (max_height, landed_frame) with max_height positive (rounded to one decimal) and landed_frame the frame of landing (from 1), or None if it didn't land.
איך הפלט אמור להיראות?What should the output look like?
(60.0, 31) (52.5, None)
main.py
רמזHint 1:top = min(top, y).top = min(top, y).
הפתרון שלנוOur solution
main.py
def simulate_jump(jump, gravity, frames):
y, vy, top =0.0, jump, 0.0for frame inrange(1, frames +1):
vy += gravity
y += vy
top =min(top, y)
if y >=0:returnround(-top, 1), frame
returnround(-top, 1), Noneprint(simulate_jump(-8, 0.5, 100), simulate_jump(-8, 0.5, 10))
פלטOutput
(60.0, 31) (52.5, None)
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (30 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.
שאלהQuestion 7
מכונת מצבים למשחקA game state machine
כתבו run(events) שמתחילה במצב "menu" ומעבדת רשימת אירועים: menu+start → play; play+pause → paused; paused+pause → play; play+die → over; over+start → play (וניקוד מתאפס); play+coin → ניקוד +1. אירועים אחרים מתעלמים. החזירו (state, score, best) כש-best הניקוד הגבוה ביותר שהושג.
Write run(events) starting in state "menu" and processing a list of events: menu+start → play; play+pause → paused; paused+pause → play; play+die → over; over+start → play (and score resets); play+coin → score +1. Other events are ignored. Return (state, score, best) with best the highest score reached.
איך הפלט אמור להיראות?What should the output look like?
('play', 1, 3)
main.py
רמזHint 1:מילון מעברים.A transitions dict.
רמזHint 2:עדכנו best אחרי כל מטבע.Update best after every coin.
הפתרון שלנוOur solution
main.py
MOVES = {("menu", "start"):"play", ("play", "pause"):"paused", ("paused", "pause"):"play", ("play", "die"):"over", ("over", "start"):"play"}
def run(events):
state, score, best ="menu", 0, 0for e in events:if state =="play"and e =="coin":
score +=1
best =max(best, score)
elif (state, e) in MOVES:if state =="over":
score =0
state = MOVES[(state, e)]
return state, score, best
print(run(["coin", "start", "coin", "coin", "pause", "coin", "pause", "coin", "die", "start", "coin"]))
פלטOutput
('play', 1, 3)
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (30 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.