def f(a):
print a
print a+1
{999: int}.get(a,f)(a+2)
f(1)
Note, I had to count by twos, because when I counted by ones, I got a "maximum recursion limit exceeded" error.Basically, the concept is to call f recursively, until you get to 999 (and had printed 999 and 1000), at which time you call some non-recursive function (I call "int").
At 999, the inner-called f function terminates, leading to the unwinding of the stack (each previous f function terminating), and then the program ends.
This statement is the hard one to understand:
{999: int}.get(a,f)(a+2)
Basically, it's saying "Look up 'a' in this hard-coded dictionary that only has one value in it - for 999. If 'a' isn't found (as it won't be most of the time), set the look-up value to the function called 'f'. Otherwise, set the look-up value to the function called 'int'. In either case, call that looked-up value, passing the parameter of a+2." f() -> f(1).
f(1001) -> ok;
f(N) -> io:fwrite("~p~n", [N]), f(N + 1). MAX = 1000
def execute_if_not_equals(value, other, &block)
noop = lambda {}
[block, noop][value / other].call
end
def print(i)
execute_if_not_equals(i, MAX) do
puts(i + 1)
print(i + 1)
end
end
print(0)
[edit]: updated to not use != operator which is probably a cheat. var items = [];
var rec = function(item) {
var func = [];
func[true] = function(item) { items.push(item); [item-1].map(rec); };
func[false] = function(item) {};
return func[Boolean(item)](item);
};
var item = [1000];
item.map(rec);
console.log(items.reverse().join(','));
Won't do in browsers due to recurssion limit but change [1000] to [10] and you'll see it works. print str(range(1,1001)).strip('[]').replace(', ','\n')
Edit: added missing [ as pointed out below print "\n".join(map(str, range(1,1001)))
or print "\n".join(str(i) for i in range(1,1001))Or else you could just:
for i in range(1,1000):
print i
Plus relying on the string representation of a list isn't silly it as it doesn't change. I just didn't know if range would meet the criteria or not. z="0123456789"
print map(lambda x:int("".join(x))+1,zip(sorted(zip(*z)[0]*100),
sorted(zip(*z)[0]*10)*10,
zip(*z)[0]*100))
Only downside is that it prints as a list (so it has the format of [1,2,3..]) ('\n').join(map(str, [1,2,3])) from __future__ import print_function
z="0123456789"
map(print,map(lambda x: int("".join(x))+1,zip(sorted(z*100),
sorted(z*10)*10, z*100)))
The way it works is to create 3 strings, one for each digit-place. The first digit, when counting from 000 to 999, is 100 zeros, followed by 100 ones, followed by 100 twos, etc. That is represented by sorted(z*100)
The middle digit is 10 zeros, 10 ones, etc... and repeat this 10 times. So this is sorted(z*10)*10.
The least significant digit is represented by a string that just counts and starts over. it's 1000 characters long: "0123456789012345..." and represented by z*100.
I define an unnamed function (using lambda), that does the following: Join the three digits, make it an int, and add 1. So the result of this is a list of numbers from 1 to 1000. In other words, map(lambda x: int("".join(x))+1,zip(sorted(z*100),
sorted(z*10)*10, z*100)))
is about the same as if I just used range(1,1001)Then I map it to the new print function that is available in Python 3.0, or with the import statement.
A simpler version would use range, but that seems a little too simple:
from __future__ import print_function
map(print,range(1,1000))
Now that I think about it, this last one is the right answer! $,="\n"; print 1..1000 mapM_ (putStrLn . show) [1..1000]mapM_ print [1..1000]
but yeah, ...haskell +1 for that :D
(map (lambda (n)
(displayln n))
(cdr (build-list 1001 values))) #define NOT_A_LOOP(x) while(x)
Enumerator is just Ruby object goo over a loop.#include someone_else_did_it_so_we_dont_know(x)
Just as those other answers in C relied on GOTOs somewhere.
Which, of course, is passing the buck to Range#to_a
No stinking loops!