Another little puzzle to keep you thinking. I saw this problem during the reign of the dinosaurs, and thought it was pretty clever.
El problemo
Write some code that prints 543210012345.
You code must meet the following conditions:
- You must use single a loop (eg.
fororwhile) and each iteration of your loop may only print onecharacterdigit. - No
ifstatements, no ternary operators. - No strings, no arrays. Numbers only.
- You may only use at most one variable.
- You may use language-specific functions / methods (but it’s not necessary).
As always, use any programming language you like and post up your answers in the comments. Once there have been several attempts, I’ll post up my suggested answers. It’ll be interesting to see the different ways people do this, and the various optimization techniques that are utilized.
Good luck!
Note: When leaving a comment, indent your code blocks by four space or one tab so the formatting is saved and you don’t have to escape characters. For inline code, wrap it in backticks, eg. `This is some code`
47 Comments so far
Leave a commentPages: « 1 [2] Show All
Oooh, good point Jonathan. (Although my Ruby solution still works then.)
for (int i = 11; i >= -11; i -= 2) putchar(’0′ + (abs(i) >> 1));
determined by agnoster on July 13, 2007 5:09 pm | Permalink
Hi! Thanks for commenting.
I did mean integer, but during a brief moment of insanity, I wrote character. I’ve fixed that up now. Thanks for pointing that out.
expressed by Ankur on July 13, 2007 5:18 pm | Permalink
not so nice as other solutions though
reported by Michal on July 13, 2007 6:34 pm | Permalink
My humble suggestion is adaptable as much as the dinosaurs were - died unable to adapt to the changing environment… urghh requirements, right?
print 54321001.times { print 12345 }The .times method executes a single loop. There is no statement saying that I should write the loop myself…
No algorithm magic, ugly? Yes, but it is simple, does not make me think, it is readable and works
Regards!
reported by Lunohodov on July 13, 2007 7:00 pm | Permalink
I forgot to mention that Ruby is the language in use… Sorry for that!
reported by Lunohodov on July 13, 2007 7:05 pm | Permalink
Lunohodov, your suggestion is quite clever (I’d almost call it cheeky
) but you’re only allowed to print one digit in each iteration of your loop. Perhaps that wasn’t clear enough.
Nice try, though.
voiced by Ankur on July 13, 2007 7:17 pm | Permalink
My solution in Ruby (without the disguised arrays that Agnoster and Michal used
)
revealed by sphynx on July 13, 2007 7:43 pm | Permalink
My solution in C++, fairly scalable:
stated by Kris Malfettone on July 13, 2007 11:42 pm | Permalink
Kris, that’s clever!
My C++ isn’t that great, but does that code output
0twice? It seems to me that it would output54321012345(with only one0).composed by Ankur on July 13, 2007 11:55 pm | Permalink
Yeah it does output zero twice. That is actually what the +1 is for in the initializer of the for loop. Without the +1, it would only output zero once.
composed by Kris Malfettone on July 14, 2007 12:58 am | Permalink
Sphynx: If you’re going to consider k.upto(n) to be a “disguised array” (which it’s not - it’s possible it’s a disguised range but it’s definitely not an array - and at any rate I strongly doubt it, considering the implementation would make more sense as a
forloop), then you have to consider yourto_s(which seems totally unnecessary to me) to be the use of a string, so… disqualified.But if you insist, can you at least agree this is not a “disguised array”?
-11.step(11,2){|x|print x.abs>>1}Kris: You and I must have very different notions of the word “scalable”. Almost all the other solutions (apart from the first one, and the ones that try to fake it) can handle fairly arbitrary ranges, whereas your solution doesn’t work anywhere past n=500 (well… depending on the limits of your machine and C implementation - but exponentiation seriously stunts your range of attack). Besides which you’re using some very slow functions. log is slower than pow is slower than dividing is slower than bitshifting.
written by agnoster on July 14, 2007 1:40 am | Permalink
Now you guys are taking it a bit too seriously.
No one cares about scalability or performance. I think all anyone cares about is “neat”.
All of the solutions have dealt with the rules in an interesting way (sometimes avoiding the rules altogether). I think, unless we have any more new solutions (and not just rehashes of what we’ve seen already) we should stop here.
Sorry about the poor formatting last night. I assumed the preview would give me some WYSIWYG but I guess not. For the next challenge I’ll just use backticks and trust the server will format it right.
BTW Nice “Python sucks, Ruby rules” jab earlier Agnoster…
posted by Jonathan Wight on July 14, 2007 1:49 am | Permalink
Jonathan - never said that. Just said whitespace sensitivity is a nightmare for code transmission. I never said “Python sucks” - it’s a fantastic language, fairly performant, clean-ish and a good selection of libraries. If you have production work to do (and you can’t afford to write it in C), if performance matters, if you need access to lots of libraries, go for Python (or Java). If you want to do something clever, succinct, and beautiful, and then post it in a blog comment without it getting mangled… well, the Ruby’s a good choice.
declared by agnoster on July 14, 2007 2:28 am | Permalink
Heh, fair enough.
So no more entries? I’m still waiting for the version that calculates PI to enough digits to find the number sequence…
professed by Jonathan Wight on July 14, 2007 2:35 am | Permalink
Agnoster: ‘Range’ or ‘Array’, it still isn’t “Numbers only.”
But I’m kindof new to Ruby, and looking carefully, I have to admit it wasn’t a range… more like a
for-loop in disguise.And the
to_s, well, that could have been left out, I guess… But what language doesn’t use a (behind the scenes) conversion to a string before printing? Like all the"%d"’s?announced by sphynx on July 14, 2007 4:34 am | Permalink
Sphynx: If we’re talking “behind the scenes”, they all use strings to output (unless you count my C solution with putchar differently), but they also all use conditionals for loops, and multiple registers to hold different values when you get down into the nuts and bolts.
determined by agnoster on July 14, 2007 4:52 am | Permalink
Jonathan,
Don’t worry, I’ve added javascript Markdown to the live preview so it’ll be pretty much WYSIWYG now. I’ve made it lazy-loading though, so it may take a second after you type something for it to start formatting with Markdown.
Just a reminder,
`Inline code wrapped in backticks (the key to the left of the "1")`So hopefully there’ll be no more formatting issues.
published by Ankur on July 14, 2007 11:15 am | Permalink
Here’s another solution (in C).
stated by Qwerty Denzel on July 14, 2007 12:15 pm | Permalink
Ooh, I like Qwerty Denzel’s solution.
Here’s my solution (no floating point, no explicit abs())
reasonded by Devin Coughlin on July 14, 2007 1:49 pm | Permalink
for (float i = 5.0; i > -6.0; i -= 0.99 ) {
printf(”%d”, abs((int)i));
}
spoken by Tom Robinson on July 16, 2007 5:38 am | Permalink
recorded by Jan on July 16, 2007 8:25 pm | Permalink
#include <stdio.h> main() { long long i=543210012345LL; while (i > 0) { putchar((i%10)+'0'); i /= 10; } putchar('\n'); }published by Robin on July 18, 2007 9:04 am | Permalink
Here’s kind of a hacky C# solution that i through together in about 30 seconds:
recorded by Kris Broman on August 29, 2007 4:55 am | Permalink
Here is another solution that I believe is unique from all of the other solutions seen so far.
published by Chris Braun on September 22, 2007 4:54 pm | Permalink
Using Mathematica:
FromDigits[ Abs[ Join [ NestList[# - 1 &, 5, 5], NestList[# - 1 &, 0, 5] ] ] ]voiced by Prem Thomas on October 17, 2007 8:06 am | Permalink
Not that you asked for a recursive version, but…
#! /bin/python def pal( n ): print n, try: 1/n pal( n-1) except: pass print n, pal(5)professed by Glenn Franxman on October 21, 2007 6:59 pm | Permalink
And for completeness, here it is again, but in erlang:
filename:pal.erl
-module( pal ) .
-export( [duh/1] ).
duh( -1 ) -> 0;
duh(N) -> io:format( “~p”,[N]), duh(N-1), io:format(”~p”,[N]).
pal:duh(5).
proclaimed by Glenn Franxman on October 21, 2007 7:03 pm | Permalink
Here is a solution using Turing
for i:1..12
put (abs(i-(13-i))-1)/2 ..
end for
voiced by Anonymous on December 10, 2007 7:07 am | Permalink
system(”curl -s http://lipidity.com/development/programming-challenge-3/“);
reported by Anon on December 16, 2007 9:40 pm | Permalink
public class chal3{ public static void main(String[] args){ double i = 5; while (i > -6){ System.out.print(Math.abs((int)i)); i = i - .99999; } System.out.println(); } }Totally taking advantage of rounding errors. Any port in a storm.
Anyway. Java is totally not meant for this. Half of the code is wasted on constructors and other junk. Which lead to a breakthrough.
public static void main(String[] args){ System.out.println(args[0]); }Now that’s what I call ENTERPRISE!
mentioned by Sean on December 17, 2007 4:18 pm | Permalink
Pages: « 1 [2] Show All
Leave a comment