Quote (Aimed_Shot @ Oct 1 2014 08:50pm)
why wouldn't it perform as well? (assuming he saves the result of the reversal in a text file and isn't reversing again)
my high school physics teacher taught us a little trick that i've always remembered. we were learning newtonian mechanics (forces, velocity, etc) and when a block is at an angle, students were constantly confused whether they should use sine or cosine. when the angle was 40 degrees, sine and cosine were similar enough that you can't tell which to use based on the number you get from the formula. so his advice:
use your model, but stretch the data to the extreme. sin 40 and cos 40 are close enough that can't figure out which is correct by looking at it, but if you stretched that angle to 0.01 degrees and 89.99 degrees, then sin and cos are very different, and it becomes clear which one to use.
in this context, instead of a text file that's 3 words long, consider a text file that's 20 gigs. OP's method is to first reverse the string, then find the first occurrence. how will he reverse the string? either 1) he'll keep the whole thing in memory (good luck), or 2) he'll keep part of it in memory and write to another file when he's low in memory. then repeat until the whole thing is in the new file. now he's doing a find on the new file.
option 1) clearly eats up tremendous memory, and 2) writes a huge file to disk. neither are preferred.
consider the alternative. read the file in chunks, keeping a pointer to the last occurrence. then release your memory and read more. repeat until you reach the end. this won't eat up as much memory, and it won't write a huge file to disk.
does that make sense?
Quote
(assuming he saves the result of the reversal in a text file and isn't reversing again)
maybe you were confused with your assumption. nobody said it's the same file. input is the character and a file, output is the last occurrence.
if his assignment was literally to write down the last occurrence (eg: write down "position 15"), then sure you can just find the first occurrence in the reversed file. but his assignment is to write the program which will do it for any given file.
This post was edited by carteblanche on Oct 1 2014 08:13pm