24 game/Solve: Difference between revisions

m
→‎{{header|REXX}}: added comments, add DO-END labels, removed blank lines. -- ~~~~
(Lua)
m (→‎{{header|REXX}}: added comments, add DO-END labels, removed blank lines. -- ~~~~)
Line 2,937:
=={{header|REXX}}==
<lang rexx>/*REXX program to help the user find solutions to the game of 24. */
 
/*------------------------------------------------------------------+
| Argument is either of two forms: ssss ==or== ssss-ffff |
Line 2,947 ⟶ 2,946:
| FFFF is the start. |
+------------------------------------------------------------------*/
 
parse arg orig /*get the guess from the argument. */
parse var orig start '-' finish /*get the start & finish (maybe).*/
Line 2,953 ⟶ 2,951:
finish=space(finish,0) /*remove any blanks from the FINISH. */
finish=word(finish start,1) /*if no FINISH specified, use START.*/
 
call validate start
call validate finish
 
opers='+-*/' /*define the legal arithmetic operators*/
ops=length(opers) /* ... and the count of them (length). */
do j=1 for ops do j=1 for ops /*define a version for fast execution. */
 
o.j=substr(opers,j,1)
do j=1 for ops /*define a version for fast execution. */
end
o.j=substr(opers,j,1)
end
 
finds=0 /*number of found solutions (so far). */
x.=0 /*a method to hold unique expressions. */
Line 3,022 ⟶ 3,016:
say sols 'unique solution's(finds) "found for" orig /*pluralize.*/
exit
 
 
/*---------------------------DIV subroutine-------------------------*/
div: procedure; parse arg q /*tests if dividing by 0 (zero). */
if q=0 then q=1e9 /*if dividing by zero, change divisor. */
return q /*changing Q invalidates the expression*/
 
 
/*---------------------------GER subroutine-------------------------*/
ger: say; say '*** error! ***'; if _\=='' then say 'guess=' _
Line 3,035 ⟶ 3,025:
say
exit 13
 
 
/*---------------------------validate subroutine--------------------*/
validate: parse arg _
Line 3,046 ⟶ 3,034:
_=verify(_,digs)
return
 
 
/*---------------------------S subroutine---------------------------*/
s:if arg(1)=1 then return ''; return 's' /*simple pluralizer.*/</lang>
'''output''' when the following input is used: <tt> 1111-1234 </tt>
 
 
/*---------------------------CHANGESTR subroutine-------------------*/
changestr: procedure; parse arg old,hay,new
r=''; w=length(old); if w==0 then return new||hay
do forever
parse var hay y (old) _ +(w) hay; if _=='' then return r||y
r=r||y||new
end
/*Note: some older REXX interpretors don't have the */
/* CHANGESTR function, so it's included here. */</lang>
 
The following is the output when
 
<pre> 1111-1234</pre>
 
is entered as an argument.
<pre style="height:30ex;overflow:scroll">
a solution: [1+1+1]*8