Printing 1 to 1000 without loop or conditionals
stackoverflow.com
stackoverflow.com
That then inspires another question: given a function, foo(), that we wish to call exactly 1000 times, what is the way to do it in the least amount of code given that we are only allowed the following:
1. calling foo().
2. defining new functions, whose bodies consist entirely of calls to previously defined functions or to foo.
3. calling those new functions.
For computing amount of code, let's take it as 1 line per function call, and 1 line per function defined.
So, for example (in pseudocode):
function foo_50
foo
foo
...
foo
foo_50
foo_50
...
foo_50
where there are 50 foo's in foo_50, and 20 calls to foo_50, would be a program of length 71. Breaking it down by powers of two: function foo_2
foo
foo
function foo_4
foo_2
foo_2
...
function foo_512
foo_256
foo_256
foo_512
foo_256
foo_128
foo_64
foo_32
foo_8
would have length 33.Is that the best we can do? How about for a general N? How about writing a program that given N writes the optimal program in the above form for N?
def bar(baz):
baz()
baz()
bar(bar(bar(bar(foo)))) and bar(bar(foo)) and foo()
for N=21, for example. Actually, you can make it two lines if your goal is really to minimize LOC: def bar(baz): baz() and baz()
bar(bar(bar(bar(foo)))) and bar(bar(foo)) and foo()Same goes for `bar(bar(...)) and bar()`; all the `and`s should be commands instead in my solution above. Of course, this is Python-specific.
It's not that using a random number generator is a cheat, it's that it is a (pointless?) lateral move rather than a solution.
That's the cheat. You don't approve. Others might.
Pick whatever you like most.
Don replies with "why it never occurred to me to try".
This is exactly how I feel when I read something like this.
void f(int n){
printf("%d\n",n);
f(n+1);
}
int main(){
f(1);
} int main()
{
printf("13 47\n");
return 0;
}
:DThis one (not mine):
#include <stdio.h>
void nothing(int);
void next(int);
void (*dispatch[2])(int) = {next, nothing};
void nothing(int x) { }
void next(int x)
{
printf("%i\n", x);
dispatch[x/1000](x+1);
}
int main()
{
next(1);
return 0;
}How so?
edit: clarification, in true language-lawyer fashion.
I'm not being a "language lawyer".
Regardless, I'll repeat it; maybe it'll stick this time:
i < 0
is a conditional expression, if (i < 0) {}
is a conditional statement.Also, while the above could be (mis)taken for C code;
cmp eax, 0
jl foo
this one surely wouldn't.And at this point I'll stop beating this particular horse.
Nothing personal, but your argument is always used in these situations, for no good reason. Please learn where to draw the line.
I'm sorry if i tire you, but i think you misinterpreted what i said. Every solution will boil down to a loop, necessarily, so my point is it's useless to make distinctions like , "this solution is a bit cheating, but this one isn't". As long as it respects the requirements, eg no loop or conditional statements, it is no cheating.
> Nothing personal, but your argument is always used in these situations
I realize my original comment was oversimplifying things, but i fail to see which situations you are talking about. My argument, or what i originally wanted it to be, is that the line you draw between map and the constructor trick is extremely subjective. In such case, the only way to distinguish valid or invalid solutions is to refer to the original requirements.
> Please learn where to draw the line.
And please keep your condescending attitude to yourself. I perfectly know where to draw the line, and i still consider the limit tptacek made to be ridiculous. If you have any real argument to oppose me instead of sheer oversimplification and exaggeration, please proceed, but from what i see you just needed to vent, and in a rather unpleasant manner.
Other than that, I don't assume a patronizing attitude when I say that arbitrarily labeling solutions to such problems as "cheating" is an infuriatingly useless argument over semantics, and nothing more. Particularly so when such questions are used in the context of a job interview; how would you feel if an interviewer wrongly rejected your answer based on what he read on SO, and whose answer she trusted more?
PS. The situations I was referring to is the myriad of such questions on SO; a problem with absurd constraints imposed. They're fun, until 5 people come along and decide going around labeling answers as "cheating". The Stack Overflow meta site has quite a few comments on that phenomenon.
My apologies.
#include <stdio.h>
#include <stdlib.h>
void main(int j) {
printf("%d\n", j);
(main + (exit - main)*(j/1000))(++j);
}
Somehow the solutions that crash on exit are cheating too much. The above doesn't (obviously) compile to something which uses conditionals at the assembly level. This solution probably wouldn't be by the book. That takes a few more lines of code.They are all in there, you just have to look a little.
puts("1000200300400500600700800901101201301401501601701801902102202302402502602702802903103203303403503603703803904104204304404504604704804905105205305405505605705805906106206306406506606706806907107207307407507607707807908108208308408508608708808909109209309409509609709809911121131141151161171181191221231241251261271281291321331341351361371381391421431441451461471481491521531541551561571581591621631641651661671681691721731741751761771781791821831841851861871881891921931941951961971981992223224225226227228229233234235236237238239243244245246247248249253254255256257258259263264265266267268269273274275276277278279283284285286287288289293294295296297298299333433533633733833934434534634734834935435535635735835936436536636736836937437537637737837938438538638738838939439539639739839944454464474484494554564574584594654664674684694754764774784794854864874884894954964974984995556557558559566567568569576577578579586587588589596597598599666766866967767867968768868969769869977787797887897987998889899900");#include <stdio.h>
int show(int i) { printf("%d\n",i); return( (i>=1000) || show(i+1)); }
int main(int argc,char argv) { return show(1); }
Yay Haskell.
(Original question is for C/C++, so obviously this is a bogus answer.)
You're just re-using a loop already implemented in the runtime. That's quite the same thing as using map, though a tiny bit clever. But if you qualify map as cheat, well then the constructor trick is just a cleverer cheat.
p [*1..1000]
Or being anal about the formatting: puts [*1..1000].join ' '