Coding Bat Logic Problems – Day 2

Last class we looked at the first of 4 logic problems on Coding Bat in Computer Science 1 ( http://funmultiplies.com/coding-bat-logic-problems-day-1 ). We’re now looking at the last 4 logic problems of that Coding Bat set in CS1 as we get ready to depart for Spring Break.

alarm clock

http://codingbat.com/prob/p119867

Here is the setup of this problem:

Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, …6=Sat, and a boolean indicating if we are on vacation, return a string of the form “7:00” indicating when the alarm clock should ring. Weekdays, the alarm should be “7:00” and on the weekend it should be “10:00”. Unless we are on vacation — then on weekdays it should be “10:00” and weekends it should be “off”.

def alarm_clock(day, vacation):
  if (day == 0 or day == 6) and vacation == True:
    return "off"
  elif (day == 0 or day == 6) and vacation == False:
    return "10:00"
  elif (day == 1 or day == 2 or day == 3 or day == 4 or day == 5) and vacation == False:
    return "7:00"
  else:
    return "10:00"

This one is not hard, but just involved with conditional statements. The major things to keep an eye out for are the spelling of the return “off” is all lower-case and the week starts on day 0 with Sunday.

love 6

http://codingbat.com/prob/p100958

Here is the setup for this problem:

The number 6 is a truly great number. Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6. Note: the function abs(num) computes the absolute value of a number.

def love6(a, b):
  if (a) == 6 or (b) == 6:
    return True
  elif (a) + (b) == 6 or (b) + (a) == 6:
    return True
  elif (a) - (b) == 6 or (b) - (a) == 6:
    return True
  else:
    return False

I personally do not like this problem because of the “Note” at the end. It throws many programmers off. Yes, you can solve this using absolute values, but it’s not necessary.

Once you get other that “distractor”, this is a simple conditional logic problem.

In 1 to 10

http://codingbat.com/prob/p158497

Here is the setup for this problem:

Given a number n, return True if n is in the range 1..10, inclusive. Unless outside_mode is True, in which case return True if the number is less or equal to 1, or greater or equal to 10.

def in1to10(n, outside_mode):
  if outside_mode == False and (n >= 1 and n <= 10):
    return True
  elif outside_mode == True and (n <= 1 or n >= 10):
    return True
  else:
    return False

This is a pretty simple conditional logic problem. If the Boolean flag is False, we are looking for numbers between 1 and 10 (inclusive) and if the Boolean flag is True, we are looking for numbers outside of 1 and 10 (inclusive).

On this particular question, I awarded extra credit if my students could explain mathematically why when the outside_mode Boolean flag is True, the Boolean operator must be OR.

The reason is that while the Boolean flag is False, num is to be between 1 and 10 inclusive. A number can be simultaneously between 1 and 10. As such, the Boolean AND is used in this statement.

Conversely, when the Boolean flag is True, num is to be outside of 1 and 10 inclusive. A number CANNOT be simultaneously less than 1 and greater than 10. As such, the Boolean OR is needed for this statement.

near ten

http://codingbat.com/prob/p165321

Here is the setup for this problem:

Given a non-negative number “num”, return True if num is within 2 of a multiple of 10. Note: (a % b) is the remainder of dividing a by b, so (7 % 5) is 2.

def near_ten(num):
  if num % 10 == 0 or num % 10 == 1 or num % 10 == 2 or num % 10 == 8 or num % 10 == 9:
    return True
  else:
    return False

This particular one needs a little bit of explanation on the algorithm that was used. The general concept is pretty straightforward, but the formula may not be.

Here, we are looking at the modulus of num by 10. In this case, that will always return a 0, 1, or 2 if the number is a multiple of 10, or 1 greater than a multiple of 10, or 2 greater than a multiple of 10 respectively.

On the other side, we will always return an 8 or 9 if the number is 2 less than a multiple of 10, or 1 less than a multiple of 10 respectively.

Beyond the algorithm, the problem is a simple logic conditional program.

Leave a Reply

Your email address will not be published. Required fields are marked *