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.
print "Hello world\n"*100
Would have worked just fine.11–20 of 42 posts
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…
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.No loop, and no conditional recursion.
The flaw is to use arithmetic on stack function pointer, which is not recommended nor strictly defined.
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.
I'm just sayin'.
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.
How do you know the __mul__ operator overload for string is implemented with iteration?
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…
If you must code a loop without using "for" it means you're doing a homework ;)
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.
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.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 ;)
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.
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/).