(Recursive) nested loops and IFs?

12172015, 07:17 AM
(This post was last modified: 12172015 07:24 AM by chromos.)
Post: #1




(Recursive) nested loops and IFs?
Hi,
I'm writting solver for incomplete coordinates for multi caches (is there any geocacher out there?) which solves (brute force) inequalities, but their quantity and number of variables (and their names) is obtained from user via INPUT. For six inequalities and four variables I have this static code: (L1...coords via user input, something like {"5","0","3","5","(A+1)","(B)","(BA4)","0","1","5","2","4","(C14)","(D8)","(CD+3)"} L2...List of variable names, L3, L4...Lists of variable boundaries, L5...List of expressions L6...valid variable values Digitsum... digit sum of whole coordinates) PHP Code: SUBRFROMTO() I tried to build it recursively, but this is my first encounter with recursion so I can't find syntax which works. I tried it with another approach where I packed everything as text and then I execute it via EXPR(...) and it works. Dissadvantage is unreadable code and everything must be in one string otherwise FOR and END will collapse. Can anybody show me how build nested loops and IFs if I don't know their quantity beforehand? Some screenshots: Prime G2, 15C CE 

12172015, 07:28 AM
(This post was last modified: 12172015 07:30 AM by chromos.)
Post: #2




RE: (Recursive) nested loops and IFs?
Geo Coords Solver.hpprgm (Size: 20 KB / Downloads: 4) Because there is five attachments limit, I give program for interested people into this post. (Programm counts distance and bearing correctly for North East hemisphere and can solve for six inequalities and four variables only.) Prime G2, 15C CE 

12172015, 11:50 AM
Post: #3




RE: (Recursive) nested loops and IFs?
You can replace the nested IFs with a single IF which will work for any size of L5.
Replace: Code:
By: Code:
Thanks for publishing your program, do you have some data samples that could be used to test it? 

12172015, 12:15 PM
(This post was last modified: 12172015 12:26 PM by chromos.)
Post: #4




RE: (Recursive) nested loops and IFs?
Thank you Didier, I'll try your code.
I am testing my program on this cache: Bysterske vyhlidky This is unfortunately in czech language so importatnt data are: Coords to solve: N 50°35.(A+1) (B) (BA4) E 015°24.(C14) (D8) (CD+3) Enter it as showed (without N and E letters and without ° character and without parentheses). Digit sum for solved coords is 49. Starting published coordinates are: N 50° 36.404 E 015° 24.120 Enter it as showed (without N and E letters and without ° character). I don't know if you are geocacher, so expressions in parentheses aren't multiplied with each other. Instead of, every pair of parentheses is one digit to solve. Meanwhile I updated my program (for example there wasn't handled case when no coords were found. Geo Coords Solver.hpprgm (Size: 21.11 KB / Downloads: 3) Prime G2, 15C CE 

12172015, 04:17 PM
(This post was last modified: 12172015 04:45 PM by Didier Lachieze.)
Post: #5




RE: (Recursive) nested loops and IFs?
Here is a way to replace the nested loops by a single one using a list (LoopIdx) to store the different loop index values. LoopIdx is replacing Loop1, Loop2, Loop3 & Loop4 and allows for a dynamic number of variables as it is sized according to the size of your L3.
This is based on the latest version of your program: PHP Code: SUBRFROMTO() It is working with the test case you provided. I'm not a geocacher but I know some people who are and may be interested by your program. Here are the detailed modifications I've made to your program:


12172015, 08:20 PM
(This post was last modified: 12172015 08:25 PM by chromos.)
Post: #6




RE: (Recursive) nested loops and IFs?
Thank you, Didier, you helped me very much.
PHP Code: IF ΠLIST(EXECON("EXPR(&1)>=0 AND EXPR(&1)<=9",L5)) THEN 2) Nested loops: I didn't try your code yet, but I will. There is ΠLIST again so I should study its usability much more.  Meanwhile I updated my program again and I replaced nested loops with strings executed via EXPR. Fortunately, code isn't unreadable (I moved lines within FORDO and END; into subroutine which helped). Before: PHP Code: FOR Loop1 FROM L3(1) TO L4(1) DO After (I declared Loop1, ... , Loop20 for sufficient headroom): PHP Code: //nested loops preparation I found interesting feature (or is it bug?): list variable cannot be used as FORDO counter (which is a pity). Example: PHP Code: LoopList:=MAKELIST(0,X,1,15);  It seems the program works now with any number of inequalities and with number of variables up to 20 which is more than enough. I will test it on more caches before uploading. Thank you again very much, Didier. Prime G2, 15C CE 

