Facebook puzzles: (II, Hoppity)

Un nuevo rompecabezas, un poco más complicado:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.kodegeek.blog.jobsearch.facebook.hoppity;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
/**
 * To help test whether your puzzle submissions fit the guidelines, try this simple test puzzle.
 * Your solution must follow the guidelines like any other puzzle.
 * Write a program that takes as input a single argument on the command line.
 * This argument must be a file name, which contains a single positive integer.
 * The program should read this file and obtain the integer within, and then output a sequence of strings
 * based upon the number (details below).
 * @author josevnz@kodegeek.com
 *
 */
public final class Hoppity {
 
	/**
	 * The input file will contain a single positive integer (in base 10) expressed as a string using standard ASCII text
	 * (e.g. for example, the number "15" but without the double quotes).
	 * This number may or may not be padded on either side with white space.
	 * There will be no commas, periods, or any other non-numeric characters present within the number.
	 * The file may or may not terminate in a single new line character ("\n")
	 * @param file file with the number
	 * @return integer
	 * @throws IOException
	 */
	private static int readNumber(File file) throws IOException {
		int aNumber = -1;
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			// Trim any spaces and then try to parse the number
			aNumber = Integer.parseInt(reader.readLine().replaceAll("\\s*", ""));
		} catch (IOException ioExp) {
			throw ioExp;
		} finally {
			if (reader != null) {
				reader.close();
			}
		}
		return aNumber;
	}
 
	/**
	 * @param args args[0] = Full path to input file
	 */
	public static void main(String[] args) throws Exception {
		if (args != null && args.length == 1 && args[0] != null) {
			File numFile = new File(args[0]);
			if (numFile.exists() && numFile.canRead()) {
				int number = readNumber(numFile);
				for (int i = 1; i < = number; i++) {
					boolean mod3 = i % 3 == 0;
					boolean mod5 = i % 5 == 0;
					if (mod5 && mod3) {
						System.out.println("Hop");
					} else if (mod3) {
						System.out.println("Hoppity");
					} else if (mod5) {
						System.out.println("Hophop");
					} else {
						continue;
					}
				}
			}
		}
		return;
	}
}

Lo más complicado aquí es asegurarnos que el número pueda ser leido si tiene espacios. No hay otras restricciones aparentes.

No muestro el wrapper ni el archivo de compilación Ant ya que son muy parecidos al del problema anterior.

Veneblogs: , , ,
Blogalaxia: , , ,

To2blogs: , , ,

Technorati: , , ,

Del.icio.us: , , ,