24 game/Solve: Difference between revisions

→‎{{header|REXX}}: included a CHANGESTR subroutine (function) as a separate entity. -- ~~~~
(→‎{{header|REXX}}: included a CHANGESTR subroutine (function) as a separate entity. -- ~~~~)
Line 2,938:
 
=={{header|REXX}}==
This REXX version includes the '''changestr''' function for the older REXXes.
<lang rexx>/*REXX program to help the user find solutions to the game of 24. */
/* ┌──────────────────────────────────────────────────────────────────┐
Line 3,018 ⟶ 3,017:
say; say sols 'unique solution's(finds) "found for" orig /*pluralize.*/
exit
/*───────────────────────────CHANGESTR subroutine───────────────────────*/
changestr: procedure; parse arg o,h,n,,r; w=length(o); if w==0 then return n||h
do forever; parse var h y (o) _ +(w) h; if _=='' then return r||y; r=r||y||n; end
/*───────────────────────────DIV subroutine─────────────────────────────*/
div: procedure; parse arg q /*tests if dividing by 0 (zero). */
Line 3,041 ⟶ 3,037:
end /*select*/
return \errCode</lang>
Some older REXXes don't have a '''changestr''' bif, so one is included here.
<lang rexx>/*╔══════════════════════════════╗ CHANGESTR ╔═════════════════════════╗
╔═╩══════════════════════════════╝ function ╚═════════════════════════╩═╗
║ new string to be used──────────┐ ┌─────limit of # changes (times)║
║ original string (haystack)────────┐ │ │ [default: ≈ one billian]║
║ old string to be changed───┐ │ │ │ ┌───begin at this occurance #.║
║ {O, H, and N can be null.} │ │ │ │ │ [default: 1st occurrance]║
╚═╦════════════════════════════╗ │ │ │ │ │ ╔═══════════════════════╦═╝
╚════════════════════════════╝ ↓ ↓ ↓ ↓ ↓ ╚═══════════════════════╝*/
changestr: procedure; parse arg o,h,n,t,r;b w=length(o); if/* w==0T thenand returnB n||h are optional.*/
$='' /*$: the returned string.*/
t=word(t 999999999 , 1) /*maybe use the default? */
b=word(b 1 , 1) /* " " " " */
w=length(o) /*length of OLD string.*/
if w==0 & t\=0 then return n || h /*changing a null char ? */
#=0 /*# of changed occurances*/
do j=1 until # >= t /*keep changing, T times.*/
parse var h y (o) _ +(w) h /*parse the string ... */
if _=='' then return $ || y /*no more left, return. */
if j<b then $=$ || y || o /*didn't meet begin at ? */
else do
$=$ || y || n /*build new STR from S. */
#=#+1 /*bump occurance number. */
end
end /*j*/
/*Most REXX BIFs only ···*/
return $ || h /* support three options.*/</lang>
'''output''' when the following input is used: <tt> 1111-1234 </tt>
<pre style="height:30ex;overflow:scroll">