Find where a number occurs in a list
Inputs:
A = A number
Output: the position of the last occurrence of A in L1


Code:
:max(seq(X(A=L1(X)),X,1,dim(L1

Inputs:
A = A number
B = Where it begins looking
C = Where it ends looking
Output: The position of the last occurrence of A in the portion of L1 starting from element B and ending with element C


Code:
:max(seq(X(A=L1(X)),X,B,C


inString( for lists. It returns 0 if the number isn't in the list, and you can optionally choose where to start and stop looking for the number.
A note on that: If you want to find the first occurrence, it's obviously just min( instead of max(.
How about ALL occurrences =D

:seq(x,x,1,dim(L1)
:(L1=A)Ans--> L2
:SortD(L2)
:(L2<>0)-->dim(L2
KermMartian wrote:
A note on that: If you want to find the first occurrence, it's obviously just min( instead of max(.

No, 'cause if any element isn't equal to A, X(A=L1(X)) is equal to 0, so the min of the set would be 0. To get the first occurrence, I'd do this:

Code:
max(seq((dim(L1)+1-X)(A=L1(X)),X,1,dim(L1
1-Ans+dim(L1

I'm not exactly sure what rthprog was doing on that last line, but this is how I'd do it.

Code:
seq(X(A=L1(X)),X,1,dim(L1->L2
SortD(L2
sum(not(L2->dim(L2
Perhaps try this instead of "seq(x,x,1,dim(L1":

Code:
cumSum(1+0L1
calc84maniac wrote:
Perhaps try this instead of "seq(x,x,1,dim(L1":

Code:
cumSum(1+0L1
That would work too.

Ed H: Ah yes, you're absolutely right. And what rthprog was doing was the same as you; <> is the =/= sign, whereas you used a not(.
Harq wrote:
Most efficient getkey

input: none
output: the key pressed in k (or you can change it)


Code:
Repeat Ans
getkey
end
Ans->K


P.S. Simple, but there really is a slight difference in performance between this and some other similar methods.


how about
:Repeat Ans
:getkey-->K
:End

?
rthprog wrote:

how about
:Repeat Ans
:getkey-->K
:End

?
That would require storage to a variable on every iteration, which is going to be significantly slower.
Ed H wrote:
KermMartian wrote:
A note on that: If you want to find the first occurrence, it's obviously just min( instead of max(.

No, 'cause if any element isn't equal to A, X(A=L1(X)) is equal to 0, so the min of the set would be 0. To get the first occurrence, I'd do this:

Code:
max(seq((dim(L1)+1-X)(A=L1(X)),X,1,dim(L1
1-Ans+dim(L1

I'm not exactly sure what rthprog was doing on that last line, but this is how I'd do it.

Code:
seq(X(A=L1(X)),X,1,dim(L1->L2
SortD(L2
sum(not(L2->dim(L2


sorry I just forgot to put sum( on the last line...
Since I've been thinking about the topic a lot lately...

Convert a list from another base into base 10

For example, if FF is hexadecimal then input would be {15,15}
{L1} is the list of elements, B is the starting base.

Code:
Prompt {L1},B
:sum({L1}seq(B^-X,X,1-dim({L1}),0

Pretty simple and obvious, but just putting it out there.
Going in reverse is to come.
mine is kinda pathetic ... but anyways
Convert Base10 to any base from 2 to 16


Code:
:Prompt A,B
:" " -->Str0
:iPart(log(A)/log(E)->C
:For(N,0,C
:B^(C-N ->E
:iPart(A/E -->D
:Str0 + sub("0123456789ABCDEF", D+1,1 -->Str0
:A-ED -->A
:End
:sub(Str0,2,length(Str0)-1-->Str0


A is the number you want to convert, B is the base to convert to.... anyone want to optimize?

Change Case of a String

Code:
:not(A)-->A
:length(Str1)->B
:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"->Str2
:For(N,1,B
:inString(sub(Str2,26A + 1, 26),sub(Str1,N,1)
:Str1 + sub(sub(Str1,N,1) +sub(sub(Str2,26,26)+sub(Str2,1,26),26A+1,26),Ans+1,1)-->Str1
:End
:sub(Str1,B+1,B)-->Str1


Depending on value of A, changes Str1 to all upper case or all lowercase...
Here's an optimized Base-10 to Base-something else routine. You can make it support more bases by adding digits to the big string in the middle.
The number is in A, the base is in B, the output is in Ans.

Code:
:"_
:For(I,1,1+ln(A)/ln(B
:Ans+sub("0123456789ABCDEF",1+int(BfPart(AB^-I)),1
:End
Ed H wrote:
Here's an optimized Base-10 to Base-something else routine. You can make it support more bases by adding digits to the big string in the middle.
The number is in A, the base is in B, the output is in Ans.

Code:
:"_
:For(I,1,1+ln(A)/ln(B
:Ans+sub("0123456789ABCDEF",1+int(BfPart(AB^-I)),1
:End
Any particular reason you're using ln instead of log? I'm a bit rusty, but perhaps if I remember correctly log base n of a over log base n of b is the same regardless of what n is given that a and b are constant.
Nope, no reason. I just like ln better. Laughing Plus, it uses fewer letters (ln has two letters, log has 3).
Ed H wrote:
Nope, no reason. I just like ln better. 0x5 Plus, it uses fewer letters (ln has two letters, log has 3).


that makes no difference in memory
Oh, I know that. I just prefer fewer letters, perhaps for readability. Like, whenever I have a choice, I always use int( over iPart(, because it's easier to read.

Anyway, here's a little routine with a clever algorithm:

Number to Roman Numeral

Takes number in N and returns the answer in Ans and Str0.

Code:
:" →Str0
:For(X,0,log(N
:10fPart(.1N→Z
:While Z
:sum({4.1,4.1,9.2}(Z={4,5,9→T
:sub("IVXLCDMvxlcdmni",2X+10fPart(T)+1,1)+Str0→Str0
:abs(Z-1-int(T→Z
:End
:int(.1N→N
:End
:Str0
Hey, very impressive! You dealt with the pre/post-fixing very nicely there.
Find Unique Elements in Two Lists
Input L1 and L2
Output L3 (All elements that are in L1 but not L2, and elements that are in L2 but not L2.
Conditions: "0" is not a unique element, and no element is repeated within an individual list



Code:
:augment(L1,L2-->L3
:L3*seq((1=sum(not(L3-L3(X)))),X,1,dim(L3))-->L3
:SortD(L3
:sum(L3!=0)-->dim(L3
I haven't seen this here yet

Get Letter Routine
Input=None
Output=Ans containing the letter pressed ('?' if it isn't a letter)
Destroys K


Code:
:Repeat K
:getKey→K
:If K
:sub("ABC??DEFGHIJKLMNOPQRSTUVWXYZθ?? :?",5(iPart(K/10)-4)+10fPart(K/10),1
:End


Could probably be optimized.
_player1537 wrote:
I haven't seen this here yet

Get Letter Routine
Input=None
Output=Ans containing the letter pressed ('?' if it isn't a letter)
Destroys K


*EDITED*

there we go


Code:
:Repeat Ans
:GetKey
:End
:sub("ABC  DEFGHIJKLMNOPQRSTUVWXYZθ   :?",Ans-5iPart(.1Ans+4),1
  
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
» Goto page Previous  1, 2, 3, 4, 5 ... 13, 14, 15  Next
» View previous topic :: View next topic  
Page 4 of 15
» 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