- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
Admin
Admin
Admin
I think me meant String.EncryptROT26(String.Reverse(String.Reverse(s)));
Admin
I do 26 ROT1's for the win! More method calls me more secure!
Admin
I don't really like this joke. It'n not a legitiamte use of recursion, it's just an infinite loop. Of course, it doesn't defeat its real purpose, make beginner programmers feel superior to anyone who didn't spend 2 hours studying algorithms.
Admin
I do 255 custom ROT1's for the win! ;)
Admin
It's actually NOT an infinite loop. Back to school ^^
Admin
imagine this:
Admin
Admin
I would bet it's more like <char>List, or something similar.
Admin
Ok, now consider the possibility that I've seen the source code and it does use a char[].
http://www.docjar.com/html/api/java/lang/AbstractStringBuilder.java.html
Admin
Admin
[Yeah, lots of other people have already made that joke. Sue me.]
I feel that it's better to start with a case where there is only one recursive call. While you can adapt both of your examples to this (e.g. a search in a BST or finding the kth order statistic), both require more explanation than you need.I'm not sure I would choose string reversal though; IMO factorial is about ideal for a first example. It's something that is actually fairly reasonable to do recursively anyway, lends itself to an easier description of how you achieve that result, has the same complexity as a loop solution (unlike that reverse function; and in fact it may generate the same code with an optimizing compiler in C or C++), etc.
I'd probably do Fibbonicci as a first example of having more than one recursive call, with the disclaimer that it's actually a really bad way of implementing it (without memoizing). Only after that would I get into stuff like trees and quicksort.
(This is especially true because I'm not sure you can talk much about trees before you talk about recursion.)
Admin
Of course, technically it could be called recursion. It's just the worst possible example of it. This joke to me feels kind of like a guy, who says "lol, I'm cool, check out my mad driving skillz", and than drives with parking brake on, on reverse, into the wall. Technically, he was driving. But honestly, he wasn't.
Admin
Admin
I've had a thought recently, that this might be a pretty good question: writing recursive function which calculates Fibonacci number without memoizing/loops/arrays, and works O(n).
Admin
Close, it's Howard Rorack.
Admin
Admin
Yeah, you can justify any level of stupidity these days simply by calling an opponent a troll.
So, you do think that example has more sense than, say
?
Admin
Re: Wrong Answer
Actually sounds like the guy that create NoSQL.
Admin
Why do you call me stupid? No one has told me that yet. Perhaps, I don't understand the real meaning of this joke, and it is not "look, I defined recursion as recursion, so now it's recursive, hahaha, I've heard what recursion is", but "look what a dumb-ass definition I wrote, no one in his right mind would take this as a remotely valid illustration of recursion". It's just that I've seen people thinking of a first meaning as true far too often.
Admin
Well, he did you benefit of no keeping you in the dark anymore.
Oh, is that what it is?
More like "look, I defined recursion as recursion, so now it's recursive, hahaha, everyone (hopefully) else here understands what recursion is, so the irony and the cleverness with which it was demonstrated".
Admin
Regarding your example: it's perfectly cromulent for displaying the use of an if statement... What, did you want an example with real business domain models, and perhaps a dash of DI, maybe even a recursion provider?
You're wrong and you're loud... The worst combination.
Admin
What?!? Don't you realize how many memory copies you are going to do with that?
Do a reverse for loop and build it up the normal way.
(As if it matters on today's processors...)
CAPTCHA - erat (Isn't he a Right Wing for the Nashville Predators?)
Admin
Yes, I got one a couple weeks ago. I was parsing an XSD to make a TreeView out of it and I didn't notice that a certain named element was in there below itself, confusing my parser into an endless loop. It took a long time (about 2-3 minutes) before it happened (other people distracted me right as I was running it). It had to fill all 8GB on my machine first before I got a stack overflow.
Admin
OK, thanks, I get what you all mean now. I guess I'm too sensitive about these kind of jokes (e.g. stupider kinds of jokes about Shrodinger's cat), which make people believe they understand something by simply remembering a couple of remotely relevant witty remarks. I guess, that's an elitist in me talking.
Admin
I don't think this will work, because I can crack that by placing the doublelbuod encrypted password table between two opposed mirrors. This works for an infinite number of test cases or until it vanishes.
Admin
Admin
Cool. You do this all by yourself.
Admin
Hint: java.lang.String.substring(int) returns the substring starting at the zero-based int index.
Admin
Admin
Admin
No, what I meant was a joke about recursion (by hoodaticus). The code in article is finite, as far as my knowledge of Java allows me to judge.
Admin
Admin
Admin
I heard sucking Bawls can be very soothing.
Admin
I'm still in Uni (actually year off to work on some OSS), so go easy and what not.
Aside from the fact that the string reversal in a recursion is really daft idea and using the built in library is a good one, I'd do it with a simple as for loop. I'm fuzzy on Java syntax and what it allows...
///////////////////////////////////// String funcX (String s) { String res = "";
if (s == null || s.length() < 2) return s;
for (i = s.length()-1; i > -1; i--) { res += s.charAt(i); } return res; } ///////////////////////////////////// There's no need for the else as it will just continue regardless or "exit" because of the return. I count down to avoid a string prepend and thus moving every char in the string "over". I assume for loops can count down, else replace with a while and be done with it. If this is really bad, please do say.
Admin
Of course, since that joke has no base case either, it too would be a poor example of recursion by his standards, so he'd have made the same comment about it, and the joke and ensuing comment thread would just repeat itself indefinitely.
Either way, our supply of stack space would soon be spent.
Admin
By the way, no one has yet answered my question about recursive fibonacci numbers calculation. I've got one solution, but it looks kind of sloppy (at least when the implementation is in C++; I guess, on Haskell or Matlab it'd look much better). I want to see if you come up with something more elegant. And besides, I think this problem is pretty neat.
Admin
Admin
As mentioned, factorial is often used as an example of something that can easily be understood recursively. But implementing factorial in a loop is often easier (And sometimes shorter). Depending, of course, on whether you allow non-integers (the gamma function) as arguments.
Directory traversal, on the other hand, seems to be easier to implement recursively. Depending on exactly what you're doing, there might be more "state" to keep track of, which recursion can do more neatly.
Admin
Admin
Admin
Until you said Fall (as opposed to Autumn), I thought I might have featured in that article...
Admin
I want to make it tail recursive, but I don't see how to get that into this version.
Admin
Admin
answers to question 1: (a) code reverses a string (b) function could be improved by writing it in a language that has a built-in string-reversal capability, like MUMPS (c)
Admin