Processing math: 100%

Prijavi problem


Obeleži sve kategorije koje odgovaraju problemu

Još detalja - opišite nam problem


Uspešno ste prijavili problem!
Status problema i sve dodatne informacije možete pratiti klikom na link.
Nažalost nismo trenutno u mogućnosti da obradimo vaš zahtev.
Molimo vas da pokušate kasnije.

if statement - practice

In this section we will only practice using the if statement and combining it with loops.

Tasks for exercise

Go to the end of a path and take only one ball

Karel should arrive at the end of the corridor, and only take the first ball on the way. The starting square never has a ball on it, and Karel initially does not carry any balls.

1
 
1
from karel import *
2
# write the program
3

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_if__take_first_ball_only)

Take the ball on the neighboring square

There is only one ball on the board. Karel and the ball are located on two adjacent squares with no wall between them (Karel is only one step appart from the ball, if he turns to the ball first). There may or may not be a wall between other squares. Karel should take the ball and he can finish on any square in the end.

As usual, run the program several times to test it on various examples.

One possible idea is that in each of the four directions, we try to make Karel go one step forward and pick up the ball. Various scenarios can occur in each of the four attempts:

  • it is possible that there are no squares in front of Karel in that direction

  • it is possible that there is a square in front of Karel, but there are no balls on it

  • it is possible that there is a square and that there is a ball on it

When trying the next direction, it is much simpler if we don’t have to take into consideration whether Karel has found a square without a ball in the previous direction he tried, or did not find a square at all. To simplify the next attempt, it is convenient for us that Karel finishes the previous attempt when he was on an empty square in the same state as when there was no square. When there is no square in the attempted direction, Karel will remain on the starting square, facing the attempted direction. To facilitate the continuation of the search, we can leave Karel on the same square facing the same direction when he returns from an empty adjacent square. In fact, it won’t do any harm if we do it also when Karel takes the ball (it is possible that Karel needlessly continues to search, but that will not cause any errors). Because we’ve brought Karel to the same state (position and orientation) after any of the three cases above, we know our new starting state exactly, for each subsequent attempt. After each attempted direction, we just need to turn Karel towards the next direction we will try to find the ball in (either to the left or to the right).

1
10
 
1
from karel import *
2
for i in range(4):
3
    if front_is_clear():
4
        move()
5
        # tell Karel to try to take the ball
6
        # tell Karel to go back to the starting square...
7
        # ... and turn towards the square at which he just tried
8
        # (as if he had not gone to that square at all)
9
    # tell Karel to prepare for the next attempt
10

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_if__take_neighboring_ball)

Follow the path

There is only one ball on the table, and Karel should take it. The way to the ball is not straight, but there are no intersections (there is always only one way to continue moving, even from the starting square).

1
 
1
from karel import *
2
... # write the program
3

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_if__take_ball_no_branches)

Sidetrack

There is only one ball on the table and Karel should take it. To get to the ball, Karel should go straight only when he can’t turn left or right (there will be no ambiguous crossroads where there is a path to both left and right).

1
 
1
from karel import *
2
... # write the program
3

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_if__p1_left_p2_right_p3_forward)

Go left wherever you can

There is only one ball on the table and Karel should take it. Karel will always reach the ball by turning left whenever he can, and going straight when he can’t go left (when he cannot go either left or straight, that means he has arrived). Karel is initially turned the way he should, and his first step is always straight forward.

1
 
1
from karel import *
2
... # write the program
3

Please try loading this page in HTML5 enabled web browsers. All the latest versions of famous browsers such as Internet explorer, Chrome, Firefox, Opera support HTML5.

(Karel_if_p1_left_p2_forward)