Determine if a string is collapsible: Difference between revisions

Add Refal
(FutureBasic solution added)
(Add Refal)
 
(4 intermediate revisions by 4 users not shown)
Line 1,344:
readln;
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func$ collapse s$ .
for c$ in strchars s$
if c$ <> cc$
r$ &= c$
.
cc$ = c$
.
return r$
.
s$[] &= ""
s$[] &= "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "
s$[] &= "..1111111111111111111111111111111111111111111111111111111111111117777888"
s$[] &= "I never give 'em hell, I just tell the truth, and they think it's hell. "
s$[] &= " --- Harry S Truman "
for s$ in s$[]
print "«««" & s$ & "»»» (" & len s$ & ")"
r$ = collapse s$
print "«««" & r$ & "»»» (" & len r$ & ")"
print ""
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 1,511 ⟶ 1,535:
I never give 'em hel, I just tel the truth, and they think it's hel.
- Hary S Truman
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn CollapseString( inString as CFStringRef ) as CFStringRef
CFMutableStringRef outString = fn MutableStringWithCapacity(0)
long index
unichar prevChar = 0, currChar
for index = 0 to len(inString) - 1
currChar = fn StringCharacterAtIndex( inString, index )
if ( currChar != prevChar ) then MutableStringAppendFormat( outString, @"%C", currChar )
prevChar = currChar
next
end fn = outString
 
window 1, @"Collapse String", (0,0,600,300)
 
CFStringRef string, collapsedString
 
string = @""
collapsedString = fn CollapseString( string )
printf @"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
string = @"\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "
collapsedString = fn CollapseString( string )
printf@"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
string = @"..1111111111111111111111111111111111111111111111111111111111111117777888"
collapsedString = fn CollapseString( string )
printf@"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
string = @"I never give 'em hell, I just tell the truth, and they think it's hell. "
collapsedString = fn CollapseString( string )
printf@"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
string = @" --- Harry S Truman "
collapsedString = fn CollapseString( string )
printf@"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
string = @"\"AAAAAll that glitters is not goldDDDD.\" - William Shakespeare"
collapsedString = fn CollapseString( string )
printf@"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
HandleEvents
</syntaxhighlight>
 
{{out}}
<pre>
<<<>>> 0
<<<>>> 0
 
<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> 72
<<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> 70
 
<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> 72
<<<.178>>> 4
 
<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> 72
<<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> 69
 
<<< --- Harry S Truman >>> 72
<<< - Hary S Truman >>> 17
 
<<<"AAAAAll that glitters is not goldDDDD." - William Shakespeare>>> 62
<<<"Al that gliters is not goldD." - Wiliam Shakespeare>>> 52
</pre>
 
Line 1,583 ⟶ 1,673:
original : length = 8, string = «««😍😀🙌💃😍😍😍🙌»»»
collapsed: length = 6, string = «««😍😀🙌💃😍🙌»»»
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn CollapseString( inString as CFStringRef ) as CFStringRef
CFMutableStringRef outString = fn MutableStringWithCapacity(0)
long index
unichar prevChar = 0, currChar
for index = 0 to len(inString) - 1
currChar = fn StringCharacterAtIndex( inString, index )
if ( currChar != prevChar ) then MutableStringAppendFormat( outString, @"%C", currChar )
prevChar = currChar
next
end fn = outString
 
window 1, @"Collapse String", (0,0,600,300)
 
CFStringRef string, collapsedString
 
string = @""
collapsedString = fn CollapseString( string )
printf @"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
string = @"\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "
collapsedString = fn CollapseString( string )
printf@"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
string = @"..1111111111111111111111111111111111111111111111111111111111111117777888"
collapsedString = fn CollapseString( string )
printf@"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
string = @"I never give 'em hell, I just tell the truth, and they think it's hell. "
collapsedString = fn CollapseString( string )
printf@"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
string = @" --- Harry S Truman "
collapsedString = fn CollapseString( string )
printf@"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
string = @"\"AAAAAll that glitters is not goldDDDD.\" - William Shakespeare"
collapsedString = fn CollapseString( string )
printf@"<<<%@>>> %ld\n<<<%@>>> %ld\n",string,len(string),collapsedString,len(collapsedString)
 
