Exercise: Pedestrian Movement

Let's create an optional type 'car' variable which determines the value of a 'pedestrian' variable.

Problem Statement

In this exercise, we will try to recreate a pedestrian crossing at a traffic stop. You have to write the pedestrian() function which takes a car variable as its argument. The car variable will be of the option type.

If there is no car (the value of car is None), the function will return “Cross”.

If the Some() constructor for car gets the string, “Moving”, as its argument, the function will return “Wait”.

For all other cases of car, the value returned by pedestrian() will be “Check”.

Sample Input

1. car: None

2. car: Some("Moving")

3. car: Some(anyString)

Sample Output

1. pedestrian(): "Cross"

2. pedestrian(): "Wait"

3. pedestrian(): "Check"

Coding Challenge

Think carefully about the logic behind this exercise before jumping to the implementation. You only need to create the pedestrian() method which returns the correct string based on the value of car.

car has already been created. You do not need to worry about it.

If you feel stuck, you can always refer to the solution review in the next lesson.

Good luck!

Get hands-on with 1200+ tech skills courses.