4 ms·
The worrying thing is that you're suggesting there may be people on hackernews who can't bang this out in a few seconds :/ Does it even really require much thi
by axod 16y ago
The worrying thing is that you're suggesting there may be people on hackernews who can't bang this out in a few seconds :/
Does it even really require much thinking? I'm not trying to be arrogant here, but it seems like a good definition of "trivial".
- jacquesm 16y ago> The worrying thing is that you're suggesting there may be people on hackernews who can't bang this out in a few seconds :/ My sentiment exactly. And by the way, you do have a bug in that code.
- axod 16y agoReally? give me a clue...?
- tsally 16y agoThe result of the program should always be a valid string. ;) Tried to be as ambiguous as I could, not really sure of another way to say it.
- axod 16y ago"valid string" ? :/ can you give me an example of an invalid string? It correctly tacks a 0 byte on the end if that's what you mean, of course cleaning up the unused space would be outside of the task at hand. Maybe I'll look like a complete idiot soon if it transpires I've completely missed the 'gotcha' ;)
- RiderOfGiraffes 16y agoI'd be really interested to see your original version and your latest version side-by-side, because I'm wondering if your "clean-up" actually changed its behavior.
- tsally 16y agoActually, I might just be tired. :-p I don't see anything wrong with axod's code. Just emailed you a fairly verbose C version. I hate writing production code...
- axod 16y agoOriginal code: // esi -> asciiz string // cl = character to remove mov edi, esi cld loop1: lodsb // Load the byte from [esi] into al or al, al jz alldone cmp al, cl je loop1 // Skip this byte, since it's one we want to remove stosb // Store the byte at [edi] from al jmp loop1 // Loop around for the next byte alldone: xor al, al stosb // 0 terminate it The only real behavioral change was that this version would cope with an input of cl=0. My cleaned up version would not cope with cl=0, but would need a sanity check for that.
- RiderOfGiraffes 16y agoHmm. My memory must be faulty, but I don't remember the first one having the final xor and stosb. I do remember looking at your solution and thinking that you'd forgotten it, that's why I expressed an interest in seeing the original. Still, likely my memory is at fault.
- 10ren 16y agoheheh my initial version also explicitly added the 0, and then I also found a way to do it in the loop. Seems a common evolution. But your final solution is clearer than mine. While it uses an extra variable (register), it avoids looking up the same thing twice. So... more efficient and clearer. BTW: TIL char * a = "hello"; doesn't work in C (I think it doesn't allocate memory). I'm actually happy enough that I could solve this at all, not having used C for 20 years ...old
- jacquesm 16y agoYou are making some scary assumptions about the validity of that pointer...
- axod 16y agoI didn't include all the boring error checking code, I included just the 'meat' ;) If you call it with valid inputs, it produces valid outputs. IMHO That's not a 'bug'. That's a lack of boring gruntwork error checking.
- jacquesm 16y agoYes, that's more or less the problem with lots of code. I'm not saying your code is invalid or that it won't work, but 'defensive' programming says: - assume your input is total garbage - handle all correct situations correctly - handle all garbage gracefully or throw an error depending on what the circumstances dictate I'm also missing the stackframe handling, but as you said, this is the 'meat', that meat needs a bit of scaffolding to work.
- axod 16y agoOh definitely. I agree. I assumed though that the "problem" itself was the main aim here, not testing if someone can write scaffolding error checking code. How far do you go? Check that the memory isn't mapped as read only? ;) Check that there's actually some memory mapped into that address space? Obviously you need both skills, but they're pretty related I'd say. If you can solve low level problems you can probably think through any potential issues, invalid inputs, edge cases etc and churn out code to deal with them. I guess my issue is I don't see what being able to solve this says about someone, apart from "isn't completely terrible at programming".
- NickPollard 16y agoDefensive programming is not always the correct assumption though.
- 16y ago
- j_baker 16y agoIt's not that it requires much thinking. It's just that it's not every day that most of us have to code something like this in C.
- JoachimSchipper 16y agoFrom another I made on this page: > If you don't find any egregious bugs, I hereby claim it really is trivial. Still, it may be a nice exercise.
- deleted 16y ago[deleted]