- 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
I think I win the incomprehensibility award for today since nobody has yet pointed out that my matching function was totally wrong, since the full original string was being passed in.
The value of n from the outer function needed to be passed in as an offset, as shown below:
Also, the test in the outer function to stop execution and return the list should have been a strict greater than. This is easy to see from the case where the length of str and substr are the same.
Admin
I don't think that's correct. Unless I'm mistaken, you're only ever testing if the beginning of your string matches the substring. Shouldn't the default case of your (cond) block be:
Of course, then you'd need to revise the terminating condition of your algorithm.
One thing I've always hated about LISP-style languages - they force you to use recursion when iteration would yield a much more elegant solution. Yes, recursion provides a nice abstraction at times, but leaving out iteration because recursion can replace it is like leaving out for-loops because while-loop can replace them, or leaving out swtich statements because if-elseif-elseif... chains can replace them.
Admin
Regardless of language or platform, only an idiot would write the code "If Date.ToString < OtherDate" and then wonder what the compiler means when the error message clearly says "Hey stupid, you are trying to compare a date and a string, try again with 2 matching items.... like maybe a date and a date or a string and a string..."
Admin
How the heck did the formatting on my post get screwed up so badly? Well, the entire message is there at least (partly inside the "quote" tag), so I won't repost it.
Admin
Addendum (2008-05-05 12:52): And by pre-increment, I meant add 3 or 5. LOL!
Admin
The code below fits the acceptance criteria, in O(n) time, but it fails where two possible substrings are next to each other (ie. PPolly)
using System; using System.Collections.Generic;
public class MyClass { public static void Main() { string text = "Polly put the kettle on, polly put the kettle on, polly put the kettle on we'll all have tea";
}
Admin
I think this is O(n), but then I didn't major in Comp Sci.
Counters?? Bah! :^)
-- MHill
Admin
That's good that somebody else did notice where I went wrong. However, you can't use cdr directly on strings in Scheme; they're a type distinct from lists (unlike, for example, Elisp), though string->list is an available option.
While you may be correct about Lisp-likes not providing iteration in general, Scheme is an exception as it does have several iteration constructs. The most general purpose and least transparent in how it looks is "do": http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-7.html#%_idx_138
From there you have for-each, which can be considered an iterative semantic (same with map, filter, and fold), but to use it you have to start with a list. You can also define your own if you're so inclined.
Admin
yeah, i post teh c0de tkx
Admin
Not that I have any experience with this, but I would simply leave the questions unasked, then provide flags for the program (along with a man page) allowing you to account for any of those cases.
Admin
[Epic] Fail. Doesn't work for all capital letters (think: foreign languages), returns "`" for "@".
It does remind me, though, the wonderful days of using an IBM machine with EBCDIC as a base charset, precisely because such a simple problem could not be solved in a similar way there. And EBCDIC is the True WTF, without a question.
Admin
Or in ruby... for(1..101) do |i| if((i/3)*3==i) print "Fiz" if((i/5)*5==i) print "Buz" puts end
I'm pretty sure there's a for loop sort of way to only go over 3 and 5 to make it even more clever, but I don't remember it.
Admin
Yazeran
Plan: To go to Mars one day with a hammer.
Admin
TRWTF is that you guys actually have time to waste writing this :-/
Admin
Everyone here who's posted a solution that involves something that takes a substring and an == during the iteration part
e.g.
int indexOf(string needle, string haystack){ for(int i=0, i<haystack.Length-needle.Length;i++){ if(haystack.Substring(i,needle.Length) == needle) return i; } return -1; }
I would not hire, because this has exponential time AND memory cost. On the other hand, a better solution (which loses the exponential time cost):
int indexOf(string needle, string haystack){ for(int i=0;i<haystack.Length-needle.Length;i++){ bool found = true; for(int j=0;j<needle.Length;j++){ if(haystack[i+j] != needle[j]){ found = true; break; } } if(found) return i; } return -1; }
would be OK.
An instant hire would be either an implementation or mention that there is a better method - the Boyer Moore algorithm - although I would not expect the candidate to memorize it.
Admin
Fixbuz in C#:
class Program { const string Fiz = "Fizz"; const string Buz = "Buzz"; const string Fizbuz = "FizzBuzz"; const int start = 0, end = 400; static void Main(){ for(int i=start;i<end;i++){ string[] s = new string[end-start]; Parallel.For(start, end, i => s[i] = ((i%3==0 && i%5==0)? Fizbuz : ((i%3==0)? Fiz : ((i%5==0)? Buz : i.ToString()))); Console.Write(String.Join(", ", s)); }
Admin
A solution in Java.
Admin
The ones who answer the questions on the help boards already have jobs.
Admin
And they spend their time on message boards.
Admin
http://dev.mysql.com/doc/refman/4.1/en/mysql-tips.html#safe-updates
Admin
FizBuz is much simpler than you are making it. 15, 30,45, 60, 75, 90 are FizBuz. No others. So just type every third number into Notepad, type every 5th number into notepad, type in those special cases above, merge the notepad files, and you are done. Programming? Who needs it! Not every "programming" task is best done by a program.
Admin
This would fail when the substring repeats itself. "rerere" would contain only one instance of "rere" according to your code.
Admin
Most of you that are nitpicking about optimizations by caching this and avoiding calls to that are forgetting that modern compilers make these optimizations for you. Let's take a snippet in the most modern and advanced language on the planet, VB .NET 2008, for an example:
OH $DEITY IN THE NAME OF ALL THAT IS HOLY! I'm failing to cache the length of the text, so I'm invoking the horrible penalty of an extra call per loop... or am I? Let's look at the IL:
The compiler caches it for me; notice how the loop starts at IL_000b, but get_Length() is called at IL_0002? The compiler recognizes this value is constant (in a single-threaded context) and simply pushes it on the stack. "Optimizing" the code by caching the text length actually adds an extra IL instruction and consumes more mempry; the code first gets the value, stores it in a local variable, then has to retrieve it so it can be pushed on the stack for the loop:
Write code that's easy for humans to read, and let computers make it easy for computers to read.
The real WTF is why the code tag inserts extra line spacing.
Admin
I guess when you're bored waiting for some code to compile...
Admin
Marketing? In many places that qualifies him as Management.
captcha: vulputate
Admin
I wonder if the "drop tables" post is the reason why he is looking for a new job?
Admin
I guess his boss did find out :-)
Would it be cheating if somebody happened to know the algorithm but didn't remember the name and had to spend a little bit of time working out some minor details of the algorithm, which they didn't remember off the top of their head?Admin
I'd be tempted to leave if presented with any work at all!
Work? Me!? Are you completely mad??
Admin
Ooh, ooh, I've got one!
It's even efficient and still correct when presented with an empty "xs" :)
Admin
please point me to the compiler where one can code up naive string matching and the compiler will "optimize" to boyer-moore!
Admin
Admin
My answer would be something like this:
Admin
Let me be the first to say: NEERRRRRRRDDDDD!
Admin
Quite right sir. That was the "you get what I mean" version. Here is the "actually tested and working" version:
Admin
Sorry but if the string is "cracker wanted by Polly" and the test string is "Polly" then your solution wouldn't find it.
"i<haystack.Length-needle.Length"
So your outer loop would end prematurely and would not detect the existence of the test string if it was at the end of the primary string.
Of course I've got a massive headache right now so I could be talking totally out of my butt.
Admin
[quote user="Sitten Spynne"]... Let's take a snippet in the most modern and advanced language on the planet, VB .NET 2008 .../quote]
Oh Sweet Baby Jesus! Now you've gone and done it!
Admin
Here's a solution in PHP. I could probably eliminate the
but it's Monday and my mind is still asleep. Also, no clue why my code is being double spaced.Admin
Most interviewers will look at the FizBuz question with 4 possible outcomes:
Admin
FIZBUZ??? I've never heard of that, but wouldn't this little chunk of code do just that? I spent a whole ten seconds thinking (waiting on notepad really) before I typed this out:
Granted, I didn't compile it, but I assume it works... I could even get it down to a few less lines, like moving the counter declaration to the first part of the for statement, but really. That's just overkill. Yeah, I did it in C++. Want it done in a different language?
You mean people can't do this?
-- So before I posted this, I read through the comments the rest of the way down, and found that there is an additional piece to the original request: Print all other numbers normally. Does that mean each on their own line? That would be too trivial. Perhaps a space between each? Okay, also trivial, so let's see ...
And for those looking at "that other problem", why are you printing "No output" or "No results found" or whatever, when the original problem said "IF THERE ARE NO MATCHES, NO OUTPUT IS GENERATED" (sorry, wanted to emphasize)???
Admin
Cheater!
The world is not just ASCII (and ToLower is not either). The "proper" manual solution would be parsing UnicodeData.txt from the Unicode Consortium :)
or use char.ToLower(c)
mihi
Admin
Admin
FizzBuzz in Java: static void FizzBuzz() { For(int x=1;x<=100;x++) { if(x%3==0) { System.out.println("Fizz"); if(x%5==0) { System.out.print("Buzz"); } } else if(x%5==0) { System.out.println("Buzz"); } else { System.out.println(x);
} } }
There's probably an easier or more efficient way to do this, but I wanted to put "Fizz", "Buzz", "FizzBuzz", or the number on its own line.
Admin
For needle length N and haystack size H, and assuming an array-copying substring implementation, this runs in O(HN) time with O(HN) memory churn and O(N) instantaneous memory load. Assuming a (much more likely) array-sharing substring implementation, churn drops to O(H) and load drops to O(1). None of these measures are exponential.
Churn and load have dropped to O(1) here, but it still runs in O(HN) time. If you're going to criticize someone's algorithm, please learn the meaning of the word "exponential" first.As a style note, you could eliminate the 'found' variable with a labeled continue.
Admin
For fun, in python!
If you want to get really trivial (anal) about not using string functions, you could iterate through the input string and test cases and convert them to lowercase then.
Admin
Admin
Admin
Terrible question, and it seems like you realize why it's a terrible answer. The right answer is: "Depends on the metric d on the space -- once that is known, evaluate d(x,y)"
Admin
Note that if a number is divisble by both 3 and 5, it is divisible by 15...
Admin
No, no, no! The solution for Fizbuz is:
Admin
I believe the point, such as it is, that we're all trying to make is that There is no Rule Six. Anybody who considers %15 to be a separate input case, even in a theoretical sense, fails. This is the great thing about understanding state systems.
So, good try on nit-picking, but: well, just no.
As for akatherder's[ fourth point, I'm looking forward to some obfuscated Scheme. It's all right: Haskell or Ocaml would do just as well...