HandleEvents
</syntaxhighlight>
 
{{out}}
<pre>
<<<>>> 0
<<<>>> 0
 
<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> 72
<<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> 70
 
<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> 72
<<<.178>>> 4
 
<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> 72
<<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> 69
 
<<< --- Harry S Truman >>> 72
<<< - Hary S Truman >>> 17
 
<<<"AAAAAll that glitters is not goldDDDD." - William Shakespeare>>> 62
<<<"Al that gliters is not goldD." - Wiliam Shakespeare>>> 52
</pre>
 
Line 3,288 ⟶ 3,312:
Length: 1 <<<A>>>
</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, ('')
('"If I were two-faced, would I be wearing this '
'one?" --- Abraham Lincoln ')
('..11111111111111111111111111111111111111111111'
'11111111111111111117777888')
('I never give \'em hell, I just tell the truth, '
'and they think it\'s hell. ')
(' '
' --- Harry S Truman '): e.Strings
= <Each Show e.Strings>;
};
 
Each {
s.F = ;
s.F t.I e.X = <Mu s.F t.I> <Each s.F e.X>;
};
 
Brackets {
e.X, <Lenw e.X>: s.L e.X =
<Prout <Symb s.L> ': <<<' e.X '>>>'>;
};
 
Show {
(e.X) = <Brackets e.X>
<Brackets <Collapse e.X>>
<Prout>;
};
 
Collapse {
= ;
s.C s.C e.S = <Collapse s.C e.S>;
s.C e.S = s.C <Collapse e.S>;
};</syntaxhighlight>
{{out}}
<pre>0: <<<>>>
0: <<<>>>
 
72: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
70: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>
 
72: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
4: <<<.178>>>
 
72: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
69: <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>
 
72: <<< --- Harry S Truman >>>
17: <<< - Hary S Truman >>></pre>
 
=={{header|REXX}}==
Line 3,390 ⟶ 3,465:
 
=={{header|RPL}}==
{{trans|Kotlin}}
{{works with|Halcyon Calc|4.2.7}}
{{trans|Kotlin}}
{| class="wikitable"
! RPL code
Line 3,399 ⟶ 3,474:
≪ → string
≪ "" DUP
1 string SIZE '''FOR''' j
string j DUP SUB
'''IF''' DUP2 ≠ '''THEN'''
Line 3,406 ⟶ 3,481:
'''END''' DROP
'''NEXT''' DROP
≫ ≫ ‘'''<span style="color:blue">CLAPS</span>'''’ STO
|
'''<span style="color:blue">CLAPS</span>''' ''( "strrinng" -- "string" )''
output string = last = ""
scan the input string
Line 3,419 ⟶ 3,494:
|}
{{works with|HP|49}}
« → string
« { "" }
string SIZE 1 '''FOR''' j
string j DUP SUB SWAP
'''IF''' DUP2 HEAD == '''THEN''' NIP '''ELSE''' + '''END'''
-1 '''STEP'''
∑LIST
» » ‘'''<span style="color:blue">CLAPS</span>'''’ STO
When displaying strings, RPL always adds double quotes. To fulfill the artistic touch requirement, we have used guillemets to bracket Lincoln's statement.
≪ { ""
{{in}}
"≪ If I were two-faced, would I be wearing this one? ≫ --- Abraham Lincoln "
<pre>
"..1111111111111111111111111111111111111111111111111111111111111117777888"
≪ { ""
"I Ifnever Igive were'em two-facedhell, would I bejust wearingtell thisthe truth, one?and they ---think Abrahamit's Lincolnhell. "
" --- Harry S Truman " }
"..1111111111111111111111111111111111111111111111111111111111111117777888"
1 5 '''FOR''' j DUP j GET <span style="color:blue">CLAPS</span> SWAP '''NEXT''' DROP
"I never give 'em hell, I just tell the truth, and they think it's hell. "
≫ EVAL
" --- Harry S Truman " }
1 5 FOR j DUP j GET CLAPS SWAP NEXT DROP
≫ EVAL
</pre>
{{out}}
<pre>
Line 4,022 ⟶ 4,103:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
// Returns collapsed string, original and new lengths in
2,096

edits