Question: For problem #1, you must: Write a Python program Test, debug, and execute the Python program Submit a copy of your commented source code on-line

For problem #1, you must:

Write a Python program

Test, debug, and execute the Python program

Submit a copy of your commented source code on-line prior to class

(a) (30 pts.) A Teddy Bear Picnic

This problem involves creating a game with teddy bears. The game starts when I give you some bears. You can then give back some bears, but you must follow these rules (where n is the number of bears that you have):

If n is even, then you may give back exactly n/2 bears.

If n is divisible by 3 or 4, then you may multiply the last two digits of n and give back this many bears. (By the way, the last digit of n is n%10, and the next-to-last digit is ((n%100)/10).

If n is divisible by 5, then you may give back exactly 42 bears.

The goal of the game is to end up with EXACTLY 42 bears.

For example, suppose that you start with 250 bears. Then you could make these moves:

--Start with 250 bears.

--Since 250 is divisible by 5, you may return 42 of the bears, leaving you with 208 bears.

--Since 208 is even, you may return half of the bears, leaving you with 104 bears.

--Since 104 is even, you may return half of the bears, leaving you with 52 bears.

--Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and return these 10 bears. This leaves you with 42 bears.

--You have reached the goal!

(In Python) Write a recursive method to meet this specification:

def bears(n):

// Precondition: n must be an integer

// Postcondition: A true return value means that it is possible to win

// the bear game by starting with n bears. A false return value means that

// it is not possible to win the bear game by starting with n bears.

// Examples:

// bear(250) returns True (as shown above)

// bear(42) returns True

// bear(84) returns True

// bear(53) returns False

// bear(41) returns False

Hint: To test whether n is even, use the expression ((n % 2) == 0).

(b) (10 pts.) Discuss the time complexity of your code for problem.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!