5 ms·
How would you do the same in other languages? It would be interesting to see the same question answered in Python, Perl, Java, etc. etc. Just as a fun thought
by ajays 16y ago
How would you do the same in other languages? It would be interesting to see the same question answered in Python, Perl, Java, etc. etc. Just as a fun thought exercise, and not to get into language flamewars.
- 3pt14159 16y ago1.upto 1000 {|n| puts n} # ruby ftw
- ajays 16y agoIsn't "upto" a loop? The idea, IIRC, is to print the numbers without loops and conditionals.
- 3pt14159 16y ago1.upto(10).class => Enumerator
- jerf 16y agoIt "can be" implemented recursively. If you're going to be pedantic about this (and who doesn't love being pedantic?) you need to be very careful about your definitions, because regardless of what you write you're going to have jump instructions in the finally-executed machine code. If you say that you can't have a loop "anywhere", you're limited to recursion, and then what will you do when someone comes along and points out that recursion and looping are ultimately equivalent? This is one of those cases where examining the boundary too closely makes it dissolve under the scrutiny, and it's better just to smile and nod and play along (and have fun arguing).
- jluxenberg 16y agoputs ((1..1000).to_a.join("\n")) Which, of course, is passing the buck to Range#to_a
- deleted 16y ago[deleted]
- silentbicycle 16y ago1+!1000 No stinking loops!
- deleted 16y ago[deleted]
- iskander 16y ago1 + til 1000 No stinking distinction between monadic/dyadic usage of array operators!
- BobKabob 16y agoHere's a solution in Python, using pretty much the same logic as the top vote-getter on Stack Overflow: 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."
- rararational 16y agoDoes this fit the criteria for python: print str(range(1,1001)).strip('[]').replace(', ','\n') Edit: added missing [ as pointed out below
- kroo 16y agono! For one thing, it still prints out an opening '[' before 1. Second of all, relying on the structure of the string representation of an array is just silly. Perhaps: print "\n".join(map(str, range(1,1001))) or print "\n".join(str(i) for i in range(1,1001))
- rararational 16y agoHeh, I forgot about join but that for is still a loop right? :) 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.
- xentronium 16y agoWill this count for solution? :) 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.
- gurraman 16y agoAn Erlang solution: f() -> f(1). f(1001) -> ok; f(N) -> io:fwrite("~p~n", [N]), f(N + 1).
- BobKabob 16y agoHow about this Python solution: 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..])
- patrickyeon 16y ago('\n').join(map(str, [1,2,3]))
- BobKabob 16y agoEven better Python variation: Prints 1 to 1000 and quits: 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!
- die_sekte 16y agoHaskell: mapM_ (putStrLn . show) [1..1000]
- kamechan 16y agoactually, can be done more simply as: mapM_ print [1..1000] but yeah, ...haskell +1 for that :D
- jsmcgd 16y agoClojure: (map println (range 1 1001))
- scrabbles 16y agoIs there a better way than this for scheme (racket)? (map (lambda (n) (displayln n)) (cdr (build-list 1001 values)))
- jodrellblank 16y agohttp://pleac.sourceforge.net/ http://pleac.sourceforge.net/ - Perl cookbook examples translated to other languages, and links to similar translation challenges.
- acqq 16y agoFor this specific task, I think the shortest Perl, as I've mentioned in http://news.ycombinator.com/item?id=2326372 http://news.ycombinator.com/item?id=2326372 is just $,="\n"; print 1..1000
- deleted 16y ago[deleted]
- Tomek_ 16y agoJavascript, validity depends on how exactly do you define conditions and loops: 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.