12182015, 08:06 AM
(This post was last modified: 12182015 09:35 AM by Didier Lachieze.)
Post: #7




RE: (Recursive) nested loops and IFs?
(12172015 08:20 PM)chromos Wrote: In this case you have to use ΠLIST: ΠLIST(list) returns the product of the list elements which is the equivalent of a Boolean AND while ∑LIST(list) returns the sum of the list elements which is the equivalent of a Boolean OR. Nested IF conditions can be replaced by ANDs: PHP Code: IF (EXPR(L5(1))>=0 AND EXPR(L5(1))<=9) THEN is the same as: PHP Code: IF (EXPR(L5(1))>=0 AND EXPR(L5(1))<=9) AND (EXPR(L5(2))>=0 AND EXPR(L5(2))<=9) AND (EXPR(L5(3))>=0 AND EXPR(L5(3))<=9) THEN EXECON("EXPR(&1)>=0 AND EXPR(&1)<=9",L5) returns a list with the result of the test for each element of L5, either 0 (false) or 1 (true). Then ΠLIST returns the product (AND) of all these results: 0 (false) if at least one is false and 1 (true) if all are true. As we work with lists this is not tied to the number of variables. (12172015 08:20 PM)chromos Wrote: 2) Nested loops: I didn't try your code yet, but I will. There is ΠLIST again so I should study its usability much more. Here L4L3 returns a list of the differences between the elements of L4 and L3, so ΠLIST(L4L3) returns the product of these differences which gives the total number of loops to do. 

12182015, 10:25 PM
(This post was last modified: 12182015 10:58 PM by chromos.)
Post: #8




RE: (Recursive) nested loops and IFs?
This...
(12182015 08:06 AM)Didier Lachieze Wrote: ... ΠLIST(list) returns the product of the list elements which is the equivalent of a Boolean AND...... and this... Quote:... As we work with lists this is not tied to the number of variables...... is explanation I needed. Thank you.  I updated Geo Coords Solver again, most visible thing is better workflow. On this link is czech geocache you can use e.g. google translator, so you can try to solve it. :) (Hint  closest coordinates are usually the right ones). Geo Coords Solver.hpprgm (Size: 25 KB / Downloads: 5) Prime G2, 15C CE 

12182015, 10:40 PM
Post: #9




RE: (Recursive) nested loops and IFs?
One more thing :
PHP Code: IF ΠLIST(EXECON("EXPR(&1)>=0 AND EXPR(&1)<=9",L5)) THEN can be replaced by : PHP Code: IF ΠLIST(EXPR(L5+">=0") AND EXPR(L5+"<=9")) THEN 

12182015, 11:32 PM
Post: #10




RE: (Recursive) nested loops and IFs?
I had unhandled A/B type expressions, so I modified your previous code into...
PHP Code: IF ΠLIST(EXECON("EXPR(&1)>=0 AND EXPR(&1)<=9 AND FP(EXPR(&1))==0",L5)) THEN In your new code... PHP Code: IF ΠLIST(EXPR(L5+">=0") AND EXPR(L5+"<=9") AND EXPR(FP(L5)+"==0")) THEN Prime G2, 15C CE 

12192015, 12:00 AM
Post: #11




RE: (Recursive) nested loops and IFs?
(12182015 11:32 PM)chromos Wrote: In your new code... L5 is a list of strings, not a list of reals so FP(L5) doesn't work. This should work but it starts to look a bit ugly: PHP Code: IF ΠLIST(EXPR(L5+">=0") AND EXPR(L5+"<=9") AND EXPR("FP("+L5+")==0")) THEN 

12192015, 12:05 AM
Post: #12




RE: (Recursive) nested loops and IFs?
You're right. I'm too tired from it probably. :)
Prime G2, 15C CE 

01172017, 11:46 AM
(This post was last modified: 01172017 11:48 AM by chromos.)
Post: #13




RE: (Recursive) nested loops and IFs?
Does anybody know why this stopped working in latest Prime firmware?
(in L5 are stringed expressions, something like "(A+B)"). PHP Code: IF ΠLIST(EXPR(L5+">=0") AND EXPR(L5+"<=9") AND EXPR("FP("+L5+")==0")) THEN I replaced it with: PHP Code: IF ΠLIST(EXECON("EXPR(&1)>=0 AND EXPR(&1)<=9 AND FP(EXPR(&1))==0",L5)) THEN Prime G2, 15C CE 

« Next Oldest  Next Newest »

User(s) browsing this thread: 1 Guest(s)