Friday, December 11, 2009

Python solution for the Hoppity problem

An entry in the Code Snippet series.

Here's a Python solution to the Hoppity problem.

How to set it up:
Copy the script below into a shell script.
Copy the code below into a file, Hoppity.py
Make a data file that contains the number, as required in the Hoppity problem.

How to run it:
Run the script, give it the path and name to your data file.

The script:
echo "Tell it the path to your data file, i.e. /home/rick/FaceBook_Puzzles/data.txt"
python3 Hoppity.py

The code:
import sys

#input_file = open("/home/rick/FaceBook_Puzzles/data.txt",'r')
line = sys.stdin.readline()
input_file = open(line.strip(),'r')

as_string = input_file.read()
the_val = eval(as_string)
for idx in range(1, (the_val + 1)):
if ( ((idx % 3) == 0) and ((idx % 5) == 0)):
print ("Hop")
elif ((idx % 3) == 0):
print ("Hoppity")
elif ((idx % 5) == 0):
print ("Hophop")

No comments: