Recently, while I was making a program I needed to save a lot of text, so I searched for ways to compress it. After finding Nik's post I made a program that compressess text using his method, but it runs a bit slow. If anybody could help me optimize the code, that would be great.

Compression:

Code:
ClrHome
DelVar FClrList L1
Input "STRING: ",Str1
length(Str1
If Ans/2≠int(Ans/2:Then
"A"+Str1->Str1:1->F:End
1+int((length(Str1)-1)/8->dim(L1
For(A,length(Str1),1,~2
DelVar BDelVar C
For(D,0,1
inString("ABCDEFGHIJKLMNOPQRSTUVWXYZ-' ",sub(Str1,A-D,1->B
C+Ans(not(D->C
End
1+int((3A/2-1)/12->D
Ans≠dim(L1
3(length(Str1)-A)/2-3Ans(length(Str1)-8(dim(L1)-1))/2-24(dim(L1)-Ans-D)/2->E
L1(D)+10^E(100(3int(B/10)+int(C/10))+100fPart(B/10)+10fPart(C/10->L1(D
End
If F:L1(1)-10^(E+1)->L1(1

Decompression:

Code:
ClrHome
" ->Str1
For(E,1,dim(L1
For(A,12,3,~3
int(1000*fPart(L1(E)/(10^A->B
10int(int(B/100)/3)+10fPart(int(B/10)/10->C
round(30fPart(int(B/100)/3)+10fPart(B/10->B
B+.1(B=C->B
For(D,C,B,B-C
If int(D
Str1+sub("ABCDEFGHIJKLMNOPQRSTUVWXYZ-' ",int(D),1->Str1
End:End:End
sub(Str1,2,length(Str1)-1->Str1
Disp Str1

Any tips/optimizations/constructive criticism are welcome.
Feel free to ask for explanations.
Sorry I can't be much help: there are others on the forum far abler than I, and I haven't had time to think through your procedures properly. But I did notice the line:

Code:
If Ans/2≠int(Ans/2


which basically means "If Ans is an odd number".

This could be simplified to

Code:
If remainder(Ans,2


which has the same effect.
remainder( is unavailable on many calculators, so I'd recommend against it for maximum portability. fPart(Ans/2 should also do the trick though.
OldMathTeacher wrote:
and I haven't had time to think through your procedures properly.

Oops,should have added explainations. Sorry about that.


Code:
ClrHome
DelVar FClrList L1
Input "STRING: ",Str1
If fPart(length(Str1/2:Then      ;dealing with an uneven number of letters
"A"+Str1->Str1:1->F:End         ;adding an "A" so it's even
1+int((length(Str1)-1)/8->dim(L1   ;giving L1 the right size
For(A,length(Str1),1,~2
DelVar BDelVar C
For(D,0,1
inString("ABCDEFGHIJKLMNOPQRSTUVWXYZ-' ",sub(Str1,A-D,1->B   ;letter->number
C+Ans(not(D->C      ;store the first letter to C, second one to B
End
1+int((3A/2-1)/12->D      ;calculate the right list element
Ans≠dim(L1
3(length(Str1)-A)/2-3Ans(length(Str1)-8(dim(L1)-1))/2-24(dim(L1)-Ans-D)/2->E   ;getting the right power so the numbers don't overlap
L1(D)+10^E(100(3int(B/10)+int(C/10))+100fPart(B/10)+10fPart(C/10->L1(D   ;turn the first digit of both numbers to a ternary base and add the others after that
End
If F:L1(1)-10^(E+1)->L1(1   ;removing "A" again if there were an uneven amount of letters



Code:
ClrHome
" ->Str1
For(E,1,dim(L1
For(A,12,3,~3
int(1000*fPart(L1(E)/(10^A->B   ;extract 3 digits a time
10int(int(B/100)/3)+10fPart(int(B/10)/10->C      ;get the first number
round(30fPart(int(B/100)/3)+10fPart(B/10->B      ;get the second number
B+.1(B=C->B      ;in case the 2 letters are the same (would like some other solution)
For(D,C,B,B-C
If int(D
Str1+sub("ABCDEFGHIJKLMNOPQRSTUVWXYZ-' ",int(D),1->Str1      ;number->letter
End:End:End
sub(Str1,2,length(Str1)-1->Str1
Disp Str1


Especially the slow decompression bothers me a bit.
If anybody could help me speed that up that would be great. Very Happy
Could you explain the algorithm a bit, please ?
In the usual text to list conversion you take the index of the letter in the alphabet, put seven of them next to eachother and put the 14 digit number in a list element right? e.g. cats becomes 03 01 20 19
Using Nik's algorithm you basically take the group the letters in pairs, take the first digit of both numbers and put them in a ternary base. After that you add the remaining digits. Using my previous example this becomes 031 719. 00 in ternairy is 0 and after adding the other digits you get 031. 21 in ternairy is 7 so this pair becomes 719.
This way you can save 8 letters with 12 digits instead of 7 letters with 14 digits.
Or maybe I've just misinterpreted Nik's post.
Oh I understand, thanks Wink
I might have an idea (another way of compressing that I'm thinking of at the moment. If it is worth, I'll post it Wink )
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement