Hoppity!

Python

Public Domain

My solution to the facebook engineering puzzle: Hoppity

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python
import sys

def fbpuzzle(filename):
    """ Takes a filename argument and for each number in the text file
    it returns Hop, Hoppity, or Hop depending on the ability to divide the
    number by 3, 5, or both. """
    try:
        handler = open(filename)
        read_all = handler.readlines()
        for num in read_all:
                num = int(num)
                if num % 3 == 0 and num % 5 == 0:
                    print "Hop\n"
                elif num % 3 == 0:
                    print "Hoppity\n"
                elif num % 5 == 0:
                    print "Hophop\n"
    finally:
        handler.close()