Today, we looked at logic problems in Coding Bat in Computer Science 1. Students had to use Boolean logic operators and conditional if/else an if/elif/else statements.

#### Cigar Party

http://codingbat.com/prob/p195669

Here is the setup of the problem:

*When squirrels get together for a party, they like to have cigars. A squirrel party is successful when the number of cigars is between 40 and 60, inclusive. Unless it is the weekend, in which case there is no upper bound on the number of cigars. Return True if the party with the given values is successful, or False otherwise.*

def cigar_party(cigars, is_weekend): if is_weekend == True and cigars >=40: return True elif is_weekend == False and cigars >=40 and cigars <=60: return True else: return False

Here, we used the Boolean AND operator to check the number of cigars in relation to the value of is_weekend.

Lines 2 – 3 address if it is a weekend AND are there at least 40 cigars. If so, a TRUE is returned.

Lines 4 – 5 address if is is NOT a weekend AND there are between 40 and 60 cigars. If so, a TRUE is returned.

Lines 6 – 7 address everything else as the default case and return a FALSE.

#### Date Fashion

http://codingbat.com/prob/p129125

Here is the setup of the problem:

*You and your date are trying to get a table at a restaurant. The parameter “you” is the stylishness of your clothes, in the range 0..10, and “date” is the stylishness of your date’s clothes. The result getting the table is encoded as an int value with 0=no, 1=maybe, 2=yes. If either of you is very stylish, 8 or more, then the result is 2 (yes). With the exception that if either of you has style of 2 or less, then the result is 0 (no). Otherwise the result is 1 (maybe).*

def date_fashion(you, date): if you <=2 or date <=2: return 0 elif you >= 8 or date >= 8: return 2 else: return 1

Here, the order of the conditions is EXTREMELY important. Let’s look at the case of you = 9 and date = 1.

In the code above, we would reach the first the first conditional statement with you <=2 is FALSE but date <=2 is TRUE. Since a Boolean OR is being used, only one of them must be true for the associated code to run. So, we return a value of 0 and exit the conditional loop, which is the correct response for the problem.

Now, let’s change the code to the following:

def date_fashion(you, date): if you >= 8 or date >= 8: return 2 elif you <=2 or date <=2: return 0 else: return 1

All that we have done is swap the conditions between lines 2 and 4 with each other. Let’s examine the case of you = 9 and date = 1 again.

We enter at the first conditional statement with you >= 8 is TRUE but date >= is FALSE. Since a Boolean OR is being used, only one of them must be true for the associated code to run. So, we return a value of 2 and exit the conditional loop, which is the incorrect response for the problem.

The order of our conditional statements is EXTREMELY important!

#### Squirrel Play

http://codingbat.com/prob/p135815

Here is the setup of the problem:

*The squirrels in Palo Alto spend most of the day playing. In particular, they play if the temperature is between 60 and 90 (inclusive). Unless it is summer, then the upper limit is 100 instead of 90. Given an int temperature and a boolean is_summer, return True if the squirrels play and False otherwise.*

def squirrel_play(temp, is_summer): if is_summer == True and temp >= 60 and temp <= 100: return True elif is_summer == False and temp >= 60 and temp <= 90: return True else: return False

Lines 2 – 3 check if it is summer. If it is, we then check to see if the temperature is between 60 and 100 degrees inclusive. If it passes all 3 of these tests (Boolean AND), we return the value of TRUE.

Lines 4 – 5 check if it is NOT summer. If it is NOT summer, we then check to see if the temperature is between 60 and 90 degrees inclusive. If it passes all 3 of these tests (Boolean AND), we return the value of TRUE.

Lines 6 -7 are the default case if neither of the above tests can be passed and returns the value of FALSE.

#### Caught Speeding

http://codingbat.com/prob/p137202

Here is the setup of the problem:

*You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday — on that day, your speed can be 5 higher in all cases.*

def caught_speeding(speed, is_birthday): if is_birthday == True and speed <= 65: return 0 elif is_birthday == False and speed <= 60: return 0 elif is_birthday == True and speed >= 66 and speed <=85: return 1 elif is_birthday == False and speed >=61 and speed <= 80: return 1 else: return 2

Much like the problem before earlier, we a set of Boolean AND statements. When using the Boolean AND we must remember that ALL conditions must be satisfied for the statement to be considered TRUE and to thereby execute the associated instructions.

Lines 2 – 3 and 6 – 7 are working with when is_birthday is TRUE while lines 4 – 5 and 8 – 9 are working with when is_birthday is FALSE. All of these lines are using comparative operators for the speeds.

## One thought on “Coding Bat Logic Problems – Day 1”