• hoochiekiss (unregistered) in reply to hoodaticus
    hoodaticus:
    cockSmoker10:
    More recently what with all the raping stories in the news I have increased my precautions by reverse-facing the spikes in my RapeEx two times before inserting it into my rectum so it is double protected.
    +5
    I concur as well.
  • Nebler (unregistered) in reply to Jeff
    Jeff:
    What about improvements to the recursive string reversal code?

    I'm thinking:

    1. First choice, use any built in library functions since they're already coded, tested, and optimized.

    2. Don't use recursion since that's a lot of overhead and you could get a possible stack overflow if the string was long. (Can you even get stack overflows in C#? I honestly haven't had one in many many years).

    3. As a coding exercise, off the top of my head I'd probably use a one character buffer and swap characters from the outside to the inside. Ala:

    strToRev="CodeTest";

    for(int i=0; i< strToRev.length() / 2; i++) { int farCharPosition = strToRev.length()-i-1; char buf = strToRev[farCharPosition]; strToRev[farCharPosition] = strToRev[i]; strToRev[i] = buf; }

    Thoughts?

    1. Is the only correct option. Always use unicode-aware methods, like string.reverse. All your surrogate pairs will die otherwise. The code in the interview gets this wrong as well.
  • (cs) in reply to dohpaz42
    dohpaz42:
    Hanoi 4 ever:
    dohpaz42:
    WC:
    No, it's the John Galt from Atlas Shrugged.

    I read that code snippet and thought, "Is that really a recursive function to reverse a string?" ... Yup, apparently it is. Wow.

    Reversing a string is a simple analogy for describing recursive functions. It's an easy concept to understand, and just as simple to implement, and it demonstrates recursion in an easy to understand way. Usually this sort of exercise is done in a beginner programming class (at least, it was for me back when I took beginning programming in college).

    Reversing a string is a poor example of recursion, as it's so obviously easier to do with a loop. Use something like quicksort or a tree-traversal.

    Recursion is usually a precursor to larger algorithms, such as sorting. If you can't understand recursion, you have no hope of quick sort or tree traversal.

    recursion (n) - see: recursion

  • B1 Bomber in the Lavatory (unregistered) in reply to CaptainCaveman
    CaptainCaveman:
    "And the only reason that release went out was they had fired the QA group for repeatedly rejecting Charles' and Terry's high quality work."

    Hooray, lets fire people for doing their jobs well. I have seen this shit myself in the not-so-distant past. People who don't work in IT wonder why I became so jaded.

    This is really not so surprising when you consider that the boss obviously valued his own personal perceptions far higher than any other inputs.

  • Rumen's Boss (unregistered) in reply to Machtyn
    Machtyn:
    dohpaz42:
    resmoker10:
    Actually that's something that bugs me about C# that it doesn't have a string reverse function. For some reason Microsoft didn't see fit to add a string.Reverse() method and I have had to roll my own.

    Password reversal is a useful when storing passwords in a database to obscure them from potential attackers. More recently what with all the hacking stories in the news I have increased my precautions by reversing the strings two times before inserting them into the password table so they are double encrypted.

    I hope that you don't mean that you do String.encrypt(String.reverse(String.reverse)), and I'm just reading your post wrong!

    Actually, I think he means String.reverse(String.reverse). He didn't mention anything about an encrypt in "Password reversal is a useful when storing passwords..."

    String.reverse(String.reverse("I then had a good laugh because they are now double encrypted")). (They'll never guess what I said now!)

    I think me meant String.EncryptROT26(String.Reverse(String.Reverse(s)));

  • ih8u (unregistered) in reply to Rumen's Boss
    Rumen's Boss:
    Machtyn:
    dohpaz42:
    resmoker10:
    Actually that's something that bugs me about C# that it doesn't have a string reverse function. For some reason Microsoft didn't see fit to add a string.Reverse() method and I have had to roll my own.

    Password reversal is a useful when storing passwords in a database to obscure them from potential attackers. More recently what with all the hacking stories in the news I have increased my precautions by reversing the strings two times before inserting them into the password table so they are double encrypted.

    I hope that you don't mean that you do String.encrypt(String.reverse(String.reverse)), and I'm just reading your post wrong!

    Actually, I think he means String.reverse(String.reverse). He didn't mention anything about an encrypt in "Password reversal is a useful when storing passwords..."

    String.reverse(String.reverse("I then had a good laugh because they are now double encrypted")). (They'll never guess what I said now!)

    I think me meant String.EncryptROT26(String.Reverse(String.Reverse(s)));

    I do 26 ROT1's for the win! More method calls me more secure!

  • Fat (unregistered) in reply to hoodaticus

    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.

  • SG_01 (unregistered) in reply to ih8u
    String EncryptRot(String s, int amount)
    {
        for ( int i = 0; i < s.Length; ++i )
        {
            s[i] = 1 + ( ( s[i] - 1 + amount ) % 255 );
        }
        return s;
    }
    

    I do 255 custom ROT1's for the win! ;)

  • SG_01 (unregistered) in reply to Fat
    Fat:
    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.

    It's actually NOT an infinite loop. Back to school ^^

  • (cs) in reply to Fat
    Fat:
    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.
    Actually, it is recursion.

    imagine this:

    string GetRecursionDef() 
    {
      return GetRecursionDef(); 
    }
  • (cs) in reply to Fat
    Fat:
    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.
    Actually, a proper example of an infinite loop would be
    do
    { Fat.study(recursion); }
    while (!Fat.understands(recursion));
  • anonymouser (unregistered) in reply to Hortical
    Hortical:
    Sutherlands:
    Psuedocode:

    foreach(letter in string) StringBuilder.AppendFront(letter)

    Consider the possibility that the StringBuilder uses a char[] internally.

    Consider the possibility that every time you insert a char at the beginning, all the other chars that have been added have to be moved over a space. Every time.

    I would bet it's more like <char>List, or something similar.

  • Hortical (unregistered) in reply to anonymouser
    anonymouser:
    Hortical:
    Sutherlands:
    Psuedocode:

    foreach(letter in string) StringBuilder.AppendFront(letter)

    Consider the possibility that the StringBuilder uses a char[] internally.

    Consider the possibility that every time you insert a char at the beginning, all the other chars that have been added have to be moved over a space. Every time.

    I would bet it's more like <char>List, or something similar.

    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

  • (cs) in reply to boog
    boog:
    Fat:
    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.
    Actually, a proper example of an infinite loop would be
    do
    { Fat.study(recursion); }
    while (!Fat.understands(recursion));
    That is fucking EPIC! That's going down in my quote book, and perhaps a T-Shirt...
  • (cs) in reply to resmoker10
    resmoker10:
    Actually that's something that bugs me about C# that it doesn't have a string reverse function. For some reason Microsoft didn't see fit to add a string.Reverse() method and I have had to roll my own.

    Password reversal is a useful when storing passwords in a database to obscure them from potential attackers. More recently what with all the hacking stories in the news I have increased my precautions by reversing the strings two times before inserting them into the password table so they are double encrypted.

    String reversal has been deprecated as an encryption technique for some time now. It's highly recommended that you switch to ROT13 for enhanced security.

    [Yeah, lots of other people have already made that joke. Sue me.]

    Hanoi 4 ever:
    dohpaz42:
    Reversing a string is a simple analogy for describing recursive functions. It's an easy concept to understand, and just as simple to implement, and it demonstrates recursion in an easy to understand way. Usually this sort of exercise is done in a beginner programming class (at least, it was for me back when I took beginning programming in college).

    Reversing a string is a poor example of recursion, as it's so obviously easier to do with a loop. Use something like quicksort or a tree-traversal.

    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.)

  • Fat (unregistered) in reply to C-Octothorpe
    C-Octothorpe:
    Fat:
    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.
    Actually, it is recursion.

    imagine this:

    string GetRecursionDef() 
    {
      return GetRecursionDef(); 
    }

    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.

  • Hortical (unregistered) in reply to Fat
    Fat:
    C-Octothorpe:
    Fat:
    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.
    Actually, it is recursion.

    imagine this:

    string GetRecursionDef() 
    {
      return GetRecursionDef(); 
    }

    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.

    [image]
  • Fat (unregistered) in reply to EvanED
    EvanED:
    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).

    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).

  • Bryan the K (unregistered) in reply to hoodaticus
    hoodaticus:
    John Galt:
    Won't that Java snippet run forever in an infinite loop if you supply it with a string that's more than 1 character long? It looks like all it's doing is moving the first character to the end of the submitted string over and over again.
    Is this the John Galt from Stack Overflow?

    Close, it's Howard Rorack.

  • (cs) in reply to Fat
    Fat:
    C-Octothorpe:
    Actually, it is recursion.

    imagine this:

    string GetRecursionDef() 
    {
      return GetRecursionDef(); 
    }

    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.

    Well, at least you're consistent... Stupid, but consistent. That counts for something, right?

  • Fat (unregistered) in reply to Hortical
    Hortical:
    Fat:
    C-Octothorpe:
    Fat:
    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.
    Actually, it is recursion.

    imagine this:

    string GetRecursionDef() 
    {
      return GetRecursionDef(); 
    }

    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.

    [image]

    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

    bool a=true;
    bool b;
    if (a==true)
      b=true;
    else if (a==false)
      b=false;
    else
      b=(!true)&&(!false);
    

    ?

  • Mahomedalid (unregistered)

    Re: Wrong Answer

    Actually sounds like the guy that create NoSQL.

  • Fat (unregistered) in reply to C-Octothorpe
    C-Octothorpe:
    Fat:
    C-Octothorpe:
    Actually, it is recursion.

    imagine this:

    string GetRecursionDef() 
    {
      return GetRecursionDef(); 
    }

    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.

    Well, at least you're consistent... Stupid, but consistent. That counts for something, right?

    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.

  • Hortical (unregistered) in reply to Fat
    Fat:
    Why do you call me stupid? No one has told me that yet.

    Well, he did you benefit of no keeping you in the dark anymore.

    Fat:
    Perhaps, I don't understand the real meaning of this joke,

    Oh, is that what it is?

    Fat:
    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.

    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".

  • (cs) in reply to Fat
    Fat:
    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

    bool a=true;
    bool b;
    if (a==true)
      b=true;
    else if (a==false)
      b=false;
    else
      b=(!true)&&(!false);
    

    ?

    You're not an opponent, you're just wrong. And I don't think anybody called you a troll.

    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.

  • PRMan (unregistered) in reply to Sutherlands
    Sutherlands:
    Anon:
    Other than this being a reverse function, since I am not a java developer what *would* be the proper way to reverse a string since the interview question explicitly asks how you would improve on the function.
    Psuedocode:

    foreach(letter in string) StringBuilder.AppendFront(letter)

    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?)

  • PRMan (unregistered) in reply to Jeff
    Jeff:
    (Can you even get stack overflows in C#? I honestly haven't had one in many many years).

    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.

  • Fat (unregistered) in reply to Hortical
    Hortical:
    Fat:
    Why do you call me stupid? No one has told me that yet.

    Well, he did you benefit of no keeping you in the dark anymore.

    Fat:
    Perhaps, I don't understand the real meaning of this joke,

    Oh, is that what it is?

    Fat:
    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.

    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".

    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.

  • fu2uf (unregistered) in reply to resmoker10

    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.

  • (cs) in reply to Fat
    Fat:
    I guess, that's an elitist in me talking.
    Yeah, that must be it, or the more likely explanation was that you started a dick measuring contest while missing a, um, prerequisite.
  • Hortical (unregistered) in reply to Fat
    Fat:
    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.

    Cool. You do this all by yourself.

  • B1 Bomber in the Lavatory (unregistered) in reply to Fat
    Fat:
    Hortical:
    Fat:
    Why do you call me stupid? No one has told me that yet.

    Well, he did you benefit of no keeping you in the dark anymore.

    Fat:
    Perhaps, I don't understand the real meaning of this joke,

    Oh, is that what it is?

    Fat:
    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.

    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".

    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.

    I think it all started when you erroneously declared that this is infinitely recursive. Why don't you take a look at the function again?

    Hint: java.lang.String.substring(int) returns the substring starting at the zero-based int index.

  • Sock Puppet #5 (unregistered) in reply to B1 Bomber in the Lavatory
    B1 Bomber in the Lavatory:
    Fat:
    Hortical:
    Fat:
    Why do you call me stupid? No one has told me that yet.

    Well, he did you benefit of no keeping you in the dark anymore.

    Fat:
    Perhaps, I don't understand the real meaning of this joke,

    Oh, is that what it is?

    Fat:
    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.

    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".

    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.

    I think it all started when you erroneously declared that this is infinitely recursive. Why don't you take a look at the function again?

    Hint: java.lang.String.substring(int) returns the substring starting at the zero-based int index.

    To be fair, he did start by saying he had never studied algorithms.

  • hoodaticus (unregistered) in reply to Sock Puppet #5
    Sock Puppet #5:
    B1 Bomber in the Lavatory:
    Fat:
    Hortical:
    Fat:
    Why do you call me stupid? No one has told me that yet.

    Well, he did you benefit of no keeping you in the dark anymore.

    Fat:
    Perhaps, I don't understand the real meaning of this joke,

    Oh, is that what it is?

    Fat:
    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.

    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".

    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.

    I think it all started when you erroneously declared that this is infinitely recursive. Why don't you take a look at the function again?

    Hint: java.lang.String.substring(int) returns the substring starting at the zero-based int index.

    To be fair, he did start by saying he had never studied algorithms.
    I wish people would stop quoting this comment thread, it's getting quite long.

  • Fat (unregistered) in reply to B1 Bomber in the Lavatory

    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.

  • you know who (unregistered) in reply to hoodaticus
    hoodaticus:
    Sock Puppet #5:
    B1 Bomber in the Lavatory:
    Fat:
    Hortical:
    Fat:
    Why do you call me stupid? No one has told me that yet.

    Well, he did you benefit of no keeping you in the dark anymore.

    Fat:
    Perhaps, I don't understand the real meaning of this joke,

    Oh, is that what it is?

    Fat:
    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.

    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".

    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.

    I think it all started when you erroneously declared that this is infinitely recursive. Why don't you take a look at the function again?

    Hint: java.lang.String.substring(int) returns the substring starting at the zero-based int index.

    To be fair, he did start by saying he had never studied algorithms.
    I wish people would stop stroking my commendable tool, it's getting quite long.

  • you know who (unregistered) in reply to Fat
    Fat:
    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.
    [image]
  • Obvious Troll is Obvious (unregistered) in reply to Rootbeer

    I heard sucking Bawls can be very soothing.

  • Matt (unregistered)

    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.

  • (cs) in reply to C-Octothorpe
    C-Octothorpe:
    boog:
    Fat:
    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.
    Actually, a proper example of an infinite loop would be
    do
    { Fat.study(recursion); }
    while (!Fat.understands(recursion));
    That is fucking EPIC! That's going down in my quote book, and perhaps a T-Shirt...
    In retrospect, I could have pointed out that if I advised him to look up recursion in the dictionary, the joke and ensuing comment thread would just repeat itself indefinitely.

    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.

  • Fat (unregistered)

    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.

  • (cs) in reply to Matt
    Matt:
    I'm still in Uni (actually year off to work on some OSS), so go easy and what not.

    ... There's no need for the else as it will just continue regardless or "exit" because of the return. I count down

    This is all fine (though personal style dictates i >= 0 instead of i > -1 for decreasing loops, there's nothing wrong with the latter).

    to avoid a string prepend and thus moving every char in the string "over".
    Now here is where there's a bit of a problem. Java strings (I'd like to say "like most common languages", though I'm not positive that would hold*) are immutable, which means that it's copying the string each time anyway. So you're not "moving every char over", but you are copying the whole string, and in the process turning what should be O(n) into O(n^2). You should really use a StringBuffer or a char array to avoid this. Pretty much "be very wary of string concatenations in loops" is a widely-applicable rule.
    • Off the top of my head, we have Java, C# and .Net in general, and Python as immutable, and C++ as mutable. I don't count C as having strings for purposes of this discussion. :-)
    Fat:
    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.
    I have an idea which I will try soon.
  • (cs)

    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.

  • (cs) in reply to Fat
    Fat:
    Hortical:
    Fat:
    Why do you call me stupid? No one has told me that yet.

    Well, he did you benefit of no keeping you in the dark anymore.

    Fat:
    Perhaps, I don't understand the real meaning of this joke,

    Oh, is that what it is?

    Fat:
    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.

    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".

    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.

    Then you are TRWTF for believing that recursion is an elite topic. How the fuck do you do Retry without recursion?

  • (cs) in reply to boog
    boog:
    C-Octothorpe:
    boog:
    Fat:
    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.
    Actually, a proper example of an infinite loop would be
    do
    { Fat.study(recursion); }
    while (!Fat.understands(recursion));
    That is fucking EPIC! That's going down in my quote book, and perhaps a T-Shirt...
    In retrospect, I could have pointed out that if I advised him to look up recursion in the dictionary, the joke and ensuing comment thread would just repeat itself indefinitely.

    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.

    ... and all the world's internet backbones would start fuming ozone from the unbounded bandwidth consumption, killing millions as satellites fall out of orbit... basically all the worst parts of Christian eschatology.

  • Tim (unregistered)

    Until you said Fall (as opposed to Autumn), I thought I might have featured in that article...

  • (cs) in reply to EvanED
    EvanED:
    Fat:
    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.
    I have an idea which I will try soon.
    After fighting with SML and O'Caml a bit, I gave up and used Python.
    >>> def fibb2(n):
    ...     if n==0:
    ...             return (0,1)
    ...     if n==1:
    ...             return (1,1)
    ...     (a,b) = fibb2(n-1)
    ...     return (b, a+b)
    ... 
    >>> def fibb(n):
    ...     return fibb2(n)[1]
    ... 
    >>> [(n,fibb(n)) for n in xrange(0,9)]
    [(0, 1), (1, 1), (2, 2), (3, 3), (4, 5), (5, 8), (6, 13), (7, 21), (8, 34)]

    I want to make it tail recursive, but I don't see how to get that into this version.

  • Johnny (unregistered) in reply to John Galt
    John Galt:
    Won't that Java snippet run forever in an infinite loop if you supply it with a string that's more than 1 character long? It looks like all it's doing is moving the first character to the end of the submitted string over and over again.
    This is the second time in the last few days that I've realised people don't understand what different parameters in SubString are....or are you a troll?
  • painful swelling of the salivary glands (unregistered) in reply to dohpaz42
    dohpaz42:
    Reversing a string is a simple analogy for describing recursive functions. It's an easy concept to understand, and just as simple to implement, and it demonstrates recursion in an easy to understand way. Usually this sort of exercise is done in a beginner programming class (at least, it was for me back when I took beginning programming in college).

    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)

    REVERSE
    ;Take in a string and reverse it using the built in function $REVERSE
    NEW S
    READ:30 "Enter a string: ",S
    WRITE !,$REVERSE(S)
    QUIT

  • Annihilatorg (unregistered) in reply to C-Octothorpe
    C-Octothorpe:
    Fat:
    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

    bool a=true;
    bool b;
    if (a==true)
      b=true;
    else if (a==false)
      b=false;
    else
      b=(!true)&&(!false);
    

    ?

    You're not an opponent, you're just wrong. And I don't think anybody called you a troll.

    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.

    And for feck's sake... this IS tdwtf...

    bool a=true;
    bool b;
    if (a==true)
      b=true;
    else if (a==false)
      b=false;
    else
      b=FILE NOT FOUND;
    

Leave a comment on “Three for Three, Recursion Threads, and Wrong Answer”

Log In or post as a guest

Replying to comment #:

« Return to Article