r/dailyprogrammer • u/Cosmologicon 2 3 • Aug 26 '15
[2015-08-26] Challenge #229 [Intermediate] Reverse Fizz Buzz
Description
Imagine that I've written a program to solve a modified version of Fizz Buzz. My program takes as input some positive integers, like this:
2 5 4
These input numbers correspond to letters, in this case a, b, and c. Now, my program loops through all integers starting at 1, printing out one line at a time, each line containing one or more letters in alphabetical order. If the current number is divisible by 2, the line will contain a. If it's divisible by 5, it'll contain b. If it's divisible by 4, it'll contain c.
So for instance, when the loop reaches 2, my program will output a. When the loop reaches 8 it'll output ac. At 30 it'll output ab. At 7 no line will be output, not even a blank line. Thus the output will begin like this:
a
ac
b
a
ac
ab
ac
a
b
ac
a
abc
a
Your challenge is to reverse my program. Write a program that takes the beginning of the output from my program, and determines what input my program was given to produce it. There will be more than one possible answer, so find the solution with the smallest possible numbers.
Examples
Since this is Intermediate, it's okay to use brute force. As long as you can solve these examples in less than a minute, that's fine. But definitely test your program on the examples! (And don't worry about input or output format too much. Just do whatever's easiest for you to get the solutions.)
Example Input 1
a
b
a
a
b
a
Example Output 1
3 5
Example Input 2
b
be
ab
be
b
abe
b
Example Output 2
3 1 8 8 2
(Note that in this case, you can infer that there must be at least 5 numbers in the solution, because of the presence of the letter e, even though c and d don't appear. The numbers corresponding to c and d must be high enough for them not to have appeared yet.)
Example Input 3
a
b
c
d
a
ab
Example Output 3
6 9 10 11
Optional challenge input
Get the challenge input here. You probably won't be able to brute force this one. How fast can you make your program run on this input?
Thanks to u/Blackshell for suggesting this challenge in r/dailyprogrammer_ideas!
4
u/eatsfrombags Aug 26 '15
I thought this was super simple until I realized example 3 has double digit numbers. : (
3
u/daveinthecave Aug 27 '15
/u/Cosmologicon , can we assume that the number of outputs of this program are less than 24 (a - z) and if not, what should we assume are the input letters beyond the letter z?
4
u/Flynn58 Aug 26 '15
That makes this...significantly harder.
2
u/hutsboR 3 0 Aug 26 '15
/u/cosmologicon, I believe there's something wrong with Example 2's input or output. There's only three symbols in the input (
a,b,e) and five integers in the output. I may be misunderstanding something about the problem.14
u/kirsybuu 0 1 Aug 27 '15
Prolog, using clpfd (not plain brute-force)
Outputs:
6
u/eatsfrombags Aug 27 '15 edited Aug 28 '15
Python 3, brute force. I'm a rookie and would love feedback!
It seems to me like the hardest part of this challenge is knowing how large of divisors to check. If the expected output, the divisors, were restricted to 1-9, this would be much easier, or faster anyhow. But then example 3 has 10 and 11 as divisors. So then I had to check past 9, to catch 10 and 11. I checked as far as 20, just to do it. But it runs much faster if I just set the max to 11, since I know that's the farthest I need to check. If I set it to 99, example 2 kills Python. Am I making sense??
EDIT: Still brute force, but generators and improved checks speed it up significantly. Runs all the examples is a tenth of a second, but still can't handle the challenge. Also sort of cheating since I know the largest divisor to check, but if I could get around this I could probably solve the challenge...
Also, I'm new here - are the comments appreciated or helpful to anybody, or do they just make my code obnoxiously long?
6
u/krista_ Aug 27 '15
If this had blank lines, we could use Fourier series...
4
u/NoobOfProgramming Aug 26 '15 edited Aug 27 '15
spoiler but just thoughts and no solution
edit:
I would totally write some actual code and do the problem right now but i have to go to class :P
1
u/andriii25 Aug 27 '15
/u/Cosmologicon, can you elaborate what do you mean by smallest possible numbers? If the solutions would be 2-7 and 3-6 which would be the smallest and why? So how are the possible solutions in order?
3
u/a_Happy_Tiny_Bunny Aug 27 '15 edited Aug 27 '15
Haskell
Simple brute force. It solves all example inputs instantaneously.
There might be an off-by-one error in the ranges I generate. It doesn't show up for the inputs, but I'll quickcheck it later just in case.
3
u/Curtalius Aug 27 '15 edited Aug 27 '15
Python 3, trying to learn the language. My algorithm uses the fact that each line forms logic, a > b > c > d > 2a > 3a ==2b for example, and tests each portion of the logic, adjusting it's guesses as needed. I haven't got the unnused 'c' and 'd' from input 2 working yet, working on it. Solves challenge input in under a second. What would be the best way to check that it produces accurate results?
Edit: bug fixes, now works correctly for all example inputs and challenge input.
3
u/MuffinsLovesYou 0 1 Aug 27 '15 edited Aug 27 '15
I did a nice little answer to this one a while back in /r/dailyprogrammer_ideas, so Imma just paste that here.
Edit: the input has changed slightly, going to tweak the program and post the modifications.
Edit Edit: So this is a very different puzzle when you change the input and will require more than minor tweaks to adjust. The input from _ideas included the number you were counting up till. I'm going to have to solve this after work :P.
3
u/patrickwonders Aug 28 '15 edited Aug 28 '15
Common Lisp (solves as soon as it is possible to solve and without brute force):
This solution creates a closure which, when fed input lines, returns an answer as soon as there is one. The main loop then reads a line, feeds it to the closure, and returns an answer and the number of lines read.
As an example, given the input:
The program returns:
Indicating the answer as well as the number of input lines consumed.
The
reverse-fizzbuzzfunction itself is overloaded. If given a list, it uses each list entry as an input line. If given an input stream, it uses each input line as an input line. If given a string, it uses each line of the string as an input line. If given a vector, it generates the lines that the fizzbuzz program would generate given those inputs.3
u/schepens83 Aug 29 '15 edited Aug 29 '15
Ruby
My first submit to a challenge! Im trying to learn ruby and started using this reddit as a means to a week ago. The challenge input is beyond this program. Speed is also not that great.
Suggestions and feedback are welcome!
2
u/maukamakai Aug 26 '15
Clojure
So I don't have a solution yet, but I have some code that will lazily generate output for a given set of starting numbers.
And testing it with the given examples:
I think my approach to solving this will initially be brute force, lazily generating each combination and checking against the known solution.
Any advice on more idiomatic clojure code would be helpful. Specifically I feel that there should be a better way to retrieve map values given a seq of keys other than what I'm doing in 'map-nums'.
2
u/skeeto -9 8 Aug 27 '15
C, brute force in parallel using OpenMP. Instant result on all the examples, and I'm running the challenge input on an 8-core machine to see how long it will take. So far I've searched all possible solutions up to a maximum of 19.
This is a really hard one because of how the different parts of the solution are twisted together. If empty lines were included, it would be trivial, but without them it's very difficult.
2
u/ReckoningReckoner Aug 27 '15
Ruby. Runs#1-3 instantaneously. A mixture or brute force and some math. Gonna revisit this again tomorrow. Many dumb mistakes.
Output:
2
u/gabyjunior 1 2 Aug 30 '15
Constraint programming in C, list of constraints is built for each variable while reading input. Then value is set for variable a and the program is recursively searching for other variables in alphabetical order, reducing the search space by computing the most restrictive lower/upper bounds from list of constraints.
Output for challenge:
The 3 examples and challenge are solved instantly. To see the list of constraints for each variables you can set the PRINT_BOUNDS define to 1 and recompile.
Example with input 2
2
u/[deleted] Aug 27 '15
Since some of y'all are having trouble figuring out a non-bruteforce method, I thought I'd share my thoughts while I'm trying to figure it out myself.
1
u/purmou Aug 26 '15 edited Aug 26 '15
Below are some preliminary thoughts on how to approach this. Not a solution.
1
u/featherfooted Aug 27 '15
Python
Weighing in at 128 lines, I decided not to dump the whole thing into a code block here so I uploaded it to GitHub here.
I implemented a BFS over edit distance from the "trivial" solution
1 1 1 1 ...for however many variables are detected (up to 52).The longest solution (6-9-10-11) took 1 minute 33 seconds to run on my laptop, so I'm a little bit off from the under-1-minute mark.
1
u/battleguard Aug 28 '15 edited Aug 28 '15
C# (Really fun challenge sorry I don't mess with input but I like practicing unit testing while doing these challenges)
1
u/ReckoningReckoner Aug 30 '15 edited Sep 01 '15
Ruby non brute force. Solves all inputs in under 1.3 seconds. EDIT: Solves everything now, cleaned up code EDIT: realized I uploaded an older version of program
Output:
1
u/[deleted] Sep 25 '15
Common Lisp using Screamer
solves the challenge in ~half a second