Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

ENTER A SUBJECT DUM-DUM

Name: Anonymous 2007-08-24 4:27 ID:cvcO9wR4

Reverse a string. Optimize for speed. Optimize for space.

Reversing a string is simple. But now imagine you have to read a 512kb string with only 64kb of ram. Is there any way to do this efficiently?

Name: Anonymous 2007-08-24 4:29 ID:cvcO9wR4

nvm. heres a better way to put it:

can you do this without reading the whole string into memory?

Name: Anonymous 2007-08-24 4:48 ID:Heaven

>>2
can you do this without reading the whole string into memory?
Yes. Did you copy this from some old reddit discussion?

Name: Anonymous 2007-08-24 4:55 ID:cXjzk3hW

Yeah, load the first and last 32KB into memory. Reverse that string. Write them back. The repeat with the the next 32KB chunks towards the centre until done.

Name: Anonymous 2007-08-24 4:56 ID:Heaven

maybe something like this?
fseek(input_stream,0,SEEK_END);
for(int i=ftell(input_stream);i+1;fseek(input_stream,--i,SEEK_SET))
 fputc(fgetc(input_stream),output_stream);

Name: Anonymous 2007-08-24 10:45 ID:g5GfqWql

If you're on some primitive machine with only 64kb of RAM, I think most of the trickiness (in terms of speed, and also in terms of getting things to work if you truly only had 64k and were talking directly to the disk) would revolve around the string not being an even multiple of your disk's sector size.

Name: Anonymous 2007-08-24 12:54 ID:DwV2sylu

>>6
Nice shot, I lol'd

Don't change these.
Name: Email:
Entire Thread Thread List