Determine if a string is collapsible: Difference between revisions

Add Refal
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(Add Refal)
 
(8 intermediate revisions by 6 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,584 ⟶ 1,674:
collapsed: length = 6, string = «««😍😀🙌💃😍🙌»»»
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
Line 2,210 ⟶ 2,301:
ans = ║ - Hary S Truman ║
PASSES 1 out of 1 test
</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map test tests))]
 
tests :: [[char]]
tests
= [
"",
"\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ",
"..1111111111111111111111111111111111111111111111111111111111111111111788",
"I never give 'em hell, I just tell the truth, and they think it's hell. ",
" --- Harry S Truman "
]
 
test :: [char]->[char]
test s = lay [before, after]
where before = disp s
after = disp (collapse s)
 
disp :: [char]->[char]
disp s = show (#s) ++ ": <<<" ++ s ++ ">>>"
 
collapse :: [*]->[*]
collapse [] = []
collapse [x] = [x]
collapse (x:y:xs) = collapse (y:xs), if x=y
= x:collapse (y:xs), otherwise</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: <<<..1111111111111111111111111111111111111111111111111111111111111111111788>>>
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>
 
Line 3,177 ⟶ 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,276 ⟶ 3,462:
 
done...
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{{trans|Kotlin}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → string
≪ "" DUP
1 string SIZE '''FOR''' j
string j DUP SUB
'''IF''' DUP2 ≠ '''THEN'''
ROT OVER +
SWAP ROT
'''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
c = jth character
if c ≠ last
output string += c
last = c
forget c or previous last
clean stack
|}
{{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.
≪ { ""
"≪ 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 " }
1 5 '''FOR''' j DUP j GET <span style="color:blue">CLAPS</span> SWAP '''NEXT''' DROP
≫ EVAL
{{out}}
<pre>
5: ""
4: "≪ If I were two-faced, would I be wearing this one? ≫ - Abraham Lincoln "
3: ".178"
2: "I never give 'em hel, I just tel the truth, and they think it's hel. "
1: " - Hary S Truman "
</pre>
 
Line 3,482 ⟶ 3,724:
COLLAPSED : length = 7, string = «««ardvark»»»
This string IS collapsible !</pre>
 
=={{header|sed}}==
Since sed has no native support for arithmetic, line length counting is omitted in this solution for simplicity:
<syntaxhighlight lang="sed">h
s/.*/<<<&>>>/
x
s/\(.\)\1*/\1/g
s/.*/<<<&>>>/
H
s/.*//
G</syntaxhighlight>
Test:
<pre>
printf '%s\n' \
'' \
'"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 ' \
| sed -f collapse.sed
</pre>
{{out}}
<pre>
 
<<<>>>
<<<>>>
 
<<<"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 >>>
 
<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
<<<.178>>>
 
<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
<<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>
 
<<< --- Harry S Truman >>>
<<< - Hary S Truman >>>
</pre>
 
=={{header|Sidef}}==
Line 3,822 ⟶ 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