r/dailyprogrammer • 2 0 • Mar 19 '17

Weekly #27 - Mini Challenges

So this week, let's do some mini challenges. Too small for an easy but great for a mini challenge. Here is your chance to post some good warm up mini challenges. How it works. Start a new main thread in here.

if you post a challenge, here's a template we've used before from /u/lengau for anyone wanting to post challenges (you can copy/paste this text rather than having to get the source):

**[CHALLENGE NAME]** - [CHALLENGE DESCRIPTION]

**Given:** [INPUT DESCRIPTION]

**Output:** [EXPECTED OUTPUT DESCRIPTION]

**Special:** [ANY POSSIBLE SPECIAL INSTRUCTIONS]

**Challenge input:** [SAMPLE INPUT]

If you want to solve a mini challenge you reply in that thread. Simple. Keep checking back all week as people will keep posting challenges and solve the ones you want.

Please check other mini challenges before posting one to avoid duplications within a certain reason.

71 Upvotes

45 comments sorted by

View all comments

Show parent comment

3

u/thorwing Mar 19 '17

Java 8

public static void main(String[] args) throws IOException{
	Files.lines(Paths.get("enable1.txt")).filter(w->w.length()>4&&isRollerCoaster(w)).forEach(System.out::println);
}
private static boolean isRollerCoaster(String w){
	for(int signum1 = 0, i = 1, signum2; i < w.length(); i++, signum1 = signum2)
		if((signum2 = Integer.signum(w.charAt(i)-w.charAt(i-1))) == signum1 || signum2 == 0)
			return false;
	return true;
}

Too much to print -> 11385 results

1

u/thorwing Mar 20 '17

I think I like a recursive version more

public static void main(String[] args) throws IOException{
	System.out.println(Files.lines(Paths.get("enable1.txt")).filter(w->w.length()>4&&isRollerCoaster(w,0)).count());
}
private static boolean isRollerCoaster(String w, int signum){
	if(w.length() == 1) return true;
	int cur = Integer.signum(w.charAt(1)-w.charAt(0));
	return cur == signum || cur == 0 ? false : isRollerCoaster(w.substring(1), cur);
}