- 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
You could have gone on to explain the origin of the term: "Pipe derives from the expression of approval used by the character Artie (aka 'the strongest man in the world') in the cult sitcom 'The Adventures of Pete and Pete'".
Admin
Calculating fibonacci numbers in O(log(n)) is what I'd call interesting.
Admin
First, your proposed problem is interesting in a very different way -- much more of a "mathy" interesting, and not really a "programming" interesting. That's not bad by any means, but it is different and at least for me appeals to a very different part of my mind.
Second, you say "a simple loop", but what if you're writing Fibonacci in a functional language? Then that "simple loop" suddenly becomes much more difficult, and the recursive expression is very natural.
Third, so what if it's somewhat of an artificial requirement? That doesn't make it uninteresting. Most people add lots artificial requirements on a regular basis. I rock climb. That doesn't become uninteresting because the "go up the rock instead of walking up that trail" is unnecessary.
I thought it was reasonably neat.
Admin
With "Wrong Answer", I have heard of a few occasions that metadatabases have worked (Reddit, for example, gets by with some periodic performance problems). I believe that it can work when it's done to get things done, but fails when it's done as a work-avoidance technique.
Admin
[quote user="boog"][quote user="Fat"]Calculating fibonacci numbers in O(log(n)) is what I'd call interesting.[/quote]
That would indeed by interesting, but since the result has a size of O(n), it is not possible.
Admin
He doesn't even encrypt them , dummy
Admin
or something....
I'm amused by the response: Use meaningful names and comment code...
Admin
Admin
Admin
You want to choose an easier example? I think you overlooked the part where the question is supposed to be difficult, not easy.
Admin
Admin
Likewise (or perhaps conversely), although I can see there is a certain intuition that makes recursion suitable for Directory (or any tree) traversal, but I don't really understand what you mean by the comments about more state=.
Thanks for playing, Dave.
Admin
Admin
Not really a smart way to reverse a string however. Even without built in support for reversing strings, this could be done much more readably just iterating through the string in reverse.
Recursion has its place, but is a PITA to debug or easily understand from just a quick look.
I find recursion much easier to understand, when its being demonstrated in some area where it is actually useful, like file spidering etc.
Admin
You'd have no hope of WRITING your own quick sort or tree traversal.
However, you'd probably still be able to implement it into your own code. I'd guess the majority of programmers (from what i've seen in business, gives me a bit of an ego boost when I see how crap some other peoples code is) have lots of things they use but dont understand.
I miss the 80's and 90's sometimes, when you had to not only understand stuff, but to learn new stuff, you had to either find a book with the info in (or someone who knew about it), or just spend hours and hours hacking stuff from scratch. I have to admit I can get more done nowadays however by leveraging other peoples code and not reinventing the wheel..
Admin
Sounds very familiar!
My boss sent me to interview someone; I think it was because he was too chicken to tell her to her face that he had no intention of hiring her.
Admin
People who like to write in functional languages don't do it that way. (Or at least I suspect. At least I don't really think that way; that's definitely not how I came up with my answer.) They don't come up with an iterative solution and turn it into the recursive one because that's what the language supports any more than people who do all their programming in imperative languages make functions by coming up with the recursive one and then transforming it to an explicit stack. In both cases you may, once in a few blue moons, actually do that, but it's not the norm.
Yeah, but how does code that uses one of those compare to a recursive solution in those languages? I didn't say it was impossible, just that it's harder than the recursive version. And I stand by that statement. :-)Admin
Perhaps we should define what n is?
Here's an O(1) solution.
http://www.dreamincode.net/code/snippet6172.htm
It can be optimized further by realizing that termTwo is always less than .5 so you can just do (int)(termOne + 0.5);
Admin
Admin
What I meant by "then that 'simple loop' suddenly becomes much more difficult, and the recursive expression is very natural" can probably be more precisely stated as follows:
Is that clearer? Does it sound like that was the point of "disagreement"?
Admin
Yup, the horns, the facial tentacles, the claws, all of it. And not a noodly appendage in sight.
Admin
You are reading it wrong, he's doing String.reverse(String.reverse). Doing it your way would be insecure.
Admin
That would be horribly inefficient. I bet I could train a finite-context automaton to do that far more quickly than your tree-walk could do it.
Admin
Real programmers would use an XOR-swap.
Admin
Thanks EvanED.
I don't like using >= I like keeping the symbol as one. Don't ask why, as I don't even know.
For some reason I remember strings simply being arrays, but if they re immutable in Java, then it certainly doesn't hold. Definitely more of a C++ guy despite two years of java that felt like a piss take at times. It's good to know these things though I hopefully never need to apply them.
Admin
Admin
Funny, I thought that was a matrimonial style ...
Admin
I guess the easiest solution is the best and fastest one - at least on my machine.
For i = Len(s) To 1 Step -1
meh = meh & Mid(s, i, 1) 'mid == substring
Next i
funcX2 = meh
(yes vb :P )
Admin
Not in assembly code, but you do have to distinguish between strings with even or odd numbers of characters, or you run the risk of turning the character at the very center into all zeroes.
Admin
Might have been because (a) he was too busy doing other things and/or (b) might have wanted to offer you the experience of doing an interview in a context where if you perform badly the consequences are minimal.
OTOH perhaps not - I'm unfamiliar with your exact situation so I'm guessing.
Admin
Here's the version I came up with for fun, which is (IMHO) a bit cleaner (less messing around with the indexes):
And if you go so far as to define a helper method like
then the while loop boils down to a pretty neat
Admin
Actually apart from a slight problem this would actually obfuscate the text quite a bit because it doesn't return the input.
The slight problem is that ascii2ebcdic would convert many alphabetic characters into non alphabetic ones leaving rot13 behavior as undefined (as far as I know, how do you rot13 the character "1"?)
If you solve that by instead using some non-ascii encoding that just maps letters differently, eg:
a <-> f b <-> z c <-> b d <-> s etc
Then this: asciiToAlt(s).rot13().altToAscii(s).rot13()
Doesn't return the same string as input. For example pass in "a", it's converted to "f", then rot13ed to "s", converted to "d" and then rot13ed to an output of "q".
Admin
Bullshit.
Admin
it is possible, google for it and it's such an elegant solution
Admin
[quote user="gnasher729"][quote user="boog"][quote user="Fat"]Calculating fibonacci numbers in O(log(n)) is what I'd call interesting.[/quote]
That would indeed by interesting, but since the result has a size of O(n), it is not possible. [/quote]
(forgot the quote) it is possible, google for it and it's such an elegant solution
Admin
String.reverse(String.reverse(SuperSecretPassword));
Admin
In Java why not use the reverse method built into StringBuilder, rather than manually coding the reverse?
Admin
Love it too, but how about:
public bool learn(recursion) { if (!understand(recursion)) { learn(recursion); } return true; }
Admin
that just causes a stack overflow
Admin
This is my take on the string reverse problem, notice the surrogate pair handling:
Admin
[quote user="math_geek"][quote user="gnasher729"][quote user="boog"][quote user="Fat"]Calculating fibonacci numbers in O(log(n)) is what I'd call interesting.[/quote]
That would indeed by interesting, but since the result has a size of O(n), it is not possible. [/quote]
(forgot the quote) it is possible, google for it and it's such an elegant solution [/quote]
By the way, are there any log(N) solutions other than golden ratio formula (although as Maurits suggested, from some point of view it's O(1), since power computing can be done in constant time)? Preferably, using only integer arithmetics.
Admin
Here's my attempt at the O(n) Fibonacci challenge:
//overload for when you don't need the previous value private int Fib(int n) { int prev; return Fib(n, out prev); }
//overload for the actual recursion private int Fib(int n, out int prev) { if(n==0) { prev = 0; return 1; } else { int prev2; prev = Fib(n-1, out prev2); return prev + prev2; } }
Admin
My Java-Fu is rather weak... I prefer the C (or C++) method:
void reverse (const char *in, char *out) { out += strlen(in); *out = '\0'; char c; while ((c = *in++) != '\0') *--out = c; }
I reloop through the string until '\0' because, to be 100% compliant, it is impractical to do a reverse loop using size_t, as you cannot test easily against 0 (less than zero becomes 0xFFFFFFFF, and testing against 0xFFFFFFFF is slower than testing against zero)... and a forward loop against size is just as slow if not slower than doing a loop towards '\0'.
Admin
Admin
Charles and Terry are nephews of the CEO, right? That's the only possible explanation I can think of.
Hiring obviously bad people without even consulting the people involved in the hiring process, and then firing the people who can't help but reveal how bad they are, is only excusable if appeasing the CEO's family is more important than the continued profitability of the company.
Admin
Admin
Real programmer would realize that this code is wrong.
For two reasons:
Unicode is a 4 bytes character set. For efficiency, java (and most other languages too) store them in UTF-16 (or a variant thereof), which uses 2 bytes when possible and the whole 4 bytes when necessary. Quite a few asian characters are outside of the 2 bytes unicode range and need to be coded by two subsequent chars. Inverting both characters of a surrogate pair makes an invalid string that is likely to blow up in your face later on (when displaying it, for example)
Unicode supports combining characters. So 'é' can either be expressed as a single 'é' character, or as 'e' followed by the ''' combining character. Inverting the order of these two characters would place the accent on the wrong letter, on generate an invalid string.
Lesson: Use the API methods. Even if you think you know the problem domain, it may be more complicated than expected.
Admin
I know your "elegant" solution. It uses O (log (n)) additions and multiplications. But the numbers to be added are up to O (n) in size, and adding numbers of size O (n) with fixed hardware takes O (n) in time. The addition is not a primitive operation anymore. The result must take at least O (n) since that is the size of the result; doing it faster than O (n log n) seems difficult.
Admin
How about some C#? No string reversal built-in, but there IS an Array one.
Admin
I don't believe you - the length of time it takes to add up two numbers is proportional to the number of digits it has, and therefore, as we determined, O (log n).