Live data from Hacker News

Display 100 Hello World Without Using Loops

ajibanda.com

11–20 of 42 posts

Re: Display 100 Hello World Without Using Loops

#11
post #3

Python version: exec "print 'Hello world'\n" * 100

I think a better Python version would be print '\n'.join(['Hello world'] * 100) No need for exec.

I wonder why 430gj9j added exec..

  print "Hello world\n"*100
Would have worked just fine.

Re: Display 100 Hello World Without Using Loops

#12
post #2

Decent enough explanation of recursion. Why not take it further and produce a more general solution? In JavaScript I'd write something like this: function recurseBetween(start, end, callback) { // Create a recursive function // which checks the limits and calls the supplied callback var recursiveCallback = function(i) { // Call the original callback callback(i); // If we're at the end, stop if(i >= end) { return; } e…

Here's a completely abstracted version, but I'm certain it's less clear to use ;)

    function recurseBetween(initial, hasEnded, modify, callback) {
      // Overwrite the callback with a recursive version:
      var recursiveCallback = function(i) {
        // Call the original callback
        callback(i);
        // If we're at the end, stop
        if(hasEnded(i)) {
          return;
        } else {
          // Else increment and recurse
          recursiveCallback(modify(i));
        }
      };
      // Start recursing with the start value
      recursiveCallback(initial());
    }
Which transforms our for loop example to:

    recurseBetween(function() {
      return 1;
    }, function(i) {
      return i
Which at first glance is unwieldy, but gives complete control to the calling script, such that we could do things like introduce a step:

    recurseBetween(function() {
      return 1;
    }, function(i) {
      return i
Or even more exotic things.

In reality, this syntax is too complicated. I would probably introduce some helpers; eg you could pass in a straight integer or a function and it would deal with it. I'd also probably send the arguments as named parameters so the syntax looked like this instead:

    recurseBetween({
      'start': 1,
      'end': 100,
      'modify': function(i) {
        return i+=10;
      },
      'callback': function(i) {
        console.log(i);
      }
    });
Which is a little clearer. Obviously the example is somewhat contrived because in reality you'd just use a for loop! But an interesting exercise nonetheless.

Re: Display 100 Hello World Without Using Loops

#14
post #4
post #3

Python version: exec "print 'Hello world'\n" * 100

that reminds me why I don't like Python. Ad-hoc tools (many) instead of a few general concepts working well together. Btw at interview time this solution would not be acceptable, because you are using still a built-in language construct for looping.

Well then you don't seem to know python and focus on some red herrings, because it's much more of the latter than the former. The example leverages general concepts such as duck typing and defining __mul__ for a string as doing something thoughtful.

Re: Display 100 Hello World Without Using Loops

#16
post #4
post #3

Python version: exec "print 'Hello world'\n" * 100

that reminds me why I don't like Python. Ad-hoc tools (many) instead of a few general concepts working well together. Btw at interview time this solution would not be acceptable, because you are using still a built-in language construct for looping.

Huh? This is a specific case of the * operator, which is overloaded for type string to return a repetition of the string. The only ad-hoc in the example is the print statement, which is gone in 3.2.

How do you know the __mul__ operator overload for string is implemented with iteration?

Re: Display 100 Hello World Without Using Loops

#17
post #2

Decent enough explanation of recursion. Why not take it further and produce a more general solution? In JavaScript I'd write something like this: function recurseBetween(start, end, callback) { // Create a recursive function // which checks the limits and calls the supplied callback var recursiveCallback = function(i) { // Call the original callback callback(i); // If we're at the end, stop if(i >= end) { return; } e…

Or go back to C and learn that recursion is not really used like that. If you know the limits beforehand, use "for". If you don't know how deep the processing will go, use recursion.

If you must code a loop without using "for" it means you're doing a homework ;)

Re: Display 100 Hello World Without Using Loops

#18
post #11

Earlier quoted context omitted.

I think a better Python version would be print '\n'.join(['Hello world'] * 100) No need for exec.

I wonder why 430gj9j added exec.. print "Hello world\n"*100 Would have worked just fine.

He abstracted this loop:

    for i in xrange(100):
        statement
into:

   exec "statement" * 100
The 'no exec' alternatives propose something not as generic since they can only print something a number of times.

Re: Display 100 Hello World Without Using Loops

#19
post #17
post #2

Decent enough explanation of recursion. Why not take it further and produce a more general solution? In JavaScript I'd write something like this: function recurseBetween(start, end, callback) { // Create a recursive function // which checks the limits and calls the supplied callback var recursiveCallback = function(i) { // Call the original callback callback(i); // If we're at the end, stop if(i >= end) { return; } e…

Or go back to C and learn that recursion is not really used like that. If you know the limits beforehand, use "for". If you don't know how deep the processing will go, use recursion. If you must code a loop without using "for" it means you're doing a homework ;)

Or just playing, like I was ;)

Re: Display 100 Hello World Without Using Loops

#20
post #4
post #3

Python version: exec "print 'Hello world'\n" * 100

that reminds me why I don't like Python. Ad-hoc tools (many) instead of a few general concepts working well together. Btw at interview time this solution would not be acceptable, because you are using still a built-in language construct for looping.

You're right, Python isn't beautifully minimal. But the Scheme version is boring ;)

Rather than asking the interview candidate to use recursion for a problem that shouldn't be solved using recursion (unless in a language where recursion is the idiomatic iteration method), it would be better to ask about a problem that is best solved using recursion rather than printing "Hello world" -- perhaps something from Project Euler (http://projecteuler.net/).

Post reply on HN