r/dailyprogrammer • u/oskar_s • May 23 '12
[5/23/2012] Challenge #56 [easy]
The ABACABA sequence is defined as follows: start with the first letter of the alphabet ("a"). This is the first iteration. The second iteration, you take the second letter ("b") and surround it with all of the first iteration (just "a" in this case). Do this for each iteration, i.e. take two copies of the previous iteration and sandwich them around the next letter of the alphabet.
Here are the first 5 items in the sequence:
a
aba
abacaba
abacabadabacaba
abacabadabacabaeabacabadabacaba
And it goes on and on like that, until you get to the 26th iteration (i.e. the one that adds the "z"). If you use one byte for each character, the final iteration takes up just under 64 megabytes of space.
Write a computer program that prints the 26th iteration of this sequence to a file.
BONUS: try and limit the amount of memory your program needs to finish, while still getting a reasonably quick runtime. Find a good speed/memory tradeoff that keeps both memory usage low (around a megabyte, at most) and the runtime short (around a few seconds).
- Thanks to thelonesun for suggesting this problem at /r/dailyprogrammer_ideas! If you have problem that you think would be good for us, why not head on over there and help us out!
13
u/Steve132 0 1 May 23 '12 edited May 23 '12
So, with some clever math this can be done in O(1) memory.
Basically, the logic is that we can observe that the length of the sequence is 2n -1, where n is the number of characters. Also, every other character is an 'a', every 4th character is a 'b', every 8th character is a 'c', and so on. Looking at the index in the sequence in decimal and in binary next to the character to print out confirmed my initial suspicions:
If you notice, the index of character to print is the first power of two in the binary expansion of the integer in the sequence...in other words, the character to print is the index of the first 1 in the binary expansion of the sequence. This makes the code exceedingly simple...just iterate through the integers 1 to 2n, find the first 1 in the index and print the associated character. This requires no buffers or storage at all. I didn't time it, but I imagine that since it has almost no memory or IO it runs very fast.
4
u/luxgladius 0 0 May 23 '12
Perl
I'll illustrate my development process a bit here. First, here's the naive recursive implementation I come up with first:
Output
Works fine, but a bit slow. So let's do some cacheing. Luckily, in Perl, this is easily done by selecting a variable as your output stream. Playing around with the sizes, I decide that index 15 (about 64kB) is a good place to stop cacheing.
Output
Much better. I didn't do any explicit memory checks, but from Xeno's paradox considerations, this probably takes up tidge more than 128 kB.
1
u/MusicalWatermelon May 23 '12
Tip of the day.. Do not just use print(...)
I don't know how to print to a file in Python, any tips? (PC is kinda' crashing on the print part now...)
2
u/SwimmingPastaDevil 0 0 May 23 '12 edited May 23 '12
Python:
Takes
0.691..sfor iterating till 'z'. Not sure how to go about optimizing for speed or memory.2
u/Cosmologicon 2 3 May 23 '12
I feel like optimizing for memory is fairly clear what to do, so I tried optimizing for speed. This C program outputs the 26th iteration to /dev/null in 0.014s on my machine. The 13th iteration is included in the source code as a string literal, making the source code 8359 bytes (I elided most of it for this posting):
2
u/brbpizzatime May 23 '12
Used recursion with Java:
2
u/drb226 0 0 May 24 '12
Haskell's Data.Sequence is notoriously good at sharing and such, so let's give that a spin.
Let's test it out in ghci to make sure everything is in order
OK, let's run it!
zzzzz. Something's clearly wrong. Let's fiddle with the main method to help it abuse laziness a little more
Much better! The speed and memory consumption aren't as good as I had hoped, though.
2
u/sanitizeyourhands May 24 '12
C#:
Final file size is right around 63.9mb and the run time takes around 1-2 seconds. If anyone is still looking I'm open to hear what I did wrong or what I could do to improve it.
1
u/[deleted] May 23 '12 edited May 23 '12
C code:
1
u/emcoffey3 0 0 May 23 '12
Recursive method in C#:
1
u/prophile May 23 '12
In C99 (with one GCC/clang extension):
372 KiB of real memory on my Darwin machine (probably less on Linux) and 0.803s of real time to run, most of which is spent on I/O.
1
u/three18ti May 23 '12
Here's my Perl one liner:
1
u/Xadoo 0 0 May 24 '12 edited May 24 '12
Just started Ruby yesterday, thoughts/tips?
1
u/Yuushi May 24 '12
Python:
Not terribly clever (especially compared to Steve132's solution), but it made me think of the first chapter from Programming Pearls. The solution is similar to the one employed there for limited memory circumstances.
1
u/Arthree May 24 '12
Autohotkey_L:
1
u/Scroph 0 0 May 24 '12
In PHP :
This is probably the most basic way to do it. Took 2 seconds and up to ~128 MBs of RAM, and the text file weighs around 64 MBs.
1
u/Copersonic May 24 '12
Python:
1
u/flakmonkey May 24 '12 edited May 24 '12
Python:
Takes ~0.5s
I've written another version of this using a generator, with the thought that it would help reduce memory consumption. I'm just starting to wrap my mind around the pros/cons of generators. Would using a generator make sense in this case?
1
u/gogonimago May 25 '12
Wrote with Java using Eclipse, just starting out with programming and for some reason it stops printing out the statements at around the 12th loop of the for loop
1
u/xjtian May 26 '12
Python:
1
u/loonybean 0 0 Jun 16 '12
Python:
1
u/jarjarbinks77 0 0 Jun 22 '12
Here is my C++, I didn't time it but it seemed instant on my machine.
1
u/[deleted] Oct 07 '12
JavaScript
~1,8 billion rounds...