Skip to content

Instantly share code, notes, and snippets.

@ferreiro
Last active March 6, 2016 22:50
Show Gist options
  • Save ferreiro/76562fb1fcd39ebf65ac to your computer and use it in GitHub Desktop.
Save ferreiro/76562fb1fcd39ebf65ac to your computer and use it in GitHub Desktop.
class CracklePop(object):
def __init__(self, left, right):
self.left = left # Left index
self.right = right # Right index
# Private methods
def valid_bounds(self):
"""
Checks that the left and right bounds
are valid. Returns False when the given numbers are not
"integers" or the bounds are not valid
"""
left = self.left
right = self.right
return (isinstance(left, int) and isinstance(right, int) and
left >= 0 and left <= right)
def composed_message(self):
"""
Returns string that contains a series that stars on left and finish on right.
When number is divisible by 3, prints Crackle. If it's divisible by 5 Pop. And CracklePop if both cases.
"""
output_msg = ""
for index in range(self.left, self.right + 1):
if index % 3 != 0 and index % 5 != 0:
output_msg += str(index) # only prints number
else:
if index % 3 == 0:
output_msg += "Crackle"
if index % 5 == 0:
output_msg += "Pop"
output_msg += "\n"
return output_msg
# Public methods
def get_crackepop_msg(self):
"""
When bounds are valid, returns the string with the correct series.
If not, returns a failure message.
"""
if not self.valid_bounds():
return 'Bad left and right bounds.'
output_msg = self.composed_message()
return output_msg
test1 = CracklePop(1, 100)
print test1.get_crackepop_msg()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment