Determine if a string is squeezable: Difference between revisions

m
(Added Delphi example)
Line 1,238:
</pre>
 
=={{header|J}}==
Invoking j with the standard startup scripts includes definitions to delete excess, leading, and trailing blanks. <tt>deb</tt> corresponds to task squeeze, additionally removing leading and trailing blanks. The definitions are
<lang j>
dlb
}.~ (=&' ' (i.) 0:)
dtb
#~ ([: +./\. ' '&~:)
deb
#~ ((+.) (1: |. (> </\)))@(' '&~:)
</lang>
Onward with the task. <tt>squeeze</tt> makes a logical vector of inequality and uses Boolean algebra against itself shifted by one position to mask the text.
<pre>
squeeze=: (] #~ 1 (0}) [: (+. _1&|.) ~:) ::(''"_)
 
main=: dyad define
squoze=. x squeeze y
msg=. ,: 'squeeze ''' , x , ''' reduces the length from ',(":#y),' to ',(":#squoze)
msg=. msg , '<<<' , y , '>>>'
msg=. msg , '<<<' , squoze , '>>>'
)
 
to_be_squished=: noun define
 
"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln
..1111111111111111111111111111111111111111111111111111111111111117777888
I never give 'em hell, I just tell the truth, and they think it's hell.
--- Harry S Truman
--- Harry S Truman
--- Harry S Truman
)
</pre>
<lang J>
'x' squeeze 'xabxxcxdxxx' NB. example
xabxcxdx
 
 
,.' -7. -r' main&.> <;._2 to_be_squished
┌──────────────────────────────────────────────────────────────────────────────┐
│squeeze ' ' reduces the length from 0 to 0 │
│<<<>>> │
│<<<>>> │
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze '-' reduces the length from 71 to 69 │
│<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln>>> │
│<<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln>>> │
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze '7' reduces the length from 72 to 69 │
│<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>│
│<<<..1111111111111111111111111111111111111111111111111111111111111117888>>> │
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze '.' reduces the length from 71 to 71 │
│<<<I never give 'em hell, I just tell the truth, and they think it's hell.>>> │
│<<<I never give 'em hell, I just tell the truth, and they think it's hell.>>> │
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze ' ' reduces the length from 70 to 19 │
│<<< --- Harry S Truman>>> │
│<<< --- Harry S Truman>>> │
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze '-' reduces the length from 70 to 68 │
│<<< --- Harry S Truman>>> │
│<<< - Harry S Truman>>> │
├──────────────────────────────────────────────────────────────────────────────┤
│squeeze 'r' reduces the length from 70 to 69 │
│<<< --- Harry S Truman>>> │
│<<< --- Hary S Truman>>> │
└──────────────────────────────────────────────────────────────────────────────┘
</lang>
=={{header|Java}}==
<lang Java>
Anonymous user